From fe51e859835c942bd7774bbe7dd91f5ae18e9902 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Mon, 31 Jul 2023 00:41:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=BA=E9=BE=99=E8=88=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- fsm/main.go | 115 +++--------------- goctl_template/api/context.tpl | 2 +- server/auth/internal/config/config.go | 1 + server/auth/internal/svc/servicecontext.go | 4 +- server/backend/internal/config/config.go | 1 + server/backend/internal/svc/servicecontext.go | 2 +- server/canteen/internal/config/config.go | 2 + server/canteen/internal/svc/servicecontext.go | 4 +- .../data-transfer/internal/config/config.go | 2 + .../internal/svc/servicecontext.go | 4 +- .../home-user-auth/internal/config/config.go | 1 + .../internal/svc/servicecontext.go | 4 +- server/inventory/internal/config/config.go | 2 + .../inventory/internal/svc/servicecontext.go | 4 +- server/map-library/internal/config/config.go | 2 + .../internal/svc/servicecontext.go | 4 +- server/orders/internal/config/config.go | 2 + server/orders/internal/svc/servicecontext.go | 4 +- server/pay/internal/config/config.go | 1 + server/pay/internal/svc/servicecontext.go | 4 +- .../product-model/internal/config/config.go | 2 + .../internal/config/config.go | 2 + .../internal/svc/servicecontext.go | 4 +- .../internal/config/config.go | 2 + server/product/internal/config/config.go | 1 + server/product/internal/svc/servicecontext.go | 2 +- server/render/internal/config/config.go | 1 + server/render/internal/svc/servicecontext.go | 4 +- .../internal/config/config.go | 1 + .../internal/svc/servicecontext.go | 4 +- server/upload/internal/config/config.go | 1 + server/upload/internal/svc/servicecontext.go | 2 +- server/webset/internal/config/config.go | 1 + server/webset/internal/svc/servicecontext.go | 4 +- server/websocket/internal/config/config.go | 1 + .../websocket/internal/svc/servicecontext.go | 4 +- utils/autoconfig/autoconfig.go | 7 +- utils/basic/request_parse.go | 2 +- 39 files changed, 81 insertions(+), 133 deletions(-) diff --git a/.gitignore b/.gitignore index 9a4b0c48..1d2a48a9 100644 --- a/.gitignore +++ b/.gitignore @@ -52,4 +52,6 @@ server/product-model/product-model server/product-template/product-template server/shopping-cart-confirmation/shopping-cart-confirmation server/upload/upload -server/webset/webset \ No newline at end of file +server/webset/webset + +shared-state \ No newline at end of file diff --git a/fsm/main.go b/fsm/main.go index b0665b81..f0b35c4f 100644 --- a/fsm/main.go +++ b/fsm/main.go @@ -3,11 +3,9 @@ package fsm import ( "bytes" "encoding/gob" - "flag" "fmt" "fusenapi/utils/autoconfig" "log" - "net" "os" "os/signal" "path/filepath" @@ -44,17 +42,14 @@ var addresses []string = []string{ "localhost:5502", } -func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.DB) *dragonboat.NodeHost { +var shardID uint64 = 128 + +func StartNode(ServerID uint64, serverconfigs []*autoconfig.ConfigServer, gdb *gorm.DB) *SharedState { // addr := "localhost" // addr = fmt.Sprintf("%s:%d", addr, port) - flag.Parse() - if len(addr) == 0 && replicaID != 1 && replicaID != 2 && replicaID != 3 { - fmt.Fprintf(os.Stderr, "node id must be 1, 2 or 3 when address is not specified\n") - os.Exit(1) - } // https://github.com/golang/go/issues/17393 if runtime.GOOS == "darwin" { signal.Ignore(syscall.Signal(0xd)) @@ -67,17 +62,18 @@ func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.D // leave the initialMembers to be empty. we still populate the initialMembers // here for simplicity. - for idx, v := range addresses { + for _, v := range serverconfigs { // key is the ReplicaID, ReplicaID is not allowed to be 0 // value is the raft address - initialMembers[uint64(idx+1)] = v + + initialMembers[v.ReplicaId] = fmt.Sprintf("%s:%d", v.Host, v.Port-2000) } // for simplicity, in this example program, addresses of all those 3 initial // raft members are hard coded. when address is not specified on the command // line, we assume the node being launched is an initial raft member. - var nodeAddr = initialMembers[uint64(replicaID)] + var nodeAddr = initialMembers[ServerID] fmt.Fprintf(os.Stdout, "node address: %s\n", nodeAddr) // change the log verbosity @@ -89,8 +85,8 @@ func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.D // See GoDoc for all available options rc := config.Config{ // ShardID and ReplicaID of the raft node - ReplicaID: uint64(replicaID), - ShardID: exampleShardID, + ReplicaID: uint64(ServerID), + ShardID: shardID, ElectionRTT: 10, @@ -102,9 +98,8 @@ func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.D CompactionOverhead: 5, } datadir := filepath.Join( - "example-data", - "queue-data", - fmt.Sprintf("node%d", replicaID)) + "shared-state", + fmt.Sprintf("node%d", ServerID)) nhc := config.NodeHostConfig{ @@ -124,95 +119,19 @@ func StartNode(replicaID uint64, exampleShardID uint64, addr string, gdb *gorm.D panic(err) } - if err := nh.StartReplica(initialMembers, false, New, rc); err != nil { + if err := nh.StartReplica(initialMembers, false, NewFsStateMachine, rc); err != nil { fmt.Fprintf(os.Stderr, "failed to add cluster, %v\n", err) os.Exit(1) } - return nh - -} - -// StartNode 启动节点 -func StartNode1(ServerID string, RaftBind string, serverconfigs []*autoconfig.ConfigServer, gdb *gorm.DB) *FsStateMachine { - - fsm := &FsStateMachine{ - store: make(map[int64]*UserState), - - gdb: gdb, + ss := &SharedState{ + shardID: shardID, + replicaID: ServerID, + nh: nh, } - var retainSnapshotCount = 2 - // var ServerID string = "fs1" - // var RaftBind string = "localhost:5500" - var RaftDir string = fmt.Sprintf("/tmp/raftdir/%s", ServerID) + return ss - // Setup Raft configuration. - config := raft.DefaultConfig() - config.LocalID = raft.ServerID(ServerID) - - // Setup Raft communication. - addr, err := net.ResolveTCPAddr("tcp", RaftBind) - if err != nil { - panic(err) - } - transport, err := raft.NewTCPTransport(RaftBind, addr, 3, 30*time.Second, os.Stderr) - if err != nil { - panic(err) - } - - // Create the snapshot store. This allows the Raft to truncate the log. - snapshots, err := raft.NewFileSnapshotStore(RaftDir, retainSnapshotCount, os.Stderr) - if err != nil { - panic(fmt.Errorf("file snapshot store: %s", err)) - } - - // Create the log store and stable store. - logStore := raft.NewInmemStore() - stableStore := raft.NewInmemStore() - - // Create the Raft system. - fsm.ra, err = raft.NewRaft(config, fsm, logStore, stableStore, snapshots, transport) - if err != nil { - panic(err) - } - - var dup map[string]bool = make(map[string]bool) - var rserver []raft.Server = []raft.Server{ - { - Suffrage: raft.Voter, - ID: config.LocalID, - Address: transport.LocalAddr(), - }, - } - dup[string(config.LocalID)] = true - dup[string("backend")] = true - dup[string("product-model")] = true - dup[string("product-template")] = true - - for _, cfg := range serverconfigs { - if _, ok := dup[cfg.Name]; !ok { - dup[cfg.Name] = true - rserver = append(rserver, raft.Server{ - Suffrage: raft.Voter, - ID: raft.ServerID(cfg.Name), - Address: raft.ServerAddress(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port-2000)), - }) - } - } - - configuration := raft.Configuration{ - Servers: rserver, - } - - fu := fsm.ra.BootstrapCluster(configuration) - if err := fu.Error(); err != nil { - log.Println(err) - } - - waitForCluster(fsm.ra) - - return fsm } // func JoinCluster(ServerID string, LeaderAddress string, RaftBind string, gdb *gorm.DB) *StateCluster { diff --git a/goctl_template/api/context.tpl b/goctl_template/api/context.tpl index ba20b339..e061f249 100644 --- a/goctl_template/api/context.tpl +++ b/goctl_template/api/context.tpl @@ -26,7 +26,7 @@ type ServiceContext struct { func NewServiceContext(c {{.config}}) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("localhost:%d", c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/auth/internal/config/config.go b/server/auth/internal/config/config.go index 679c8627..16f1eb01 100644 --- a/server/auth/internal/config/config.go +++ b/server/auth/internal/config/config.go @@ -10,6 +10,7 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 MainAddress string diff --git a/server/auth/internal/svc/servicecontext.go b/server/auth/internal/svc/servicecontext.go index 2da74859..4b5bd92d 100644 --- a/server/auth/internal/svc/servicecontext.go +++ b/server/auth/internal/svc/servicecontext.go @@ -18,7 +18,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -28,7 +28,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("localhost:%d", c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/backend/internal/config/config.go b/server/backend/internal/config/config.go index 0e62a34a..7b0d28ab 100644 --- a/server/backend/internal/config/config.go +++ b/server/backend/internal/config/config.go @@ -10,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/backend/internal/svc/servicecontext.go b/server/backend/internal/svc/servicecontext.go index acd2937e..ecf8fd2a 100644 --- a/server/backend/internal/svc/servicecontext.go +++ b/server/backend/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen diff --git a/server/canteen/internal/config/config.go b/server/canteen/internal/config/config.go index 59d7e610..911b311e 100644 --- a/server/canteen/internal/config/config.go +++ b/server/canteen/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/canteen/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/canteen/internal/svc/servicecontext.go b/server/canteen/internal/svc/servicecontext.go index fd2be45e..61a3a516 100644 --- a/server/canteen/internal/svc/servicecontext.go +++ b/server/canteen/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/data-transfer/internal/config/config.go b/server/data-transfer/internal/config/config.go index ee387fda..b9730e56 100644 --- a/server/data-transfer/internal/config/config.go +++ b/server/data-transfer/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( types "fusenapi/server/data-transfer/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/data-transfer/internal/svc/servicecontext.go b/server/data-transfer/internal/svc/servicecontext.go index a0d0f308..692e7ace 100644 --- a/server/data-transfer/internal/svc/servicecontext.go +++ b/server/data-transfer/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/home-user-auth/internal/config/config.go b/server/home-user-auth/internal/config/config.go index 6a6fbbc6..b7aa6cae 100644 --- a/server/home-user-auth/internal/config/config.go +++ b/server/home-user-auth/internal/config/config.go @@ -10,6 +10,7 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 MainAddress string diff --git a/server/home-user-auth/internal/svc/servicecontext.go b/server/home-user-auth/internal/svc/servicecontext.go index 056f148d..d2a1256a 100644 --- a/server/home-user-auth/internal/svc/servicecontext.go +++ b/server/home-user-auth/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/inventory/internal/config/config.go b/server/inventory/internal/config/config.go index 34c626d1..5cc6c210 100644 --- a/server/inventory/internal/config/config.go +++ b/server/inventory/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/inventory/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/inventory/internal/svc/servicecontext.go b/server/inventory/internal/svc/servicecontext.go index b420b273..8d3dc8c2 100644 --- a/server/inventory/internal/svc/servicecontext.go +++ b/server/inventory/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/map-library/internal/config/config.go b/server/map-library/internal/config/config.go index cac8b83d..77b08c95 100644 --- a/server/map-library/internal/config/config.go +++ b/server/map-library/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/map-library/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/map-library/internal/svc/servicecontext.go b/server/map-library/internal/svc/servicecontext.go index dbcb4fd9..d4bb4043 100644 --- a/server/map-library/internal/svc/servicecontext.go +++ b/server/map-library/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/orders/internal/config/config.go b/server/orders/internal/config/config.go index cd5d1b00..6568d56f 100644 --- a/server/orders/internal/config/config.go +++ b/server/orders/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/orders/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/orders/internal/svc/servicecontext.go b/server/orders/internal/svc/servicecontext.go index 8516fd17..fec62caf 100644 --- a/server/orders/internal/svc/servicecontext.go +++ b/server/orders/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/pay/internal/config/config.go b/server/pay/internal/config/config.go index 6676984a..ac86efbc 100644 --- a/server/pay/internal/config/config.go +++ b/server/pay/internal/config/config.go @@ -10,6 +10,7 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 PayConfig struct { Stripe struct { EndpointSecret string diff --git a/server/pay/internal/svc/servicecontext.go b/server/pay/internal/svc/servicecontext.go index dab756c6..9e048ef0 100644 --- a/server/pay/internal/svc/servicecontext.go +++ b/server/pay/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/product-model/internal/config/config.go b/server/product-model/internal/config/config.go index 0904b683..3fb4f1d6 100644 --- a/server/product-model/internal/config/config.go +++ b/server/product-model/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/product-model/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/product-template-tag/internal/config/config.go b/server/product-template-tag/internal/config/config.go index b5a0124e..0fe5458e 100644 --- a/server/product-template-tag/internal/config/config.go +++ b/server/product-template-tag/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/product-template-tag/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/product-template-tag/internal/svc/servicecontext.go b/server/product-template-tag/internal/svc/servicecontext.go index 9225fd1a..64b7d6c8 100644 --- a/server/product-template-tag/internal/svc/servicecontext.go +++ b/server/product-template-tag/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/product-template/internal/config/config.go b/server/product-template/internal/config/config.go index cfb76dd9..c38520cc 100644 --- a/server/product-template/internal/config/config.go +++ b/server/product-template/internal/config/config.go @@ -2,6 +2,7 @@ package config import ( "fusenapi/server/product-template/internal/types" + "github.com/zeromicro/go-zero/rest" ) @@ -9,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/product/internal/config/config.go b/server/product/internal/config/config.go index 77259953..31e07893 100644 --- a/server/product/internal/config/config.go +++ b/server/product/internal/config/config.go @@ -10,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/product/internal/svc/servicecontext.go b/server/product/internal/svc/servicecontext.go index 4345d820..1815c519 100644 --- a/server/product/internal/svc/servicecontext.go +++ b/server/product/internal/svc/servicecontext.go @@ -16,7 +16,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen diff --git a/server/render/internal/config/config.go b/server/render/internal/config/config.go index 5f9c4816..417d0d15 100644 --- a/server/render/internal/config/config.go +++ b/server/render/internal/config/config.go @@ -10,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/render/internal/svc/servicecontext.go b/server/render/internal/svc/servicecontext.go index babd3d9c..1b66a911 100644 --- a/server/render/internal/svc/servicecontext.go +++ b/server/render/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/shopping-cart-confirmation/internal/config/config.go b/server/shopping-cart-confirmation/internal/config/config.go index 6ddec5d8..1f3f0d9e 100644 --- a/server/shopping-cart-confirmation/internal/config/config.go +++ b/server/shopping-cart-confirmation/internal/config/config.go @@ -10,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/shopping-cart-confirmation/internal/svc/servicecontext.go b/server/shopping-cart-confirmation/internal/svc/servicecontext.go index 58bf088f..251c4f47 100644 --- a/server/shopping-cart-confirmation/internal/svc/servicecontext.go +++ b/server/shopping-cart-confirmation/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/upload/internal/config/config.go b/server/upload/internal/config/config.go index a8d273d7..ffad08d3 100644 --- a/server/upload/internal/config/config.go +++ b/server/upload/internal/config/config.go @@ -10,6 +10,7 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 Env string AWS struct { S3 struct { diff --git a/server/upload/internal/svc/servicecontext.go b/server/upload/internal/svc/servicecontext.go index d3ba17da..5263c0af 100644 --- a/server/upload/internal/svc/servicecontext.go +++ b/server/upload/internal/svc/servicecontext.go @@ -19,7 +19,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen diff --git a/server/webset/internal/config/config.go b/server/webset/internal/config/config.go index 670228c0..b82e386d 100644 --- a/server/webset/internal/config/config.go +++ b/server/webset/internal/config/config.go @@ -10,4 +10,5 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 } diff --git a/server/webset/internal/svc/servicecontext.go b/server/webset/internal/svc/servicecontext.go index 560bbd95..142cdfef 100644 --- a/server/webset/internal/svc/servicecontext.go +++ b/server/webset/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -25,7 +25,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/websocket/internal/config/config.go b/server/websocket/internal/config/config.go index 321047cb..8315787e 100644 --- a/server/websocket/internal/config/config.go +++ b/server/websocket/internal/config/config.go @@ -10,5 +10,6 @@ type Config struct { rest.RestConf SourceMysql string Auth types.Auth + ReplicaId uint64 SourceRabbitMq string } diff --git a/server/websocket/internal/svc/servicecontext.go b/server/websocket/internal/svc/servicecontext.go index e35ed3da..153148f8 100644 --- a/server/websocket/internal/svc/servicecontext.go +++ b/server/websocket/internal/svc/servicecontext.go @@ -17,7 +17,7 @@ import ( type ServiceContext struct { Config config.Config - SharedState *fsm.FsStateMachine + SharedState *fsm.SharedState MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -26,7 +26,7 @@ type ServiceContext struct { func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) + StateServer := fsm.StartNode(c.ReplicaId, autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/utils/autoconfig/autoconfig.go b/utils/autoconfig/autoconfig.go index 7da11bf7..72442729 100644 --- a/utils/autoconfig/autoconfig.go +++ b/utils/autoconfig/autoconfig.go @@ -12,9 +12,10 @@ import ( ) type ConfigServer struct { - Name string - Host string `yaml:"Host"` - Port int `yaml:"Port"` + Name string + Host string `yaml:"Host"` + Port int `yaml:"Port"` + ReplicaId uint64 `yaml:"ReplicaId"` } func AutoGetAllServerConfig() []*ConfigServer { diff --git a/utils/basic/request_parse.go b/utils/basic/request_parse.go index 5aed52da..5c58877d 100644 --- a/utils/basic/request_parse.go +++ b/utils/basic/request_parse.go @@ -51,7 +51,7 @@ func NormalAfterLogic(w http.ResponseWriter, r *http.Request, resp *Response) { } } -func RequestParse(w http.ResponseWriter, r *http.Request, state *fsm.FsStateMachine, LogicRequest any) (*auth.UserInfo, error) { +func RequestParse(w http.ResponseWriter, r *http.Request, state *fsm.SharedState, LogicRequest any) (*auth.UserInfo, error) { token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息 if err != nil {