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"
|
||||
"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)
|
||||
|
||||
@ -13,7 +13,7 @@ type testConfig struct {
|
||||
var config = testConfig{}
|
||||
|
||||
var myinit int = func() int {
|
||||
InitApp(&config)
|
||||
InitApp(GetCurrentPath(), &config)
|
||||
return 0
|
||||
}()
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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"))
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
version: '3.3'
|
||||
services:
|
||||
web:
|
||||
api:
|
||||
build: .
|
||||
ports:
|
||||
- '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;
|
||||
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的步骤',
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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")
|
||||
}
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -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{
|
||||
|
||||
@ -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")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user