Registry (consumer)

What is consumer

Consumer overview

Consul is an open source tool launched by HashiCorp, which is used to realize service discovery and configuration of distributed systems. Compared with other distributed service registration and discovery schemes, consul's scheme is more "one-stop", with built-in service registration and discovery framework, distributed consistency protocol implementation, health check, Key/Value storage and multi data center scheme. It no longer needs to rely on other tools (such as ZooKeeper). It is also relatively simple to use. Consul is written in Go language, so it has natural portability (supports Linux, windows and Mac OS X); The installation package contains only one executable file, which is convenient for deployment and can work seamlessly with lightweight containers such as Docker.
Consul's advantages:
1. Using Raft algorithm to ensure consistency is more direct than complex Paxos algorithm In contrast, zookeeper uses
Paxos, while etcd uses Raft.
2. Support multiple data centers, and use different ports to monitor the services of internal and external networks. Multi data center cluster can avoid single data center
Single point of failure, and its deployment needs to consider network delay, fragmentation and so on. zookeeper and etcd do not provide support for multi data center functions.
3. Support health examination. etcd does not provide this function.
4. Support http and dns protocol interfaces. The integration of zookeeper is complex. etcd only supports http protocol.
5. The official web management interface is provided, but etcd does not have this function.
6. Through comprehensive comparison, Consul, as a new star of service registration and configuration management, deserves more attention and research


Service discovery
health examination
Key/Value storage
Multi data center
Difference between consumer and Eureka
(1) Consistency
Consul strong consistency (CP)
Eureka guarantees high availability and final consistency (AP)
(2) Development language and use
eureka is a servlet program that runs in a servlet container
Consul is written by go and can be installed and started

Download and installation of consumer

Basic use of consumer
Consul supports health check and provides API interfaces for HTTP and DNS calls to complete service registration, service discovery, and K/V storage. Next, learn about consol by sending HTTP requests

Service registration and discovery

Registration service

Send a put request to via postman Address can complete service registration

"Datacenter": "dc1", 
"Node": "node01", 
"Address": "", 
"Service": { 
"Service": "mysql", 
"tags": ["master","v1"], 
"Address": "", 
"Port": 3306 

Service query

Send get request to via postman View a list of all services
Send get request to via postman Service name view specific service details
(3) Service deletion
Send a put request to via postman Delete service

 "Datacenter": "dc1", 
 "Node": "node01", 
 "ServiceID": "mysql-01" 

After deletion

Service registration based on consumer

Case preparation

Modify relevant pom files of microservices
Remove the dependency and configuration of eureka
Modify the pom file of each microservice and add the Consul based dependency provided by spring cloud

<!--SpringCloud Provided based on Consul Service discovery for-->
<!--actuator For heartbeat check-->

Spring cloud starter consumer discovery is a dependency of spring cloud on consumer support. Spring boot starter actor is applicable to the dependency of completing heartbeat detection response.
Configure service registration
Modify the application. Of each microservice YML configuration file, add relevant configuration information of consumer service discovery

  port: 9001
    name: user-server-consumer #only
      port: 8500
        register: true
        instance-id: ${}-${server.port}
        service-name: ${}
        port: ${server.port}
        ip-address: ${}
        prefer-ip-address: true

Including spring cloud. Add the related configuration of the consumer to the consumer
host: indicates the request address of the Consul Server
Port: indicates the port of the Consul Server
Discovery: configuration related to service registration and discovery
Instance id: the unique id of the instance (required for recommendation), which is the recommendation of the documents on the official website of spring cloud. In order to ensure the generation of an instance
A unique id, which can also be replaced by
s p r i n g . a p p l i c a t i o n . n a m e : {}:{}
Preferred ip address: enable ip address registration
ip address: the request ip of the current microservice

View the list of services in the console

Open the management console of ConsulServer and you can find that all microservices have been registered in Consul.

Three implementation methods of client calling server

//The first implementation does not require any registry (directly splicing URLs)

 public List<Order> currentUserOrderInfo(Integer uid) {
     String url = "http://localhost:8001/findOrderByUid/" + uid;
     //http request calls remote method
     List<Order> list = restTemplate.getForObject(url, List.class);
     return list;
The second way is to add on the startup class@EnableDiscoveryClient
 public List<Order> currentUserOrderInfo(Integer uid) {
     System.out.println("discoveryClient call");
     List<ServiceInstance> instances = discoveryClient.getInstances("order-server-provider");
     ServiceInstance serviceInstance = instances.get(0);
     String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/findOrderByUid/" + uid;
     //http request calls remote method
     List<Order> list = restTemplate.getForObject(url, List.class);
     return list;

//In the third way, ribbon is used for balancing, and @ LoadBalanced must be added to RestTemplate

 public List<Order> currentUserOrderInfo(Integer uid) {
//When using ribbon load balancing in this way, @ LoadBalanced must be added to RestTemplate
     String url = "http://order-server-provider/findOrderByUid/" + uid;
     //http request calls remote method
     List<Order> list = restTemplate.getForObject(url, List.class);
     return list;

Tags: consul

Posted by TristanOfADown on Sat, 14 May 2022 09:43:53 +0300