diff --git a/app/main.go b/app/main.go index 2f35461..6fba92a 100644 --- a/app/main.go +++ b/app/main.go @@ -4,6 +4,7 @@ import ( "os" "time" + "github.com/sirupsen/logrus" "github.com/yedf/dtm/dtmsvr" "github.com/yedf/dtm/examples" ) @@ -11,13 +12,22 @@ import ( type M = map[string]interface{} func main() { - if len(os.Args) == 1 { // 所有服务都启动 + if len(os.Args) == 1 { // 默认情况下,展示saga例子 + examples.PopulateMysql() + go dtmsvr.StartSvr() + go examples.SagaStartSvr() + examples.SagaFireRequest() + } else if os.Args[1] == "dtmsvr" { + go dtmsvr.StartSvr() + } else if os.Args[1] == "all" { + dtmsvr.PopulateMysql() + examples.PopulateMysql() go dtmsvr.StartSvr() go examples.SagaStartSvr() go examples.TccStartSvr() go examples.XaStartSvr() - } else if os.Args[1] == "dtmsvr" { - go dtmsvr.StartSvr() + } else { + logrus.Fatalf("unknown arg: %s", os.Args[1]) } for { time.Sleep(1000 * time.Second) diff --git a/common/conf.yml.sample b/common/conf.yml.sample deleted file mode 100644 index e69de29..0000000 diff --git a/common/types_test.go b/common/types_test.go index 9ac4fff..fb42e32 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -13,7 +13,7 @@ type testConfig struct { var config = testConfig{} var myinit int = func() int { - InitApp(&config) + InitApp(GetCurrentPath(), &config) return 0 }() diff --git a/common/utils.go b/common/utils.go index 6eab953..28e648c 100644 --- a/common/utils.go +++ b/common/utils.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "path" "path/filepath" "runtime" @@ -195,16 +196,25 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) { var configLoaded = map[string]bool{} // 加载调用者文件相同目录下的配置文件 -func InitApp(config interface{}) { +func InitApp(dir string, config interface{}) { logrus.SetFormatter(&formatter{}) - _, file, _, _ := runtime.Caller(1) - fileName := filepath.Dir(file) + "/conf.yml" - if !configLoaded[fileName] { - configLoaded[fileName] = true - viper.SetConfigFile(fileName) + if !configLoaded[dir] { + configLoaded[dir] = true + viper.SetConfigFile(dir + "/conf.yml") err := viper.ReadInConfig() E2P(err) } err := viper.Unmarshal(config) E2P(err) } + +func Getwd() string { + wd, err := os.Getwd() + E2P(err) + return wd +} + +func GetCurrentPath() string { + _, file, _, _ := runtime.Caller(1) + return filepath.Dir(file) +} diff --git a/common/utils_test.go b/common/utils_test.go index 1c92d23..27189c5 100644 --- a/common/utils_test.go +++ b/common/utils_test.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "net/http/httptest" + "path/filepath" "strings" "testing" @@ -93,3 +94,8 @@ func TestResty(t *testing.T) { }) assert.NotEqual(t, nil, err2) } + +func TestCaller(t *testing.T) { + p := GetCurrentFilePath() + assert.Equal(t, true, strings.HasSuffix(filepath.Dir(p), "common")) +} diff --git a/docker-compose.yml b/docker-compose.yml index 3ec9779..9362d71 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ version: '3.3' services: - web: + api: build: . ports: - '80:4005' diff --git a/dtmsvr/dtmsvr.sql b/dtmsvr/dtmsvr.sql index 3576951..2641759 100644 --- a/dtmsvr/dtmsvr.sql +++ b/dtmsvr/dtmsvr.sql @@ -1,9 +1,9 @@ --- CREATE DATABASE `dtm` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; +CREATE DATABASE IF NOT EXISTS `dtm` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; --- use dtm; +use dtm; drop table IF EXISTS saga; -CREATE TABLE `saga` ( +CREATE TABLE IF NOT EXISTS `saga` ( `id` int(11) NOT NULL AUTO_INCREMENT, `gid` varchar(45) NOT NULL COMMENT '事务全局id', `steps` json NOT NULL COMMENT 'saga的所有步骤', @@ -20,7 +20,7 @@ CREATE TABLE `saga` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; drop table IF EXISTS saga_step; -CREATE TABLE `saga_step` ( +CREATE TABLE IF NOT EXISTS `saga_step` ( `id` int(11) NOT NULL AUTO_INCREMENT, `gid` varchar(45) NOT NULL COMMENT '事务全局id', `data` json DEFAULT NULL COMMENT '请求所携带的数据', @@ -39,7 +39,7 @@ CREATE TABLE `saga_step` ( ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4; drop table IF EXISTS trans_log; -CREATE TABLE `trans_log` ( +CREATE TABLE IF NOT EXISTS `trans_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `gid` varchar(45) NOT NULL COMMENT '事务全局id', `step` int(11) DEFAULT NULL COMMENT 'saga的步骤', diff --git a/dtmsvr/dtmsvr_test.go b/dtmsvr/dtmsvr_test.go index 7f57bea..319d042 100644 --- a/dtmsvr/dtmsvr_test.go +++ b/dtmsvr/dtmsvr_test.go @@ -13,7 +13,7 @@ import ( ) var myinit int = func() int { - common.InitApp(&config) + common.InitApp(common.GetCurrentPath(), &config) return 0 }() @@ -24,7 +24,7 @@ func TestViper(t *testing.T) { func TestDtmSvr(t *testing.T) { TransProcessedTestChan = make(chan string, 1) - + PopulateMysql() // 启动组件 go StartSvr() go examples.SagaStartSvr() diff --git a/dtmsvr/main.go b/dtmsvr/main.go index 9118d3a..0577e9f 100644 --- a/dtmsvr/main.go +++ b/dtmsvr/main.go @@ -3,6 +3,7 @@ package dtmsvr import ( "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" + "github.com/yedf/dtm/examples" ) func Main() { @@ -11,9 +12,14 @@ func Main() { func StartSvr() { logrus.Printf("start dtmsvr") - common.InitApp(&config) + common.InitApp(common.GetCurrentPath(), &config) app := common.GetGinApp() AddRoute(app) logrus.Printf("dtmsvr listen at: 8080") app.Run(":8080") } + +func PopulateMysql() { + common.InitApp(common.GetCurrentPath(), &config) + examples.RunSqlScript(config.Mysql, common.GetCurrentPath()+"/dtmsvr.sql") +} diff --git a/examples/examples.sql b/examples/examples.sql index b5e2d9d..0b2ae44 100644 --- a/examples/examples.sql +++ b/examples/examples.sql @@ -1,3 +1,4 @@ +CREATE DATABASE `dtm_busi` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; use dtm_busi; drop table if exists user_account; create table user_account( diff --git a/examples/main_saga.go b/examples/main_saga.go index 7a6fc79..d39283f 100644 --- a/examples/main_saga.go +++ b/examples/main_saga.go @@ -18,7 +18,7 @@ var SagaBusi = fmt.Sprintf("http://localhost:%d%s", SagaBusiPort, SagaBusiApi) func SagaMain() { go SagaStartSvr() - sagaFireRequest() + SagaFireRequest() time.Sleep(1000 * time.Second) } @@ -29,7 +29,7 @@ func SagaStartSvr() { app.Run(fmt.Sprintf(":%d", SagaBusiPort)) } -func sagaFireRequest() { +func SagaFireRequest() { gid := common.GenGid() logrus.Printf("busi transaction begin: %s", gid) req := &TransReq{ diff --git a/examples/main_xa.go b/examples/main_xa.go index 5709c9c..e42e447 100644 --- a/examples/main_xa.go +++ b/examples/main_xa.go @@ -39,7 +39,8 @@ func XaMain() { } func XaStartSvr() { - common.InitApp(&Config) + common.InitApp(common.GetCurrentPath(), &Config) + dbGet() logrus.Printf("xa examples starting") app := common.GetGinApp() XaClient = dtm.XaClientNew(DtmServer, Config.Mysql, app, XaBusi+"/xa")