| | |
| | | "github.com/hashicorp/memberlist" |
| | | "io/ioutil" |
| | | "os" |
| | | "strconv" |
| | | |
| | | //"os" |
| | | "strings" |
| | | "time" |
| | | |
| | | "github.com/hashicorp/serf/cmd/serf/command/agent" |
| | | "github.com/hashicorp/serf/serf" |
| | | //"github.com/apache/servicecomb-service-center/pkg/log" |
| | | "log" |
| | | ) |
| | | |
| | |
| | | return |
| | | } |
| | | |
| | | |
| | | |
| | | //GetDbFromCluster get the newest database after join cluster |
| | | //dbPathWrite the path where to write after got a database, |
| | | func (a *Agent)GetDbFromCluster(dbPathWrite string) { |
| | |
| | | fmt.Println("x length is: ", len(r.Payload)) |
| | | |
| | | // // byte to file. |
| | | Dbconn.Close() |
| | | Dbconn = nil |
| | | SerfDbConn.Close() |
| | | SerfDbConn = nil |
| | | err = ioutil.WriteFile(dbPathWrite, r.Payload, 0644) |
| | | if err != nil { |
| | | fmt.Println("query byte to file error!", err) |
| | | } |
| | | err := GetConn() |
| | | err := InitDbConn("") |
| | | if err != nil { |
| | | fmt.Println("create db conn of test.db error: ", err) |
| | | } |
| | |
| | | } |
| | | |
| | | //Init serf Init |
| | | //web后台收到创建集群的请求, |
| | | func Init(clusterID string, password string, nodeID string) (*Agent, error) { |
| | | func Init(clusterID string, password string, nodeID string, ips []string) (*Agent, error) { |
| | | agent, err := InitNode(clusterID, password, nodeID) |
| | | if err != nil { |
| | | fmt.Printf("InitNode failed, error: %s", err) |
| | | return agent, err |
| | | } |
| | | |
| | | err = agent.JoinByNodeIP(ips) |
| | | if err != nil { |
| | | fmt.Printf("JoinByNodeIP failed, error: %s", err) |
| | | return agent, err |
| | | } |
| | | |
| | | return agent, err |
| | | } |
| | | |
| | | //InitNode web后台收到创建集群的请求, |
| | | func InitNode(clusterID string, password string, nodeID string) (*Agent, error) { |
| | | conf := DefaultConfig() |
| | | fmt.Println("clusterID:", clusterID, "password:", password, "nodeID:", nodeID) |
| | | //conf.ClusterID = clusterID |
| | |
| | | time.Sleep(time.Second) |
| | | fmt.Println("Stats:",agent.Agent.Serf().Stats()) |
| | | fmt.Println("EncryptionEnabled:",agent.Agent.Serf().EncryptionEnabled()) |
| | | fmt.Printf("create agent sucess!!") |
| | | fmt.Println("create agent sucess!!") |
| | | |
| | | return agent, nil |
| | | } |
| | | |
| | | func (a *Agent) JoinByNodeIP(ip string) error { |
| | | n, err := a.Agent.Join([]string{ip + ":" + strconv.Itoa(DefaultBindPort)}, true) |
| | | func (a *Agent) JoinByNodeIP(ips []string) error { |
| | | var nodes []string |
| | | fmt.Println("len(ips):", len(ips)) |
| | | for _, ip := range ips { |
| | | node := fmt.Sprintf("%s:%d", ip, DefaultBindPort) |
| | | nodes = append(nodes, node) |
| | | } |
| | | |
| | | n, err := a.Agent.Join(nodes, true) |
| | | if err != nil || n == 0{ |
| | | a.Stop() |
| | | fmt.Println("Stop node") |
| | | return fmt.Errorf("Error Encrypt Key!") |
| | | return fmt.Errorf("Error Agent.Join!") |
| | | } |
| | | |
| | | return err |
| | |
| | | |
| | | return nodes |
| | | } |
| | | |
| | | |
| | | |
| | | |