This is a creation in
Article, where the information may have evolved or changed.
1,consul Service Discovery
After the use of a few times and finally feel the system is very good.
In a small team, to do service discovery, do simple and efficient.
You can use consul directly as a service discovery, service-checking system.
You don't have to develop it yourself. Really convenient, as long as the deployment is possible.
It's easy to put the code on GitHub.
Https://github.com/freewebsys/grpc-go-demo
2, re-study consul launch
The discovery was done using dig, because the consul official website is also recommended.
But you can actually use Ping, nslookup is the same.
However, you need to modify the port of consul.
consul agent -dev -ui -server -node = consul-dev -client = 10.0 .2 .15 -dns -port = 53 -domain = freeweb Consul
-dns-port=53 is to disguise consul as a DNS server.
-domain=freeweb.consul is to set its own domain.
This can be disguised as a DNS server.
Sometimes think of a service discovery also to DNS, feel the multiplicity ah.
It's so strange to feel like a Dubbo service.
But it is this DNS that makes service discovery very simple.
And the use of DNS is also very consistent with the appetite of operations students.
They also have many ways to ensure that DSN is not a problem.
Use go to create 3 services with a third service port error.
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 +" the"Registration. Name="User-tomcat"Registration. Port=8080Registration. Tags= []string{"group:user-tomcat-01"} 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 registration:", registration) Err = Client. Agent(). Serviceregister(registration) If err! = Nil {log. Fatal("Register Server error:", err)}//Set a second object. Register two Tomcat. Registration. ID= Id +" the"Registration. Name="User-tomcat"Registration. Address="10.0.2.15"Registration. Tags= []string{"group:user-tomcat-02"} Log. Println("Get registration:", registration) Err = Client. Agent(). Serviceregister(registration) If err! = Nil {log. Fatal("Register Server error:", err)}//Set section3An object. Register two Tomcat. Registration. ID= Id +" the"Registration. Name="User-tomcat"Registration. Address="10.0.2.16"Registration. Tags= []string{"group:user-tomcat-03"} check. HTTP= FMT. Sprintf("http://%s:%s%s", registration. Address,"8083","/check") Log. Println("Get registration:", registration) Err = Client. Agent(). Serviceregister(registration) If err! = Nil {log. Fatal("Register Server error:", err)}}
Then configure the DNS server:
Multiple nameserver are configured in/etc/resolv.conf:
nameserver10.0.2.15
When using the nslookup command:
# nslookup User-tomcat.service.freeweb.consulServer: 10.0. 2. theAddress: 10.0. 2. the#53Name:User-tomcat. Service. Freeweb. ConsulAddress: 10.0. 2. theName:User-tomcat. Service. Freeweb. ConsulAddress: 127.0. 0. 1#也可以ping通了. # ping User-tomcat.service.freeweb.consulPINGUser-tomcat. Service. Freeweb. Consul(10.0. 2. the) About( -) bytes of data. -bytes from localhost. Localdomain(10.0. 2. the): icmp_seq=1Ttl= -Time=0.008Ms -bytes from localhost. Localdomain(10.0. 2. the): icmp_seq=2Ttl= -Time=0.028Ms
You will find only 2 tomcat services.
3, a simple Grpc+consul architecture
A simple GRPC + Consul architecture is done.
Registration of the service, found using Consul, because consul comes with check checks.
If the service is not available. The service is automatically removed.
The service RPC framework uses GRPC, which serves only the DNS service under Consul with access for each link.
Connected to the next is the HTTP2 binary transmission, the use of the grpc thing.
At the same time it is convenient to develop, configure the DNS can be directly used to access the domain name.
The development & operation is also super convenient. And consul also supports clustering.
DNS servers can also configure multiple consul. Also convenient operation and maintenance.
4, summary
The text of this text connection is: http://blog.csdn.net/freewebsys/article/details/60779640 not allowed to reprint without the Bo master.
Bo main address is: Http://blog.csdn.net/freewebsys
New technologies have brought new ideas. The development of the operation of the transformation is more simple.
In the company to carry out business split when the fastest will be the registration center, service discovery.
are well-built, and are simple and efficient for RPC development.