diff --git a/.gitignore b/.gitignore index 0b2fa01..220eede 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ */**/*.yml -*.yml +conf.yml* *.out */**/main main \ No newline at end of file diff --git a/README-cn.md b/README-cn.md index f3630fc..7d03075 100644 --- a/README-cn.md +++ b/README-cn.md @@ -2,6 +2,14 @@ 跨语言--语言无关,基于http协议 支持xa、tcc、saga ## 快速开始 + dtm依赖于mysql + 使用已有的mysql: + 参考conf.yml.sample将mysql信息填写到conf.yml + 或者通过docker安装mysql + docker-compose up -f docker-compose.mysql.yml + 启动并运行saga示例 + go run app/main.go + 场景描述: 假设您实现了一个转账功能,分为两个微服务:转入、转出 转出:服务地址为 http://example.com/api/busi_saga/transOut?gid=xxx POST 参数为 {"uid": 2, "amount":30} diff --git a/common/types_test.go b/common/types_test.go index 4c24c04..f951dff 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -13,8 +13,8 @@ type testConfig struct { var config = testConfig{} var myinit int = func() int { - InitApp(GetProjectPath(), &config) - config.Mysql["database"] = "dtm" + InitApp(GetProjectDir(), &config) + config.Mysql["database"] = "" return 0 }() diff --git a/common/utils.go b/common/utils.go index 3b0971f..28820e4 100644 --- a/common/utils.go +++ b/common/utils.go @@ -202,6 +202,10 @@ func InitApp(dir string, config interface{}) { configLoaded[dir] = true viper.SetConfigFile(dir + "/conf.yml") err := viper.ReadInConfig() + if err != nil { + viper.SetConfigFile(dir + "/conf.sample.yml") + err = viper.ReadInConfig() + } E2P(err) } err := viper.Unmarshal(config) diff --git a/common/utils_test.go b/common/utils_test.go index 669d3c7..50a9128 100644 --- a/common/utils_test.go +++ b/common/utils_test.go @@ -96,6 +96,6 @@ func TestResty(t *testing.T) { } func TestCaller(t *testing.T) { - p := GetProjectPath() + p := GetProjectDir() assert.Equal(t, true, strings.HasSuffix(filepath.Dir(p), "common")) } diff --git a/compose.mysql.yml b/compose.mysql.yml new file mode 100644 index 0000000..bd99691 --- /dev/null +++ b/compose.mysql.yml @@ -0,0 +1,8 @@ +version: '3.3' +services: + mysql: + image: 'mysql:5.7' + environment: + MYSQL_ROOT_PASSWORD: my-secret-pw + ports: + - '3306:3306' diff --git a/conf.yml.sample b/conf.sample.yml similarity index 91% rename from conf.yml.sample rename to conf.sample.yml index 6506f1d..2f91b07 100644 --- a/conf.yml.sample +++ b/conf.sample.yml @@ -1,8 +1,7 @@ Mysql: - host: 'mysql' + host: 'localhost' user: 'root' password: 'my-secret-pw' - database: 'dtm' port: '3306' PreparedExpire: 90 # 单位秒,处于prepared中的任务,过了这个时间,查询结果还是PENDING的话,则会被cancel diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 2f7d47b..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,15 +0,0 @@ -version: '3.3' -services: - api: - build: . - ports: - - '80:4005' - volumes: - - .:/app/dtm - # command: ['sh', '-c', 'go build app/main.go && go test -v ./dtmsvr'] - mysql: - image: 'mysql:5.7' - environment: - MYSQL_ROOT_PASSWORD: my-secret-pw - ports: - - '3306:3306' diff --git a/dtmsvr/conf.yml.sample b/dtmsvr/conf.yml.sample deleted file mode 100644 index ef212d1..0000000 --- a/dtmsvr/conf.yml.sample +++ /dev/null @@ -1,8 +0,0 @@ -Mysql: - host: 'mysql' - user: 'root' - password: 'my-secret-pw' - database: 'dtm' - port: '3306' -PreparedExpire: 90 # 单位秒,处于prepared中的任务,过了这个时间,查询结果还是PENDING的话,则会被cancel -TransCronInterval: 20 # 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和commited的任务 diff --git a/dtmsvr/config.go b/dtmsvr/config.go index 01fdda4..85df1c8 100644 --- a/dtmsvr/config.go +++ b/dtmsvr/config.go @@ -10,3 +10,5 @@ var config = &dtmsvrConfig{ PreparedExpire: 60, JobCronInterval: 20, } + +var dbName = "dtm" diff --git a/dtmsvr/dtmsvr_test.go b/dtmsvr/dtmsvr_test.go index e60d535..a9541d0 100644 --- a/dtmsvr/dtmsvr_test.go +++ b/dtmsvr/dtmsvr_test.go @@ -14,7 +14,7 @@ import ( var myinit int = func() int { common.InitApp(common.GetProjectDir(), &config) - config.Mysql["database"] = "dtm" + config.Mysql["database"] = dbName return 0 }() @@ -26,12 +26,13 @@ func TestViper(t *testing.T) { func TestDtmSvr(t *testing.T) { TransProcessedTestChan = make(chan string, 1) PopulateMysql() + examples.PopulateMysql() // 启动组件 go StartSvr() go examples.SagaStartSvr() go examples.XaStartSvr() go examples.TccStartSvr() - time.Sleep(time.Duration(100 * 1000 * 1000)) + time.Sleep(time.Duration(200 * 1000 * 1000)) // 清理数据 e2p(dbGet().Exec("truncate trans_global").Error) diff --git a/dtmsvr/main.go b/dtmsvr/main.go index 7b267b1..02cd39c 100644 --- a/dtmsvr/main.go +++ b/dtmsvr/main.go @@ -17,7 +17,7 @@ func Main() { func StartSvr() { logrus.Printf("start dtmsvr") common.InitApp(common.GetProjectDir(), &config) - config.Mysql["database"] = "dtm" + config.Mysql["database"] = dbName app := common.GetGinApp() AddRoute(app) logrus.Printf("dtmsvr listen at: %d", dtmsvrPort) @@ -26,6 +26,6 @@ func StartSvr() { func PopulateMysql() { common.InitApp(common.GetProjectDir(), &config) - config.Mysql["database"] = "dtm" + config.Mysql["database"] = dbName examples.RunSqlScript(config.Mysql, common.GetCurrentDir()+"/dtmsvr.sql") } diff --git a/examples/config.go b/examples/config.go index 1f787be..074028c 100644 --- a/examples/config.go +++ b/examples/config.go @@ -5,3 +5,5 @@ type exampleConfig struct { } var Config = exampleConfig{} + +var dbName = "dtm_busi" diff --git a/examples/data.go b/examples/data.go index bcc833e..74407bf 100644 --- a/examples/data.go +++ b/examples/data.go @@ -31,6 +31,6 @@ func RunSqlScript(mysql map[string]string, script string) { func PopulateMysql() { common.InitApp(common.GetProjectDir(), &Config) - Config.Mysql["database"] = "dtm_busi" + Config.Mysql["database"] = dbName RunSqlScript(Config.Mysql, common.GetCurrentDir()+"/examples.sql") } diff --git a/xa.go b/xa.go index 4b2bbe8..9f6439e 100644 --- a/xa.go +++ b/xa.go @@ -103,7 +103,3 @@ func (xa *XaClient) XaGlobalTransaction(gid string, transFunc XaGlobalFunc) (rer } return nil } - -func getDb(conf map[string]string) *common.MyDb { - return common.DbGet(conf) -}