diff --git a/fsm/main.go b/fsm/main.go index fe3e87c1..67201995 100644 --- a/fsm/main.go +++ b/fsm/main.go @@ -5,6 +5,7 @@ import ( "encoding/gob" "fmt" "fusenapi/initalize" + "fusenapi/utils/autoconfig" "log" "net" "os" @@ -17,7 +18,7 @@ import ( func test1() { log.SetFlags(log.Llongfile) - fsm := StartNode("fs1", "localhost:5500", initalize.InitMysql("fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest")) + fsm := StartNode("fs1", "localhost:5500", nil, initalize.InitMysql("fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest")) time.Sleep(time.Second * 5) @@ -31,7 +32,7 @@ func test1() { } // StartNode 启动节点 -func StartNode(ServerID string, RaftBind string, gdb *gorm.DB) *StateCluster { +func StartNode(ServerID string, RaftBind string, serverconfigs []*autoconfig.ConfigServer, gdb *gorm.DB) *StateCluster { fsm := &StateCluster{ store: make(map[int64]*UserState), @@ -74,62 +75,80 @@ func StartNode(ServerID string, RaftBind string, gdb *gorm.DB) *StateCluster { panic(err) } - configuration := raft.Configuration{ - Servers: []raft.Server{ - { - Suffrage: raft.Voter, - ID: config.LocalID, - Address: transport.LocalAddr(), - }, + 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) } - waitForLeader(fsm.ra) + waitForCluster(fsm.ra) return fsm } -func JoinCluster(ServerID string, LeaderAddress string, RaftBind string, gdb *gorm.DB) *StateCluster { +// func JoinCluster(ServerID string, LeaderAddress string, RaftBind string, gdb *gorm.DB) *StateCluster { - fsm := StartNode(ServerID, RaftBind, gdb) +// fsm := StartNode(ServerID, RaftBind, gdb) - configFuture := fsm.ra.GetConfiguration() - if err := configFuture.Error(); err != nil { - log.Fatalf("failed to get raft configuration: %v", err) - } +// configFuture := fsm.ra.GetConfiguration() +// if err := configFuture.Error(); err != nil { +// log.Fatalf("failed to get raft configuration: %v", err) +// } - for _, srv := range configFuture.Configuration().Servers { - if srv.ID == raft.ServerID(ServerID) && srv.Address == raft.ServerAddress(LeaderAddress) { - if future := fsm.ra.RemoveServer(srv.ID, 0, 0); future.Error() != nil { - log.Fatalf("Error removing existing server [%s]: %v", ServerID, future.Error()) - } - } - } +// for _, srv := range configFuture.Configuration().Servers { +// if srv.ID == raft.ServerID(ServerID) && srv.Address == raft.ServerAddress(LeaderAddress) { +// if future := fsm.ra.RemoveServer(srv.ID, 0, 0); future.Error() != nil { +// log.Fatalf("Error removing existing server [%s]: %v", ServerID, future.Error()) +// } +// } +// } - f := fsm.ra.AddVoter(raft.ServerID(ServerID), raft.ServerAddress(RaftBind), 0, 0) - if f.Error() != nil { - log.Fatalf("Error adding voter: %v", f.Error()) - } +// f := fsm.ra.AddVoter(raft.ServerID(ServerID), raft.ServerAddress(RaftBind), 0, 0) +// if f.Error() != nil { +// log.Fatalf("Error adding voter: %v", f.Error()) +// } - return fsm -} +// return fsm +// } -func waitForLeader(ra *raft.Raft) { - leaderCh := ra.LeaderCh() +func waitForCluster(ra *raft.Raft) { + ticker := time.NewTicker(500 * time.Millisecond) + defer ticker.Stop() - for { - select { - case isLeader := <-leaderCh: - if isLeader { - return - } - case <-time.After(10 * time.Second): - log.Println("Still waiting for the leader...") + for range ticker.C { + state := ra.State() + if state == raft.Leader || state == raft.Follower { + log.Println("Raft cluster is running") + return + } else { + log.Println("Still waiting for the cluster to start...") } } } diff --git a/goctl_template/api/context.tpl b/goctl_template/api/context.tpl index 3195fe63..233d2023 100644 --- a/goctl_template/api/context.tpl +++ b/goctl_template/api/context.tpl @@ -6,6 +6,7 @@ import ( "fmt" "net/http" + "fusenapi/utils/autoconfig" "fusenapi/initalize" "fusenapi/model/gmodel" @@ -16,21 +17,25 @@ import ( type ServiceContext struct { Config {{.config}} {{.middleware}} + + SharedState *fsm.StateCluster MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen } 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) return &ServiceContext{ - Config: c, - MysqlConn: initalize.InitMysql(c.SourceMysql), - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + Config: c, + MysqlConn: conn, + SharedState: StateServer, + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), {{.middlewareAssignment}} } } - func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { AuthKey := r.Header.Get("Authorization") if AuthKey == "" { diff --git a/server/assistant/internal/svc/servicecontext.go b/server/assistant/internal/svc/servicecontext.go index 17c64d87..26f65471 100644 --- a/server/assistant/internal/svc/servicecontext.go +++ b/server/assistant/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/assistant/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/auth/internal/svc/servicecontext.go b/server/auth/internal/svc/servicecontext.go index afaf5b3a..ebb0ff05 100644 --- a/server/auth/internal/svc/servicecontext.go +++ b/server/auth/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/auth/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,7 +25,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-2001), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("localhost:%d", c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/canteen/internal/svc/servicecontext.go b/server/canteen/internal/svc/servicecontext.go index f5180da7..12c24336 100644 --- a/server/canteen/internal/svc/servicecontext.go +++ b/server/canteen/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/canteen/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/data-transfer/internal/svc/servicecontext.go b/server/data-transfer/internal/svc/servicecontext.go index c5d9f327..1a63a4a7 100644 --- a/server/data-transfer/internal/svc/servicecontext.go +++ b/server/data-transfer/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/data-transfer/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/home-user-auth/internal/svc/servicecontext.go b/server/home-user-auth/internal/svc/servicecontext.go index 1c018b96..05d4a26b 100644 --- a/server/home-user-auth/internal/svc/servicecontext.go +++ b/server/home-user-auth/internal/svc/servicecontext.go @@ -4,6 +4,7 @@ import ( "fusenapi/fsm" "fusenapi/initalize" "fusenapi/model/gmodel" + "fusenapi/utils/autoconfig" "github.com/golang-jwt/jwt" "gorm.io/gorm" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/inventory/internal/svc/servicecontext.go b/server/inventory/internal/svc/servicecontext.go index 6bbe0314..d68ecbe3 100644 --- a/server/inventory/internal/svc/servicecontext.go +++ b/server/inventory/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/inventory/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/map-library/internal/svc/servicecontext.go b/server/map-library/internal/svc/servicecontext.go index 6f203d42..55ef2c92 100644 --- a/server/map-library/internal/svc/servicecontext.go +++ b/server/map-library/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/map-library/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/orders/internal/svc/servicecontext.go b/server/orders/internal/svc/servicecontext.go index 9b572078..887589ed 100644 --- a/server/orders/internal/svc/servicecontext.go +++ b/server/orders/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/orders/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/product-model/internal/svc/servicecontext.go b/server/product-model/internal/svc/servicecontext.go index 55ed900c..e36305be 100644 --- a/server/product-model/internal/svc/servicecontext.go +++ b/server/product-model/internal/svc/servicecontext.go @@ -3,7 +3,6 @@ package svc import ( "errors" "fmt" - "fusenapi/fsm" "fusenapi/server/product-model/internal/config" "net/http" @@ -15,8 +14,7 @@ import ( ) type ServiceContext struct { - Config config.Config - SharedState *fsm.StateCluster + Config config.Config MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -24,13 +22,11 @@ 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), conn) return &ServiceContext{ - Config: c, - MysqlConn: conn, - SharedState: StateServer, - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + Config: c, + MysqlConn: conn, + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), } } diff --git a/server/product-template/internal/svc/servicecontext.go b/server/product-template/internal/svc/servicecontext.go index b5aacb4f..c7fed3b1 100644 --- a/server/product-template/internal/svc/servicecontext.go +++ b/server/product-template/internal/svc/servicecontext.go @@ -3,7 +3,6 @@ package svc import ( "errors" "fmt" - "fusenapi/fsm" "fusenapi/server/product-template/internal/config" "net/http" @@ -15,8 +14,7 @@ import ( ) type ServiceContext struct { - Config config.Config - SharedState *fsm.StateCluster + Config config.Config MysqlConn *gorm.DB AllModels *gmodel.AllModelsGen @@ -24,13 +22,11 @@ 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), conn) return &ServiceContext{ - Config: c, - MysqlConn: conn, - SharedState: StateServer, - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + Config: c, + MysqlConn: conn, + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), } } diff --git a/server/render/internal/svc/servicecontext.go b/server/render/internal/svc/servicecontext.go index 8e28800a..6de6346c 100644 --- a/server/render/internal/svc/servicecontext.go +++ b/server/render/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/render/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/shopping-cart-confirmation/internal/svc/servicecontext.go b/server/shopping-cart-confirmation/internal/svc/servicecontext.go index 17327dad..c5011454 100644 --- a/server/shopping-cart-confirmation/internal/svc/servicecontext.go +++ b/server/shopping-cart-confirmation/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/shopping-cart-confirmation/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/server/webset/internal/svc/servicecontext.go b/server/webset/internal/svc/servicecontext.go index f3c6a6bf..2a978844 100644 --- a/server/webset/internal/svc/servicecontext.go +++ b/server/webset/internal/svc/servicecontext.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/fsm" "fusenapi/server/webset/internal/config" + "fusenapi/utils/autoconfig" "net/http" "fusenapi/initalize" @@ -24,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), conn) + StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) return &ServiceContext{ Config: c, diff --git a/utils/auto_config/auto_config.go b/utils/autoconfig/autoconfig.go similarity index 100% rename from utils/auto_config/auto_config.go rename to utils/autoconfig/autoconfig.go diff --git a/utils/auto_config/auto_config_test.go b/utils/autoconfig/autoconfig_test.go similarity index 100% rename from utils/auto_config/auto_config_test.go rename to utils/autoconfig/autoconfig_test.go