GRPC (4): Use Golang to invoke Consul API interface, register USER-TOMCAT service

Source: Internet
Author: User

1, about consul

Dubbo's Registry is Zookeeper,redis.
Motan's registry is Zookeeper,consul.
Kubernetes's registry is ETCD.
The advantage of using Consul is that the service finds everything that supports it.
You can use a domain name for load balancing.
It's also a nice server-side Discovery Pattern.

2, start the consul service, call the interface

First, install a consul service on the server:
http://blog.csdn.net/freewebsys/article/details/56296013
Then download the Go client.
Go get Github.com/hashicorp/consul
You can then use the Consul API service.

Package Srcimport ("FMT"Consulapi"Github.com/hashicorp/consul/api"    "Log"    "Testing") Const ID ="1234567890"Func Testregister (t *testing. T) {FMT. Println("test begin.") Config: = Consulapi. Defaultconfig()//config. Address="localhost"Fmt. Println("defautl config:", config) client, err: = Consulapi. Newclient(config) if err! = Nil {log. Fatal("Consul Client Error:", err)}//Create a new service. Registration: = new (Consulapi. Agentserviceregistration) Registration. ID= Id Registration. Name="User-tomcat"Registration. Port=8080Registration. Tags= []string{"User-tomcat"} Registration. Address="127.0.0.1"Add check. Check: = new (Consulapi. Agentservicecheck) Check. HTTP= FMT. Sprintf("http://%s:%d%s", registration. Address, registration. Port,"/check")//Set timeout5S Check. Timeout="5s"Set interval5S Check. Interval="5s"Register the Check service. Registration. Check= Check Log. Println("Get check. HTTP: ", check) Err = Client. Agent(). Serviceregister(registration) If err! = Nil {log. Fatal("Register Server error:", err)}}func Testdregister (t *testing. T) {FMT. Println("test begin.") Config: = Consulapi. Defaultconfig()//config. Address="localhost"Fmt. Println("defautl config:", config) client, err: = Consulapi. Newclient(config) if err! = Nil {log. Fatal("Consul Client Error:", err)} err = Client. Agent(). Servicederegister(Id) If err! = Nil {log. Fatal("Register Server error:", err)}}

The code is simple, creating a consul service that describes Tomcat's service port, IP. And a check method is declared to check whether the service is available.

You can observe the service registration situation through the UI interface:

The service is available.

Check failed service is not available.

3, use the dig command to check the service
bind-utils

View User-tomcat services directly above the server:

# Dig@10.0. 2.15 - P 8600 User-tomcat.service.consul SRV;<<>> DiG 9. 9. 4-redhat-9. 9. 4-38. El7_3. 2<<>>@10.0. 2.15 - P 8600 User-tomcat.service.consul SRV;(1 server found);; Global options: +cmd;; Got Answer:;; ->>HEADER<<-opcode: QUERY, Status:noerror, ID: 17543;; Flags: qr AA rd;QUERY: 1, ANSWER: 1, authority: 0, ADDITIONAL: 1;; WARNING: Recursion requested but not available;; QUESTION Section:;User-tomcat. Service. Consul. in SRV;; ANSWER Section: User-tomcat.service.consul. 0 in SRV 1 1 8080 consul-dev.node.dc1.consul.;; ADDITIONAL Section: Consul-dev.node.dc1.consul. 0 in A 127.0. 0.1;; Query Time: 0 msec;; SERVER: 10.0. 2.15 #8600 (10.0. 2.15);; When: Sun Mar  :: EST; ; MSG SIZE Rcvd:  

Consul-dev.node.dc1.consul. 0 in A 127.0.0.1
You can query to a domain name node.

4, summary

The original text connection is: http://blog.csdn.net/freewebsys/article/details/60466381
Not reproduced without the permission of the blogger.
Bo main address is: Http://blog.csdn.net/freewebsys

The overall feeling of consul is still very simple and practical.
It is very convenient to do server-side Discovery.
You can reduce the code logic of the client.

GRPC (4): Use Golang to invoke Consul API interface, register USER-TOMCAT service

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.