-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
53 lines (48 loc) · 1011 Bytes
/
init.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
package registry
import (
"github.com/go-zk/zk"
"sync"
"time"
)
//初始化
func (z *ZkRegistry) init() {
z.connect()
//初始化根路径
z.ensureRoot()
//watch 监听服务
z.watchListens()
}
//zk链接
func (z *ZkRegistry) connect() {
conn, _, err := zk.Connect(z.opts.hosts, time.Duration(z.opts.timeout)*time.Second)
if err != nil {
panic(err.Error())
}
z.conn = conn
}
//初始化根路径
func (z *ZkRegistry) ensureRoot() error {
exists, _, err := z.conn.Exists(z.opts.prefix)
if err != nil {
return err
}
if !exists {
_, err := z.conn.Create(z.opts.prefix, []byte(""), 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
return err
}
}
return nil
}
//watch 所有监听服务首次监听需阻塞
func (z *ZkRegistry) watchListens() {
if len(z.opts.listens) == 0 {
return //无配置监听服务不执行
}
var wg = sync.WaitGroup{}
for _, serviceName := range z.opts.listens {
wg.Add(1)
go z.watchNode(serviceName, &wg)
}
wg.Wait()
}