Golang Read write to ETCD database

Source: Internet
Author: User
Tags etcd
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.