Consul Service registration and service discovery of the giant pits

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

The most recent use of consul service registration as a project and the underlying functionality of service discovery. In the tower building cluster use encountered some pits, the following a record down.

Consul cluster Multiple node

The consul cluster node is what we call the Consul instance. The cluster consists of multiple node, which requires more than half of node-enabled servers for cluster availability. As recommended in 5 node 3 Enable server mode, 3 node-composed cluster has 2 node-enabled server mode.
You must think there is no problem when you see it, but the consul pit is more. Join your cluster consisting of the following:

Node          Address              Status  Type    Build  Protocol  DC                    SegmentBJ-MQTEST-01  10.163.145.117:8301  alive   server  1.0.6  2         iget-topology-aliyun  <all>BJ-MQTEST-02  10.163.147.47:8301   alive   server  1.0.6  2         iget-topology-aliyun  <all>BJ-TGO-01     10.163.145.110:8301     alive   client  1.0.6  2         iget-topology-aliyun  <default>

Then the client can connect to the consul cluster using the 3 IP above, assuming that the client A using 10.163.145.117 to register the service, restart after the use of address 10.163.145.110 registration before the service information, you will be pleasantly surprised to find that the UI can see at the same time there are two identical serviceid under the same servicename.

This is the consul cluster multiple node pit, because the service base, although the use of KV storage, but the service key is not related to Serviceid, so in the cluster can be repeated.

Solution One

Only one node in the cluster uses server mode, and the other is client mode. The disadvantage is that if the server node is dead, then the availability of the cluster is gone.

Solution II

The same client uses the same node address, which makes sure that there are no two identical serviceid under the same servicename. The disadvantage is that if the client-bound node is hung, then clients use it.
The code gives

Package Registryimport ("FMT" "Math" "net" "Sort" "strings" Log "Github.com/golang/glog") type Consulbin d struct {Addr string ipint float64}type consulbindlist []consulbindfunc (S Consulbindlist) Len () int {return    Len (s)}func (S consulbindlist) Swap (i, J int) {S[i], s[j] = S[j], S[i]}func (s consulbindlist) Less (i, J int) bool { Return s[i]. Ipint < S[j].        Ipint}func (S Consulbindlist) tostrings () []string {ret: = make ([]string, 0, Len (s)) for _, CBL: = Range S { ret = append (ret, CBL. ADDR)} return Ret}func Bingconsulsort (Consuladdrs []string) []string {localipstr, err: = Getagentlocalip () i F Err! = Nil {return Consuladdrs} localip: = Net. Parseip (localipstr) Localipint: = Int64 (0) if localip! = nil {localipint = util.        Inetaton (Localip)} addrslist: = make ([]consulbind, 0, Len (Consuladdrs)) for _, Addr: = Range Consuladdrs { Ads: = strings. Split (addr, ":") If Len (ads) = = 2 {IP: = net. Parseip (ads[0]) if IP! = Nil {ipint: = util. Inetaton (IP) fmt.                    Println ("IP:", IP, Ipint, Localipint, (ipint-localipint)) Addrslist = append (Addrslist, consulbind{ Addr:addr, Ipint:math. Abs (Float64 (Ipint-localipint),})}}} Consulbindlist: = Consulbindlist (Addrsli ST) sort. Sort (consulbindlist) log.infof ("Sort Addrs%v", consulbindlist) return consulbindlist.tostrings ()}

Solution Three

The client randomly uses any one of the addresses in the cluster, but before registering it, it is determined whether the servicename already exists for the Serviceid to be registered, and if so, to remove the re-registration. The downside is that watch has more events that can be upgraded to not allow duplicate registrations if they are present and healthy, and that's what I'm using.

Delete Service

At first many people feel that the service is out of the question, the next one hangs, then it will be moved out. But deleting the service in Consul is not that easy!
Please check the official documents:
Catalog Document

Agent/service Documentation

Look at the seemingly optional one can do the correct removal of service! Can continue to say, not so simple, consul pit is more.

With the /agent/service/deregister/:service_id interface selected, you will not be able to delete other node's service. For example, there is a serviceid in 10.163.145.117 agent_xxxx_v1 , but the client connection consul uses an IP of 10.163.145.110, then it cannot be deleted agent_xxxx_v1 .

It's okay. Is there an interface that is not used? Look at the /catalog/deregister UI after the execution is done, and um, yes, it did agent_xxxx_v1 . Wait a minute...... 30s found agent_xxxx_v1 again, what is the matter????

Please see Consul's bugunable to deregister a service #1188.

Solution Solutions

The first step: to query out the list of servicename owned by Serviceid;
Step Two: Iterate through the list to get to node's address and delete all Serviceid;

If Len (C.options.addrs) > 0 {addrmap: = Make (Map[string]string, Len (C.options.addrs)) for _, Host: = Ran GE C.options.addrs {addr, _, Err: = Net. Splithostport (host) if err! = Nil {log. WARNINGF ("%v is Err=%v", host, Err) continue} ADDRMAP[ADDR] = host} r SP, _, _: = C.client.health (). Service (S.name, "", false, nil) for _, SRSP: = range RSP {if srsp. Service.id = = ServiceId {if host, OK: = Addrmap[srsp. Node.address]; Ok {config: = Consul. Defaultnonpooledconfig () config. Address = host//Create Consul Connection client, err: = Consul. Newclient (config) if err! = Nil {log. WARNINGF ("Newclient is Err=%v", host, err)} err = client. Agent (). Servicederegister (serviceId) log.infof ("ServicederegistEr host=%v, serviceid=%v ", Host, ServiceId)}}}} else {err = C.client.agen T (). Servicederegister (serviceId) log.infof ("Servicederegister serviceid=%v", ServiceId)}

The

can be sure that there are other pits in consul, but these two pits have impressed me and recorded them as a reminder to the students who are ready to use Consul or have met these pits.

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.