The Spring Cloud Consul project is a service governance implementation for consul. Consul is a distributed, highly available system that contains multiple components, but as a whole, a tool for providing service discovery and service configuration for our infrastructure in the microservices architecture. It contains the following features:
- Service discovery
- Health Check
- Key/value Storage
- Multi-Data center
Thanks to the implementation of the spring Cloud Consul project, we can easily register spring boot-based microservices applications on consul and implement service governance in the MicroServices architecture.
Based on the previously implemented Eureka-based example (eureka-client), how do we register previously implemented service providers on consul? The method is very simple and we only need to pom.xml
modify the dependency of Eureka to the following dependencies:
1234 |
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-consul-discovery</artifactid> </dependency> |
Next, modify the application.properites
Consul required configuration information, such as: (The following configuration is the default value)
12 |
spring.cloud.consul.host=localhostspring.cloud.consul.port=8500 |
To this end, we have completed the conversion of Eureka-client to a service provider based on consul service governance. We have already mentioned that the interface of service discovery DiscoveryClient
is a layer of abstraction that spring cloud provides to service governance, so we can block the implementation details of Eureka and consul service governance, and our program does not need to make any changes, only need to introduce different service governance dependencies, and configure the associated configuration properties to easily incorporate microservices into the various service governance frameworks in spring cloud.
Below you can try to get consul service providers up and running. Perhaps the reader will ask, is there no need to create a server like Eureka-server? Since the consul itself provides the service side, we do not need to create a service registry as previously implemented Eureka, which can be used directly by downloading the consul service-side program.
We can start the Consul development mode with the following command:
12345678910111213141516171819202122232425262728293031323334 |
$consul Agent-dev==> starting Consul agent...==> starting Consul agent rpc...==> Consul agent running! Version:' v0.7.2 'Node Name:' LENOVO-ZHAIYC 'Datacenter:' DC1 'Server:true (bootstrap: false) Client addr:127.0.0.1 (http:8500, HTTPS:-1, dns:8600, rpc:8400) Cluster addr:127.0.0.1 (lan:8301, wan:8302) Gossip E Ncrypt:false, Rpc-tls: false, Tls-incoming: false Atlas: <disabled> ==> Log data would now streamIn as it occurs:2017/06/22 07:50:54 [INFO] raft:initial configuration (index=1): [{suffrage:voter id:127.0.0.1:8300 Address : 127.0.0.1:8300}]2017/06/22 07:50:54 [INFO] raft:node at 127.0.0.1:8300 [Follower] entering Follower state (Leader:"")2017/06/22 07:50:54 [INFO] serf:eventmemberjoin:lenovo-zhaiyc 127.0.0.12017/06/22 07:50:54 [info] consul:adding LAN ser Ver lenovo-zhaiyc (addr:tcp/127.0.0.1:8300) (DC:DC1) 2017/06/22 07:50:54 [INFO] Serf:eventmemberjoin: LENOVO-ZHAIYC.DC1 127.0.0.12017/06/22 07:50:54 [INFO] consul:adding WAN server LENOVO-ZHAIYC.DC1 (addr:tcp/ 127.0.0.1:8300) (DC:DC1) 2017/06/22 07:51:01 [ERR] agent:failed to sync remote state:no cluster LEADER2017/06/22 07:51:0 2 [WARN] raft:heartbeat timeout from" " reached, starting election2017/06/22 07:51:02 [INFO] raft:node at 127.0.0.1:8300 [candidate] entering candidate stateIn Term 22017/06/22 07:51:02 [Debug] raft:votes needed:12017/06/22 07:51:02 [Debug] raft:vote granted from 127.0.0.1:8300In the term 2. Tally:12017/06/22 07:51:02 [INFO] Raft:election won. TALLY:12017/06/22 07:51:02 [INFO] raft:node at 127.0.0.1:8300 [Leader] Entering Leader state2017/06/22 07:51:02 [INFO] C Onsul:cluster leadership ACQUIRED2017/06/22 07:51:02 [INFO] consul:new leader Elected:lenovo-zhaiyc2017/06/22 07:51:02 [DEBUG] Consul:reset tombstone GC to index 32017/06/22 07:51:02 [INFO] Consul:member' LENOVO-ZHAIYC ' joined, marking health alive2017/06/22 07:51:02 [INFO] agent:synced Service' Consul '2017/06/22 07:51:02 [DEBUG] Agent:node InfoIn sync |
After the consul server is started, we start the Consul service provider that was previously transformed. Consul, like Eureka, provides a simple UI interface to view the registration of services:
From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects. Source Source
To introduce you to spring Cloud Consul