修改为龙舟

This commit is contained in:
eson 2023-07-31 00:41:04 +08:00
parent 7fd48edef8
commit fe51e85983
39 changed files with 81 additions and 133 deletions

4
.gitignore vendored
View File

@ -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
server/webset/webset
shared-state

View File

@ -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 {

View File

@ -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,

View File

@ -10,6 +10,7 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
MainAddress string

View File

@ -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,

View File

@ -10,4 +10,5 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
}

View File

@ -17,7 +17,7 @@ import (
type ServiceContext struct {
Config config.Config
SharedState *fsm.FsStateMachine
SharedState *fsm.SharedState
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -10,6 +10,7 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
MainAddress string

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -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,

View File

@ -10,6 +10,7 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
PayConfig struct {
Stripe struct {
EndpointSecret string

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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,

View File

@ -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
}

View File

@ -10,4 +10,5 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
}

View File

@ -16,7 +16,7 @@ import (
type ServiceContext struct {
Config config.Config
SharedState *fsm.FsStateMachine
SharedState *fsm.SharedState
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen

View File

@ -10,4 +10,5 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
}

View File

@ -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,

View File

@ -10,4 +10,5 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
}

View File

@ -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,

View File

@ -10,6 +10,7 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
Env string
AWS struct {
S3 struct {

View File

@ -19,7 +19,7 @@ import (
type ServiceContext struct {
Config config.Config
SharedState *fsm.FsStateMachine
SharedState *fsm.SharedState
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen

View File

@ -10,4 +10,5 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
}

View File

@ -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,

View File

@ -10,5 +10,6 @@ type Config struct {
rest.RestConf
SourceMysql string
Auth types.Auth
ReplicaId uint64
SourceRabbitMq string
}

View File

@ -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,

View File

@ -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 {

View File

@ -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 {