-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclientDis.go
104 lines (89 loc) · 2.16 KB
/
clientDis.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
package main
import (
"go.etcd.io/etcd/clientv3"
"time"
"context"
"go.etcd.io/etcd/mvcc/mvccpb"
"sync"
"log"
)
type ClientDis struct {
client *clientv3.Client
serverList map[string]string
lock sync.Mutex
}
func NewClientDis (addr []string)( *ClientDis, error){
conf := clientv3.Config{
Endpoints: addr,
DialTimeout: 5 * time.Second,
}
if client, err := clientv3.New(conf); err == nil {
return &ClientDis{
client:client,
serverList:make(map[string]string),
}, nil
} else {
return nil ,err
}
}
func (this * ClientDis) GetService(prefix string) ([]string ,error){
resp, err := this.client.Get(context.Background(), prefix, clientv3.WithPrefix())
if err != nil {
return nil, err
}
addrs := this.extractAddrs(resp)
go this.watcher(prefix)
return addrs ,nil
}
func (this *ClientDis) watcher(prefix string) {
rch := this.client.Watch(context.Background(), prefix, clientv3.WithPrefix())
for wresp := range rch {
for _, ev := range wresp.Events {
switch ev.Type {
case mvccpb.PUT:
this.SetServiceList(string(ev.Kv.Key),string(ev.Kv.Value))
case mvccpb.DELETE:
this.DelServiceList(string(ev.Kv.Key))
}
}
}
}
func (this *ClientDis) extractAddrs(resp *clientv3.GetResponse) []string {
addrs := make([]string,0)
if resp == nil || resp.Kvs == nil {
return addrs
}
for i := range resp.Kvs {
if v := resp.Kvs[i].Value; v != nil {
this.SetServiceList(string(resp.Kvs[i].Key),string(resp.Kvs[i].Value))
addrs = append(addrs, string(v))
}
}
return addrs
}
func (this *ClientDis) SetServiceList(key,val string) {
this.lock.Lock()
defer this.lock.Unlock()
this.serverList[key] = string(val)
log.Println("set data key :",key,"val:",val)
}
func (this *ClientDis) DelServiceList(key string) {
this.lock.Lock()
defer this.lock.Unlock()
delete(this.serverList,key)
log.Println("del data key:", key)
}
func (this *ClientDis) SerList2Array()[]string {
this.lock.Lock()
defer this.lock.Unlock()
addrs := make([]string,0)
for _, v := range this.serverList {
addrs = append(addrs,v)
}
return addrs
}
func main () {
cli,_ := NewClientDis([]string{"127.0.0.1:2379"})
cli.GetService("/node")
select {}
}