Join集群
This commit is contained in:
parent
7200531c27
commit
38375ffdda
25
fsm/main.go
25
fsm/main.go
|
@ -94,6 +94,31 @@ func StartNode(ServerID string, RaftBind string, gdb *gorm.DB) *StateCluster {
|
||||||
return fsm
|
return fsm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func JoinCluster(ServerID string, LeaderAddress string, RaftBind string, gdb *gorm.DB) *StateCluster {
|
||||||
|
|
||||||
|
fsm := StartNode(ServerID, RaftBind, gdb)
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func waitForLeader(ra *raft.Raft) {
|
func waitForLeader(ra *raft.Raft) {
|
||||||
leaderCh := ra.LeaderCh()
|
leaderCh := ra.LeaderCh()
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,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("localhost:%d", c.Port-2001), conn)
|
||||||
|
|
||||||
return &ServiceContext{
|
return &ServiceContext{
|
||||||
Config: c,
|
Config: c,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user