forked from viphxin/xingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterconf.go
83 lines (75 loc) · 1.54 KB
/
clusterconf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cluster
import (
"encoding/json"
"errors"
"io/ioutil"
"github.com/viphxin/xingo/logger"
)
var cfgpath string
type ClusterServerConf struct {
Name string
Host string
RootPort int
Http []interface{} //[port, staticfile_path]
Https []interface{} //[port, certFile, keyFile, staticfile_path]
NetPort int
DebugPort int //telnet port
Remotes []string
Module string
Log string
}
type ClusterConf struct {
Master *ClusterServerConf
Servers map[string]*ClusterServerConf
}
func NewClusterConf(path string) (*ClusterConf, error) {
cconf := &ClusterConf{}
//集群服务器配置信息
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
err = json.Unmarshal(data, cconf)
if err != nil {
panic(err)
}
cfgpath = path
return cconf, nil
}
/*
获取当前节点的父节点
*/
func (this *ClusterConf) GetRemotesByName(name string) ([]string, error) {
server, ok := this.Servers[name]
if ok {
return server.Remotes, nil
} else {
return nil, errors.New("no server found!!!")
}
}
/*
获取当前节点的子节点
*/
func (this *ClusterConf) GetChildsByName(name string) []string {
names := make([]string, 0)
for sername, ser := range this.Servers {
for _, rname := range ser.Remotes {
if rname == name {
names = append(names, sername)
break
}
}
}
return names
}
func (this *ClusterConf)Reload(){
//集群服务器配置信息
data, err := ioutil.ReadFile(cfgpath)
if err != nil {
logger.Error(err)
}
err = json.Unmarshal(data, this)
if err != nil {
logger.Error(err)
}
}