rabbitmq seems ok

This commit is contained in:
yedongfu 2021-05-17 15:09:05 +08:00
parent 4ee3f54dba
commit 9e48e767b3
10 changed files with 80 additions and 99 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
dtm.yml
dtmsvr/dtmsvr.yml

View File

@ -6,7 +6,7 @@ dtm依赖于rabbitmq和mysql请搭建好rabbitmq和mysql并修改dtm.yml
## 启动tc
```go run tc```
```go run dtm-svr/svr```
## 启动例子saga的tm+rm

View File

@ -1,16 +0,0 @@
package common
import (
"path/filepath"
"runtime"
"github.com/spf13/viper"
)
func LoadConfig() {
_, file, _, _ := runtime.Caller(0)
viper.SetConfigFile(filepath.Dir(file) + "/../dtm.yml")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
}

View File

@ -5,10 +5,19 @@ import (
"github.com/go-playground/assert/v2"
"github.com/spf13/viper"
"github.com/yedf/dtm/common"
"github.com/yedf/dtm/dtmsvr"
)
func TestCtxKey(t *testing.T) {
common.LoadConfig()
assert.Equal(t, "http://localhost:8080/api/dtm/", viper.GetString("tc"))
func init() {
dtmsvr.LoadConfig()
}
func TestViper(t *testing.T) {
assert.Equal(t, "test_val", viper.GetString("test"))
}
func TTestDtmSvr(t *testing.T) {
// 发送Prepare请求后验证数据库
// ConsumeHalfMsg 验证数据库
// ConsumeMsg 验证数据库
}

View File

@ -1,57 +1,69 @@
package example
package examples
func TansIn() {
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/yedf/dtm/common"
"github.com/yedf/dtm/dtm"
)
type TransReq struct {
amount int
transInFailed bool
transOutFailed bool
}
func main() {
func TansIn(c *gin.Context) {
gid := c.Query("gid")
req := TransReq{}
if err := c.BindJSON(&req); err != nil {
return
}
logrus.Printf("%s TransIn: %v", gid, req)
if req.transInFailed {
logrus.Printf("%s TransIn %v failed", req)
c.Error(fmt.Errorf("TransIn failed for gid: %s", gid))
}
c.JSON(200, gin.H{"result": "SUCCESS"})
}
func TansInCompensate(c *gin.Context) {
gid := c.Query("gid")
req := TransReq{}
if err := c.BindJSON(&req); err != nil {
return
}
logrus.Printf("%s TransInCompensate: %v", gid, req)
c.JSON(200, gin.H{"result": "SUCCESS"})
}
func TransQuery(c *gin.Context) {
gid := c.Query("gid")
req := TransReq{}
if err := c.BindJSON(&req); err != nil {
return
}
logrus.Printf("%s TransQuery: %v", gid, req)
c.JSON(200, gin.H{"result": "SUCCESS"})
}
func trans(req *TransReq) {
gid := common.GenGid()
logrus.Printf("busi transaction begin: %s", gid)
saga := dtm.Saga{
Server: TcServer,
Gid: gid,
}
saga.Add(Busi+"/TransIn", Busi+"/TransInCompensate", req)
saga.Prepare(Busi + "TransQuery")
logrus.Printf("busi trans commit")
saga.Commit()
}
/*
import { ServiceContext } from "@ivy/api/globals"
import { generateTid } from "@ivy/api/ivy"
import { redis } from "@ivy/api/objects"
import { Saga } from '../../saga-cli'
async function getGlobalTid() {
return "global-" + await generateTid(redis)
}
export async function transIn(ctx: ServiceContext) {
let { gid, sid } = ctx.query
let { amount, transIn } = ctx.data
console.log(`gid: ${gid} sid: ${sid} transIn ${amount}`)
if (transIn === 'fail') {
throw { code: "DATA_ERROR", message: "tranIn error FAIL" }
}
return { messag: 'SUCCESS' }
}
export async function transInCompensate(ctx: ServiceContext) {
let { gid, sid } = ctx.query
let { amount } = ctx.data
console.log(`gid: ${gid} sid: ${sid} tranInCompensate ${amount}`)
return { message: 'SUCCESS' }
}
export async function transOut(ctx: ServiceContext) {
let { gid, sid } = ctx.query
let { amount, transIn, transOut } = ctx.data
console.log(`gid: ${gid} sid: ${sid} transOut ${amount}`)
if (transOut === 'fail') {
throw { code: "DATA_ERROR", message: "tranIn error FAIL" }
}
return { message: 'SUCCESS' }
}
export async function transOutCompensate(ctx: ServiceContext) {
let { gid, sid } = ctx.query
let { amount } = ctx.data
console.log(`gid: ${gid} sid: ${sid} tranOutCompensate ${amount}`)
return { message: 'SUCCESS' }
}
export async function transQuery(ctx: ServiceContext) {
let { gid } = ctx.query
return { message: 'SUCCESS' }

3
go.mod
View File

@ -3,6 +3,7 @@ module github.com/yedf/dtm
go 1.15
require (
github.com/bwmarrin/snowflake v0.3.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/assert/v2 v2.0.1
@ -10,11 +11,13 @@ require (
github.com/go-resty/resty/v2 v2.6.0
github.com/joho/godotenv v1.3.0
github.com/json-iterator/go v1.1.10
github.com/magiconair/properties v1.8.1
// github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/sleagon/ginfmt v1.1.1
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d
github.com/spf13/viper v1.7.1
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.7.0
// google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect

4
go.sum
View File

@ -23,6 +23,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
@ -237,6 +239,8 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

View File

@ -1,7 +0,0 @@
package service
import "github.com/gin-gonic/gin"
func AddRoute(engine *gin.Engine) {
}

View File

@ -1,9 +0,0 @@
package service
import (
"github.com/gin-gonic/gin"
)
func Prepare(c *gin.Context) {
}

15
tc.go
View File

@ -1,15 +0,0 @@
package main
import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/yedf/dtm/service"
)
func main() {
logrus.Printf("start tc")
gin.SetMode(gin.ReleaseMode)
app := gin.Default()
service.AddRoute(app)
app.Run()
}