Golang 读取写入Etcd数据库

           项目中用到Etcd数据库来存储容器的信息和应用的域名信息,将操作Etcd的golang代码整理了一下

1、将Container信息写入到指定目录

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
c, err := common.GetEtcdClient()
if err != nil {
beego.Error("get etcd client failed")
return
}
kapi := client.NewKeysAPI(c)
key := getSkyDnsDomain(domainEtcd.Domain)
value, \_ := json.Marshal(domainEtcd)
var etcderr error
common.HaproxyTemplateCache.Lock.Lock()
defer common.HaproxyTemplateCache.Lock.Unlock()

switch domainEtcd.Action {
case "add":
\_, etcderr = kapi.Create(context.Background(), key, string(value))
common.HaproxyTemplateCache.Data\[domainEtcd.Domain\] = &models.HaproxyConfigration{
DomainEtcd: domainEtcd,
}
case "delete":
\_, etcderr = kapi.Delete(context.Background(), key, &client.DeleteOptions{})
delete(common.HaproxyTemplateCache.Data, domainEtcd.Domain)
}
if etcderr != nil {
beego.Error("updatecontainer event erro", etcderr)
}

2、读取Etcd的缓存数据 example,只获取其中的非目录信息

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
func loadHaproxyTemplateCache() {
HaproxyTemplateCache.Lock.Lock()
defer HaproxyTemplateCache.Lock.Unlock()
HaproxyTemplateCache.Data = make(map\[string\]\*models.HaproxyConfigration)
client1, \_ := GetEtcdClient()
api := client.NewKeysAPI(client1)
/\*set skydns domain info\*/
res, err1 := api.Get(context.Background(), "/skydns/local", &client.GetOptions{Recursive: true})
if err1 != nil {
beego.Error("get /dockerstack info failed")
return
}

skydnsNodesInfo := make(map\[string\]string)
getAllNode(res.Node, skydnsNodesInfo)
var domain models.DomainEtcd
for \_, domainStr := range skydnsNodesInfo {
json.Unmarshal(\[\]byte(domainStr), &domain)
HaproxyTemplateCache.Data\[domain.Domain\].DomainEtcd = &domain
}
/\*set dockerstack container info\*/
res, err1 = api.Get(context.Background(), "/dockerstack", &client.GetOptions{Recursive: true})
if err1 != nil {
beego.Error("get /dockerstack info failed")
return
}
dockerstackNodesInfo := make(map\[string\]string)
getAllNode(res.Node, dockerstackNodesInfo)
var container models.ContainerEtcd
for \_, containerStr := range skydnsNodesInfo {
json.Unmarshal(\[\]byte(containerStr), &container)
HaproxyTemplateCache.Data\[domain.Domain\].Containers\[container.ContainerId\] = &container
}
}
func getAllNode(rootNode \*client.Node, nodesInfo map\[string\]string) {
if !rootNode.Dir {
nodesInfo\[rootNode.Key\] = rootNode.Value
return
}
for node := range rootNode.Nodes {
getAllNode(rootNode.Nodes\[node\], nodesInfo)
}
}

附 etcd存储的数据结构信息:

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
//the container info in etcd
type ContainerEtcd struct {
HostIp string
HostPort int64
Domain string
ContainerId string
ContainerIp string
ContainerPort int64
Action string
}

//domain info in etcd
type DomainEtcd struct {
Port int64
Host string
Domain string
Action string
}

type HaproxyConfigration struct {
DomainEtcd \*DomainEtcd
Containers map\[string\]\*ContainerEtcd
}
type HaproxyTemplateCache struct {
Data map\[string\]\*HaproxyConfigration
Lock sync.RWMutex
}

本文只是想提供一些代码参考,业务内容就不细讲了。。