忽略掉后台

This commit is contained in:
eson 2023-07-26 10:52:33 +08:00
parent 852c5e4a94
commit 4c104b56ae
17 changed files with 97 additions and 70 deletions

View File

@ -5,6 +5,7 @@ import (
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"fusenapi/initalize" "fusenapi/initalize"
"fusenapi/utils/autoconfig"
"log" "log"
"net" "net"
"os" "os"
@ -17,7 +18,7 @@ import (
func test1() { func test1() {
log.SetFlags(log.Llongfile) 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) time.Sleep(time.Second * 5)
@ -31,7 +32,7 @@ func test1() {
} }
// StartNode 启动节点 // 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{ fsm := &StateCluster{
store: make(map[int64]*UserState), store: make(map[int64]*UserState),
@ -74,62 +75,80 @@ func StartNode(ServerID string, RaftBind string, gdb *gorm.DB) *StateCluster {
panic(err) panic(err)
} }
configuration := raft.Configuration{ var dup map[string]bool = make(map[string]bool)
Servers: []raft.Server{ var rserver []raft.Server = []raft.Server{
{ {
Suffrage: raft.Voter, Suffrage: raft.Voter,
ID: config.LocalID, ID: config.LocalID,
Address: transport.LocalAddr(), 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) fu := fsm.ra.BootstrapCluster(configuration)
if err := fu.Error(); err != nil { if err := fu.Error(); err != nil {
log.Println(err) log.Println(err)
} }
waitForLeader(fsm.ra) waitForCluster(fsm.ra)
return fsm 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() // configFuture := fsm.ra.GetConfiguration()
if err := configFuture.Error(); err != nil { // if err := configFuture.Error(); err != nil {
log.Fatalf("failed to get raft configuration: %v", err) // log.Fatalf("failed to get raft configuration: %v", err)
} // }
for _, srv := range configFuture.Configuration().Servers { // for _, srv := range configFuture.Configuration().Servers {
if srv.ID == raft.ServerID(ServerID) && srv.Address == raft.ServerAddress(LeaderAddress) { // if srv.ID == raft.ServerID(ServerID) && srv.Address == raft.ServerAddress(LeaderAddress) {
if future := fsm.ra.RemoveServer(srv.ID, 0, 0); future.Error() != nil { // if future := fsm.ra.RemoveServer(srv.ID, 0, 0); future.Error() != nil {
log.Fatalf("Error removing existing server [%s]: %v", ServerID, future.Error()) // log.Fatalf("Error removing existing server [%s]: %v", ServerID, future.Error())
} // }
} // }
} // }
f := fsm.ra.AddVoter(raft.ServerID(ServerID), raft.ServerAddress(RaftBind), 0, 0) // f := fsm.ra.AddVoter(raft.ServerID(ServerID), raft.ServerAddress(RaftBind), 0, 0)
if f.Error() != nil { // if f.Error() != nil {
log.Fatalf("Error adding voter: %v", f.Error()) // log.Fatalf("Error adding voter: %v", f.Error())
} // }
return fsm // return fsm
} // }
func waitForLeader(ra *raft.Raft) { func waitForCluster(ra *raft.Raft) {
leaderCh := ra.LeaderCh() ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for { for range ticker.C {
select { state := ra.State()
case isLeader := <-leaderCh: if state == raft.Leader || state == raft.Follower {
if isLeader { log.Println("Raft cluster is running")
return return
} } else {
case <-time.After(10 * time.Second): log.Println("Still waiting for the cluster to start...")
log.Println("Still waiting for the leader...")
} }
} }
} }

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"fusenapi/utils/autoconfig"
"fusenapi/initalize" "fusenapi/initalize"
"fusenapi/model/gmodel" "fusenapi/model/gmodel"
@ -16,21 +17,25 @@ import (
type ServiceContext struct { type ServiceContext struct {
Config {{.config}} Config {{.config}}
{{.middleware}} {{.middleware}}
SharedState *fsm.StateCluster
MysqlConn *gorm.DB MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen AllModels *gmodel.AllModelsGen
} }
func NewServiceContext(c {{.config}}) *ServiceContext { 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{ return &ServiceContext{
Config: c, Config: c,
MysqlConn: initalize.InitMysql(c.SourceMysql), MysqlConn: conn,
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), SharedState: StateServer,
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
{{.middlewareAssignment}} {{.middlewareAssignment}}
} }
} }
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization") AuthKey := r.Header.Get("Authorization")
if AuthKey == "" { if AuthKey == "" {

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/assistant/internal/config" "fusenapi/server/assistant/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/auth/internal/config" "fusenapi/server/auth/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/canteen/internal/config" "fusenapi/server/canteen/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/data-transfer/internal/config" "fusenapi/server/data-transfer/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -4,6 +4,7 @@ import (
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/initalize" "fusenapi/initalize"
"fusenapi/model/gmodel" "fusenapi/model/gmodel"
"fusenapi/utils/autoconfig"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"gorm.io/gorm" "gorm.io/gorm"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/inventory/internal/config" "fusenapi/server/inventory/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/map-library/internal/config" "fusenapi/server/map-library/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/orders/internal/config" "fusenapi/server/orders/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -3,7 +3,6 @@ package svc
import ( import (
"errors" "errors"
"fmt" "fmt"
"fusenapi/fsm"
"fusenapi/server/product-model/internal/config" "fusenapi/server/product-model/internal/config"
"net/http" "net/http"
@ -15,8 +14,7 @@ import (
) )
type ServiceContext struct { type ServiceContext struct {
Config config.Config Config config.Config
SharedState *fsm.StateCluster
MysqlConn *gorm.DB MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen AllModels *gmodel.AllModelsGen
@ -24,13 +22,11 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) conn := initalize.InitMysql(c.SourceMysql)
StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), conn)
return &ServiceContext{ return &ServiceContext{
Config: c, Config: c,
MysqlConn: conn, MysqlConn: conn,
SharedState: StateServer, AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
} }
} }

View File

@ -3,7 +3,6 @@ package svc
import ( import (
"errors" "errors"
"fmt" "fmt"
"fusenapi/fsm"
"fusenapi/server/product-template/internal/config" "fusenapi/server/product-template/internal/config"
"net/http" "net/http"
@ -15,8 +14,7 @@ import (
) )
type ServiceContext struct { type ServiceContext struct {
Config config.Config Config config.Config
SharedState *fsm.StateCluster
MysqlConn *gorm.DB MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen AllModels *gmodel.AllModelsGen
@ -24,13 +22,11 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) conn := initalize.InitMysql(c.SourceMysql)
StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), conn)
return &ServiceContext{ return &ServiceContext{
Config: c, Config: c,
MysqlConn: conn, MysqlConn: conn,
SharedState: StateServer, AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
} }
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/render/internal/config" "fusenapi/server/render/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/shopping-cart-confirmation/internal/config" "fusenapi/server/shopping-cart-confirmation/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fusenapi/fsm" "fusenapi/fsm"
"fusenapi/server/webset/internal/config" "fusenapi/server/webset/internal/config"
"fusenapi/utils/autoconfig"
"net/http" "net/http"
"fusenapi/initalize" "fusenapi/initalize"
@ -24,7 +25,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext { func NewServiceContext(c config.Config) *ServiceContext {
conn := initalize.InitMysql(c.SourceMysql) 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{ return &ServiceContext{
Config: c, Config: c,