This is a creation in Article, where the information may have evolved or changed.
Source: Personal blog Address: http://www.damonyi.cc/?p=194
The project uses the ETCD database to store the container's information and the application's domain name information, and ETCD the Golang code of the operation.
1. Write container information to the specified directory
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 errorcommon. 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, read the ETCD cache data example, only get the non-directory information
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. Domainetcdfor _, 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. Containeretcdfor _, 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.valuereturn}for node: = Range ro otnode.nodes {Getallnode (Rootnode.nodes[node], Nodesinfo)}}
Data structure information with ETCD storage:
//the container info in etcdtype containeretcd struct {hostip stringhostport Int64domain stringcontainerid stringcontainerip stringcontainerport int64action string}//domain Info I n etcdtype domainetcd struct {Port int64host stringdomain stringaction string}type haproxyconfigration struct {DomainE TCD *domainetcdcontainers map[string]*containeretcd}type haproxytemplatecache struct {Data map[string]* Haproxyconfigrationlock sync. Rwmutex}
This article just wants to provide some code reference, the business content is not fine-spoken.