test passed
This commit is contained in:
parent
75ba3e30cc
commit
4f1939c7ac
16
app/main.go
16
app/main.go
@ -4,6 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/dtmsvr"
|
"github.com/yedf/dtm/dtmsvr"
|
||||||
"github.com/yedf/dtm/examples"
|
"github.com/yedf/dtm/examples"
|
||||||
)
|
)
|
||||||
@ -11,13 +12,22 @@ import (
|
|||||||
type M = map[string]interface{}
|
type M = map[string]interface{}
|
||||||
|
|
||||||
func main() {
|
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 dtmsvr.StartSvr()
|
||||||
go examples.SagaStartSvr()
|
go examples.SagaStartSvr()
|
||||||
go examples.TccStartSvr()
|
go examples.TccStartSvr()
|
||||||
go examples.XaStartSvr()
|
go examples.XaStartSvr()
|
||||||
} else if os.Args[1] == "dtmsvr" {
|
} else {
|
||||||
go dtmsvr.StartSvr()
|
logrus.Fatalf("unknown arg: %s", os.Args[1])
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
time.Sleep(1000 * time.Second)
|
time.Sleep(1000 * time.Second)
|
||||||
|
|||||||
@ -13,7 +13,7 @@ type testConfig struct {
|
|||||||
var config = testConfig{}
|
var config = testConfig{}
|
||||||
|
|
||||||
var myinit int = func() int {
|
var myinit int = func() int {
|
||||||
InitApp(&config)
|
InitApp(GetCurrentPath(), &config)
|
||||||
return 0
|
return 0
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -195,16 +196,25 @@ func (f *formatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|||||||
var configLoaded = map[string]bool{}
|
var configLoaded = map[string]bool{}
|
||||||
|
|
||||||
// 加载调用者文件相同目录下的配置文件
|
// 加载调用者文件相同目录下的配置文件
|
||||||
func InitApp(config interface{}) {
|
func InitApp(dir string, config interface{}) {
|
||||||
logrus.SetFormatter(&formatter{})
|
logrus.SetFormatter(&formatter{})
|
||||||
_, file, _, _ := runtime.Caller(1)
|
if !configLoaded[dir] {
|
||||||
fileName := filepath.Dir(file) + "/conf.yml"
|
configLoaded[dir] = true
|
||||||
if !configLoaded[fileName] {
|
viper.SetConfigFile(dir + "/conf.yml")
|
||||||
configLoaded[fileName] = true
|
|
||||||
viper.SetConfigFile(fileName)
|
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
E2P(err)
|
E2P(err)
|
||||||
}
|
}
|
||||||
err := viper.Unmarshal(config)
|
err := viper.Unmarshal(config)
|
||||||
E2P(err)
|
E2P(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Getwd() string {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
E2P(err)
|
||||||
|
return wd
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCurrentPath() string {
|
||||||
|
_, file, _, _ := runtime.Caller(1)
|
||||||
|
return filepath.Dir(file)
|
||||||
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -93,3 +94,8 @@ func TestResty(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.NotEqual(t, nil, err2)
|
assert.NotEqual(t, nil, err2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCaller(t *testing.T) {
|
||||||
|
p := GetCurrentFilePath()
|
||||||
|
assert.Equal(t, true, strings.HasSuffix(filepath.Dir(p), "common"))
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
version: '3.3'
|
version: '3.3'
|
||||||
services:
|
services:
|
||||||
web:
|
api:
|
||||||
build: .
|
build: .
|
||||||
ports:
|
ports:
|
||||||
- '80:4005'
|
- '80:4005'
|
||||||
|
|||||||
@ -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;
|
drop table IF EXISTS saga;
|
||||||
CREATE TABLE `saga` (
|
CREATE TABLE IF NOT EXISTS `saga` (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`gid` varchar(45) NOT NULL COMMENT '事务全局id',
|
`gid` varchar(45) NOT NULL COMMENT '事务全局id',
|
||||||
`steps` json NOT NULL COMMENT 'saga的所有步骤',
|
`steps` json NOT NULL COMMENT 'saga的所有步骤',
|
||||||
@ -20,7 +20,7 @@ CREATE TABLE `saga` (
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
drop table IF EXISTS saga_step;
|
drop table IF EXISTS saga_step;
|
||||||
CREATE TABLE `saga_step` (
|
CREATE TABLE IF NOT EXISTS `saga_step` (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`gid` varchar(45) NOT NULL COMMENT '事务全局id',
|
`gid` varchar(45) NOT NULL COMMENT '事务全局id',
|
||||||
`data` json DEFAULT NULL COMMENT '请求所携带的数据',
|
`data` json DEFAULT NULL COMMENT '请求所携带的数据',
|
||||||
@ -39,7 +39,7 @@ CREATE TABLE `saga_step` (
|
|||||||
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4;
|
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
drop table IF EXISTS trans_log;
|
drop table IF EXISTS trans_log;
|
||||||
CREATE TABLE `trans_log` (
|
CREATE TABLE IF NOT EXISTS `trans_log` (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`gid` varchar(45) NOT NULL COMMENT '事务全局id',
|
`gid` varchar(45) NOT NULL COMMENT '事务全局id',
|
||||||
`step` int(11) DEFAULT NULL COMMENT 'saga的步骤',
|
`step` int(11) DEFAULT NULL COMMENT 'saga的步骤',
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var myinit int = func() int {
|
var myinit int = func() int {
|
||||||
common.InitApp(&config)
|
common.InitApp(common.GetCurrentPath(), &config)
|
||||||
return 0
|
return 0
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ func TestViper(t *testing.T) {
|
|||||||
|
|
||||||
func TestDtmSvr(t *testing.T) {
|
func TestDtmSvr(t *testing.T) {
|
||||||
TransProcessedTestChan = make(chan string, 1)
|
TransProcessedTestChan = make(chan string, 1)
|
||||||
|
PopulateMysql()
|
||||||
// 启动组件
|
// 启动组件
|
||||||
go StartSvr()
|
go StartSvr()
|
||||||
go examples.SagaStartSvr()
|
go examples.SagaStartSvr()
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package dtmsvr
|
|||||||
import (
|
import (
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
|
"github.com/yedf/dtm/examples"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Main() {
|
func Main() {
|
||||||
@ -11,9 +12,14 @@ func Main() {
|
|||||||
|
|
||||||
func StartSvr() {
|
func StartSvr() {
|
||||||
logrus.Printf("start dtmsvr")
|
logrus.Printf("start dtmsvr")
|
||||||
common.InitApp(&config)
|
common.InitApp(common.GetCurrentPath(), &config)
|
||||||
app := common.GetGinApp()
|
app := common.GetGinApp()
|
||||||
AddRoute(app)
|
AddRoute(app)
|
||||||
logrus.Printf("dtmsvr listen at: 8080")
|
logrus.Printf("dtmsvr listen at: 8080")
|
||||||
app.Run(":8080")
|
app.Run(":8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PopulateMysql() {
|
||||||
|
common.InitApp(common.GetCurrentPath(), &config)
|
||||||
|
examples.RunSqlScript(config.Mysql, common.GetCurrentPath()+"/dtmsvr.sql")
|
||||||
|
}
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
CREATE DATABASE `dtm_busi` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
|
||||||
use dtm_busi;
|
use dtm_busi;
|
||||||
drop table if exists user_account;
|
drop table if exists user_account;
|
||||||
create table user_account(
|
create table user_account(
|
||||||
|
|||||||
@ -18,7 +18,7 @@ var SagaBusi = fmt.Sprintf("http://localhost:%d%s", SagaBusiPort, SagaBusiApi)
|
|||||||
|
|
||||||
func SagaMain() {
|
func SagaMain() {
|
||||||
go SagaStartSvr()
|
go SagaStartSvr()
|
||||||
sagaFireRequest()
|
SagaFireRequest()
|
||||||
time.Sleep(1000 * time.Second)
|
time.Sleep(1000 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ func SagaStartSvr() {
|
|||||||
app.Run(fmt.Sprintf(":%d", SagaBusiPort))
|
app.Run(fmt.Sprintf(":%d", SagaBusiPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
func sagaFireRequest() {
|
func SagaFireRequest() {
|
||||||
gid := common.GenGid()
|
gid := common.GenGid()
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
logrus.Printf("busi transaction begin: %s", gid)
|
||||||
req := &TransReq{
|
req := &TransReq{
|
||||||
|
|||||||
@ -39,7 +39,8 @@ func XaMain() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func XaStartSvr() {
|
func XaStartSvr() {
|
||||||
common.InitApp(&Config)
|
common.InitApp(common.GetCurrentPath(), &Config)
|
||||||
|
dbGet()
|
||||||
logrus.Printf("xa examples starting")
|
logrus.Printf("xa examples starting")
|
||||||
app := common.GetGinApp()
|
app := common.GetGinApp()
|
||||||
XaClient = dtm.XaClientNew(DtmServer, Config.Mysql, app, XaBusi+"/xa")
|
XaClient = dtm.XaClientNew(DtmServer, Config.Mysql, app, XaBusi+"/xa")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user