go操作etcd
what
distributed key-value store:
- strongly consistently
- use raft
vs redis:
same: key value store
differ:
- 使用场景:
- redis: 存储业务数据,快速
- etcd: 存储关键数据, 可靠
arhitecture:

componetnt:
- grpc server
- raft algorithm
- store:
- wal
- boltdbd: key value storeage
basic operation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#set
etcdctl put a b
# get
etcdclt get a
etcdctl get "" --prefix // get all keys
// read old version
etcd get a --rev=5 // the max revision is 5;
# expired time
etcdctl lease grant 100
etcdctl lease keep-alive leaseid
etcdctl put a b --lease leaseid
# del
etcdctl del a
|
service discovery
how:
- register: put and keepalive with ticker
- unregister: delete key
- client: getService:
- getKeyWithPrefix
- watch with prefix
1
2
3
4
5
6
7
8
|
etcdctl lease grant 20
etcdctl put /services/shoping/192 192 --lease xxxxx
etcdctl lease keep-alive xxxxxx
etcdctl get --prefix /services/shopping --> add local serviceList
etcdctl watch --prefix /services/shopping:
if delete, remove from local list
if put, addd to local luis
|
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
|
func Register?(){
func updateIP (){
r.etcd3Client.Put(ctx, key, value, etcd3.WithLease(resp.ID));
}
c = time.NewTicker
for {
case <-canclel
return
case <-c:
}
}
func UNRegister?(){
etcdClient.Revoke(lease)
//or delete
r.client.Delete(....)
}
func GetServuces(){
}
func Watch(){
c := watchWithPreifix
select {
case PUT:
case Del:
}
}
|