From c5e3bc370d7ed894a66b6eea565e8d7e549d415a Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Thu, 29 Jul 2021 22:41:43 +0800 Subject: [PATCH] update qs example --- common/types_test.go | 7 +++---- common/utils.go | 4 ++-- dtmsvr/config.go | 7 +++++++ dtmsvr/dtmsvr_test.go | 2 +- dtmsvr/main.go | 4 ---- examples/config.go | 7 +++++++ examples/data.go | 2 -- examples/quick_start.go | 19 +++++++++++++++---- 8 files changed, 35 insertions(+), 17 deletions(-) diff --git a/common/types_test.go b/common/types_test.go index fda05db..b7e242e 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -12,11 +12,10 @@ type testConfig struct { var config = testConfig{} -var myinit int = func() int { - InitApp(GetProjectDir(), &config) +func init() { + InitConfig(GetProjectDir(), &config) config.Mysql["database"] = "" - return 0 -}() +} func TestDb(t *testing.T) { db := DbGet(config.Mysql) diff --git a/common/utils.go b/common/utils.go index 389a553..b059786 100644 --- a/common/utils.go +++ b/common/utils.go @@ -207,8 +207,8 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) { return b.Bytes(), nil } -// InitApp init config -func InitApp(dir string, config interface{}) { +// InitConfig init config +func InitConfig(dir string, config interface{}) { logrus.SetFormatter(&formatter{}) cont, err := ioutil.ReadFile(dir + "/conf.yml") if err != nil { diff --git a/dtmsvr/config.go b/dtmsvr/config.go index aeb2b55..b6fa47f 100644 --- a/dtmsvr/config.go +++ b/dtmsvr/config.go @@ -1,5 +1,7 @@ package dtmsvr +import "github.com/yedf/dtm/common" + type dtmsvrConfig struct { TransCronInterval int64 `yaml:"TransCronInterval"` // 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和committed的任务 Mysql map[string]string `yaml:"Mysql"` @@ -10,3 +12,8 @@ var config = &dtmsvrConfig{ } var dbName = "dtm" + +func init() { + common.InitConfig(common.GetProjectDir(), &config) + config.Mysql["database"] = "" +} diff --git a/dtmsvr/dtmsvr_test.go b/dtmsvr/dtmsvr_test.go index e1886b8..3d5b538 100644 --- a/dtmsvr/dtmsvr_test.go +++ b/dtmsvr/dtmsvr_test.go @@ -19,7 +19,7 @@ var app *gin.Engine func TestMain(m *testing.M) { TransProcessedTestChan = make(chan string, 1) - common.InitApp(common.GetProjectDir(), &config) + common.InitConfig(common.GetProjectDir(), &config) config.Mysql["database"] = dbName PopulateMysql(false) examples.PopulateMysql(false) diff --git a/dtmsvr/main.go b/dtmsvr/main.go index 8bdd966..685096f 100644 --- a/dtmsvr/main.go +++ b/dtmsvr/main.go @@ -20,8 +20,6 @@ func MainStart() { // StartSvr StartSvr func StartSvr() { logrus.Printf("start dtmsvr") - common.InitApp(common.GetProjectDir(), &config) - config.Mysql["database"] = dbName app := common.GetGinApp() addRoute(app) logrus.Printf("dtmsvr listen at: %d", dtmsvrPort) @@ -31,7 +29,5 @@ func StartSvr() { // PopulateMysql setup mysql data func PopulateMysql(skipDrop bool) { - common.InitApp(common.GetProjectDir(), &config) - config.Mysql["database"] = "" examples.RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql", skipDrop) } diff --git a/examples/config.go b/examples/config.go index f89045d..49ff2e3 100644 --- a/examples/config.go +++ b/examples/config.go @@ -1,5 +1,7 @@ package examples +import "github.com/yedf/dtm/common" + type exampleConfig struct { Mysql map[string]string `yaml:"Mysql"` } @@ -7,3 +9,8 @@ type exampleConfig struct { var config = exampleConfig{} var dbName = "dtm_busi" + +func init() { + common.InitConfig(common.GetProjectDir(), &config) + config.Mysql["database"] = dbName +} diff --git a/examples/data.go b/examples/data.go index 72da650..d9806a2 100644 --- a/examples/data.go +++ b/examples/data.go @@ -32,7 +32,5 @@ func RunSQLScript(mysql map[string]string, script string, skipDrop bool) { // PopulateMysql populate example mysql data func PopulateMysql(skipDrop bool) { - common.InitApp(common.GetProjectDir(), &config) - config.Mysql["database"] = dbName RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/examples.sql", skipDrop) } diff --git a/examples/quick_start.go b/examples/quick_start.go index a2f268a..d4c48d2 100644 --- a/examples/quick_start.go +++ b/examples/quick_start.go @@ -8,6 +8,7 @@ import ( "github.com/sirupsen/logrus" "github.com/yedf/dtm/common" "github.com/yedf/dtm/dtmcli" + "gorm.io/gorm" ) // 启动命令:go run app/main.go qs @@ -42,17 +43,27 @@ func QsFireRequest() string { return saga.Gid } +func qsAdjustBalance(uid int, amount int) (interface{}, error) { + err := dbGet().Transaction(func(tx *gorm.DB) error { + return tx.Model(&UserAccount{}).Where("user_id = ?", uid).Update("balance", gorm.Expr("balance + ?", amount)).Error + }) + if err != nil { + return nil, err + } + return M{"dtm_result": "SUCCESS"}, nil +} + func qsAddRoute(app *gin.Engine) { app.POST(qsBusiAPI+"/TransIn", common.WrapHandler(func(c *gin.Context) (interface{}, error) { - return M{"dtm_result": "SUCCESS"}, nil + return qsAdjustBalance(2, 30) })) app.POST(qsBusiAPI+"/TransInCompensate", common.WrapHandler(func(c *gin.Context) (interface{}, error) { - return M{"dtm_result": "SUCCESS"}, nil + return qsAdjustBalance(2, -30) })) app.POST(qsBusiAPI+"/TransOut", common.WrapHandler(func(c *gin.Context) (interface{}, error) { - return M{"dtm_result": "SUCCESS"}, nil + return qsAdjustBalance(1, -30) })) app.POST(qsBusiAPI+"/TransOutCompensate", common.WrapHandler(func(c *gin.Context) (interface{}, error) { - return M{"dtm_result": "SUCCESS"}, nil + return qsAdjustBalance(1, 30) })) }