忽略掉后台
This commit is contained in:
parent
852c5e4a94
commit
4c104b56ae
97
fsm/main.go
97
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...")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 == "" {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user