Etcd Use of small notes

Source: Internet
Author: User
Tags etcd
This is a creation in Article, where the information may have evolved or changed.

Let's not talk about installation, just use it.

Etcd,consul are distributed kv, which is commonly used for service discovery;

1 Registration Service

Here to use ETCD.CLIENTV3 to do things, Godoc here, here is the pseudo-code

// key 随你制定, 只要能标识是那个服务器(比如 root/game/node_1)即可. value 可以包含addr, 状态, 优先度等信息client.OpPut(key,value)

Here are the tips: it is best to achieve health checks, and then good code also have a day to run, as if ETCD's Health Check only support TTL (service to ETCD timing re-put), so remember to set the TTL and the whole tick timing to execute the above code.

But consul supports two kinds of health checks, one is TTL, the other is consul proactively check the status of the service (see the bottom of the details ↓ reference), the individual prefers consul. Because I prefer young (= infinitely possible) things, just like Golang.

Ttl?

// ttl: 10sresp, _:= cli.Grant(context.TODO(), 10)ctx, _:= context.WithTimeout(context.Background(), 5*time.Second)rsp,err:=cli.Put(ctx,"root/game/node-2",`{"addr":"192.168.1.1:9999"}`,clientv3.WithLease(resp.ID))

Now the root/game/node-2 will be automatically removed after 10s.

Oh, that's so easy.

2 Getting Services

You can see that the key above is used/layered, just like the file directory. But in fact this structure is not necessary, just recommend it ~.

Getting a value requires only:

rsp,_ := cli.Get(ctx,"root/game/node_1")log.Print(rsp)

So what do you do if you want to get all the kv under root? Such

// clientv3.WithPrefix() 这个option的意思就是以前缀获取,就是只要有这个前缀的key都返回. 现在明白了吧, 为什么推荐用/分割key, 是不是很优雅?// 将返回一个数组rsp,_ := cli.Get(ctx,"root/",clientv3.WithPrefix())log.Print(rsp)

Oh, that's so easy.

3 Watch

I just said that setting the TTL will delete the key after 10s, so OK to delete it? That's watch.

Watch's role is to inform each service of the corresponding logic (reconnect new services, alarms, etc.) after the new service changes (re-registration, downline, etc.).

ctx := context.TODO()ch := cli.Watch(ctx, "root/", clientv3.WithPrefix())for {    log.Print("rev")    select {    case c := <-ch:        for _, e := range c.Events {            log.Printf("%+v", e)        }    }}

The above code in the root/under the key changes will print the corresponding changes.

Consul I also useless, here to a link Consul API, Inside said also easy to understand, and so used to come to small remember bar ~ Finish.

Reference

Getting Started with Etcd
Using Consul to implement registration and discovery of services

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.