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
characteristic:
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 http://127.0.0.1:8500/v1/catalog/register Address can complete service registration
{ "Datacenter": "dc1", "Node": "node01", "Address": "192.168.74.102", "Service": { "ID":"mysql-01", "Service": "mysql", "tags": ["master","v1"], "Address": "192.168.74.102", "Port": 3306 } }
Service query
Send get request to via postman http://127.0.0.1:8500/v1/catalog/services View a list of all services
Send get request to via postman http://127.0.0.1:8500/v1/catalog/service/ Service name view specific service details
(3) Service deletion
Send a put request to via postman http://127.0.0.1:8500/v1/catalog/deregister 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--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!--actuator For heartbeat check--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
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
server: port: 9001 spring: application: name: user-server-consumer #only cloud: consul: host: 127.0.0.1 port: 8500 discovery: register: true instance-id: ${spring.application.name}-${server.port} service-name: ${spring.application.name} port: ${server.port} ip-address: ${spring.cloud.client.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
:
{spring.application.name}:
spring.application.name:{spring.cloud.client.ipAddress}
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)
@Override 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 @Override 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; System.out.println(url); //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
@Override 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; }