-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathregistry.go
122 lines (106 loc) · 3.15 KB
/
registry.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package core
import (
"bytes"
"strings"
)
const (
SCHEME_ZK = "zk://"
SCHEME_FILE = "file://" //直接连接
)
type ConfigCenter struct {
registry IRegistry
services []Service
hostport string
}
//用于创建
func NewConfigCenter(registryAddr,
hostport string, services []Service) *ConfigCenter {
uris := make([]string, 0, 10)
for _, s := range services {
uris = append(uris, BuildServiceUri(s.ServiceUri, s.GroupId))
}
var reg IRegistry
if strings.HasPrefix(registryAddr, SCHEME_ZK) {
reg = NewZkRegistry(strings.TrimPrefix(registryAddr, SCHEME_ZK), uris, true)
} else if strings.HasPrefix(registryAddr, SCHEME_FILE) {
//本地文件配置
reg = NewFileRegistry(strings.TrimPrefix(registryAddr, SCHEME_FILE), uris, true)
}
center := &ConfigCenter{registry: reg, services: services, hostport: hostport}
// zookeeper发布一次吧
center.RegisteAllServices()
return center
}
func (self *ConfigCenter) RegisteAllServices() {
//注册服务
for _, s := range self.services {
succ := self.RegisteService(s.ServiceUri, self.hostport, PROTOCOL, s.GroupId,
ServiceMeta{
ServiceUri: s.ServiceUri,
GroupId: s.GroupId,
IsPre: s.IsPre,
ProtoVersion: PROTOCOL,
HostPort: self.hostport,
})
if !succ {
panic("ConfigCenter|RegisteAllServices|FAIL|" + s.ServiceUri)
}
}
}
func (self *ConfigCenter) RegisteService(serviceUri, hostport, protoType, groupid string, s ServiceMeta) bool {
s.ServiceUri = serviceUri
s.HostPort = hostport
s.ProtoVersion = protoType
s.GroupId = groupid
return self.registry.RegisteService(serviceUri, hostport, protoType, groupid, s)
}
func (self *ConfigCenter) UnRegisteService(serviceUri, hostport, protoType, groupid string) bool {
return self.registry.UnRegisteService(serviceUri, hostport, protoType, groupid)
}
func (self *ConfigCenter) GetService(serviceUri, protoType string, groupid string) ([]ServiceMeta, error) {
return self.registry.GetService(serviceUri, protoType, groupid)
}
func (self *ConfigCenter) Destroy() {
//注册服务
for _, s := range self.services {
succ := self.UnRegisteService(s.ServiceUri, self.hostport, PROTOCOL, s.GroupId)
if succ {
log.Infof("ConfigCenter|Destroy|UnRegisteService|SUCC|%s", s.ServiceUri)
} else {
log.Infof("ConfigCenter|Destroy|UnRegisteService|FAIL|%s", s.ServiceUri)
}
}
self.registry.Destroy()
}
const (
// /moa/service/v1/service/relation-service#{groupId}/localhost:13000?timeout=1000&protocol=v1
ZK_MOA_ROOT_PATH = "/moa/service"
ZK_ROOT = "/"
ZK_PATH_DELIMITER = "/"
PROTOCOL = "v1"
REGISTRY_ZOOKEEPER = "zookeeper"
ALL_GROUP = "*"
)
// 拼接字符串
func concat(args ...string) string {
var buffer bytes.Buffer
for _, arg := range args {
buffer.WriteString(arg)
}
return buffer.String()
}
func BuildServiceUri(serviceUri, groupId string) string {
if len(groupId) > 0 && "*" != groupId {
return concat(serviceUri, "#", groupId)
} else {
return serviceUri
}
}
func UnwrapServiceUri(serviceUri string) (string, string) {
if strings.IndexAny(serviceUri, "#") >= 0 {
split := strings.SplitN(serviceUri, "#", 2)
return split[0], split[1]
} else {
return serviceUri, "*"
}
}