SpringCloud uses Consul as its service governance center
preface
When we develop distributed architecture system, we have an indispensable tool is the service governance component, we can register, publish and invoke services through it, can understand that it maintains the roster of all our services. Mainstream service governance centers include Zookeeper, Eureka and Nacos, but today I would like to introduce another service governance component – Consul.
Use of Nacos can be found on my blog: Nacos Service Governance Center and Configuration Center
The body of the
Consul
Consul is an integrated open source distributed service registry discovery center in Spring Cloud. Written in the Go language. Support health check, multiple DCS also support K-V storage, Raft consistency algorithm to ensure strong consistency and availability. And perfectly compatible with Docker.
Consul itself is also a service governance center, as opposed to Eureka:
Consul
Some performance is sacrificed to ensure strong consistency of services.Eureka
As long as the service is registered with the master node, it is considered successfully registered, regardless of whether other nodes can be called normally.
Consul vs Eureka vs Zookeeper
CAP principle refers to data consistency, data availability and partition tolerance respectively, where AP and CP respectively refer to:
- AP mode: Some nodes break down at the expense of strong consistency, which does not affect normal nodes.
- CP mode: Data availability is sacrificed to ensure data consistency. When a machine fails, data on all nodes cannot be used.
Consistency algorithms make a group of servers agree on a value, so the dynamic feature is that eventually each server can decide on a value. Consistency of values enables requests for the same data to be processed by the same server. Both Paxos and Raft use master to achieve consistency across multiple nodes.
Consul installation and launch
Step 1: Download Consul and select an appropriate version from the consul website based on your system. Brew brew brew brew Brew Brew Brew Brew Brew Brew Brew Brew Brew Brew Brew Brew Brew Brew Brew
brew install consul
Copy the code
Step 2: Boot upconsul
./consul agent -dev -ui -client 0.0. 0. 0
Copy the code
Start command parameters:
agent
:Consul
The core command, the main role is to maintain member information, running status detection, service declaration and processing requests-server
: Representsserver
model-ui
: indicates openweb
The console- -bootstrap-expect: indicates the number of clusters to be created. The official recommendation is 3 or 5
- -data-dir: indicates the data storage directory
-node
: represents the currentnode
The name of the-client
: should be a client service registered address, can and currentserver
The default host address is 127.0.0.1- -bind: indicates the cluster communication address
- -join: indicates the IP address of the cluster to be added
Step 3: Enterhttp://localhost:8500/ui/dc1/servicesThe Consul UI is displayed
SpringCloud uses Consul as its service governance center
Maven rely on
Consul
forSpringBoot
andSpringCloud
The version has certain requirements, so here is an exampledemo
Relatively complete dependencies.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.12..RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<lombok-version>1.182.</lombok-version> </properties> <dependencies> <! -- Use it to check node health --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <! --Spring Cloud Consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <! --spring boot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <! --lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok-version}</version> <optional>true</optional> </dependency> </dependencies> <! <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR4</version> <type>pom</type> <scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Copy the code
The properties, application of the configuration class
server.port=8080
spring.application.name=springcloud-demo-producer
#consul
spring.cloud.consul.host=127.0. 01.
spring.cloud.consul.port=8500# health inspection path spring. Cloud. Consul. Discovery. The health check - path = / physical health spring.cloud.consul.discovery.health-check-interval=15s spring.cloud.consul.discovery.hostname=127.0. 01.
spring.cloud.consul.discovery.register=true
spring.cloud.consul.discovery.port=${server.port}
#consul service name
spring.cloud.consul.discovery.serviceName=springcloud-demo-producer
spring.cloud.consul.discovery.heartbeat.enabled=true
Copy the code
SpringCloudProducerApp: start the class
@EnableDiscoveryClient
: used toconsul
orzookeeper
Provides registration services while acting as a registry
@Slf4j
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class SpringCloudProducerApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringCloudProducerApp.class);
}
/** * Project startup method **@param args
*/
public static void main(String[] args) {
SpringApplication.run(SpringCloudProducerApp.class, args);
log.info("====== service has started ========"); }}Copy the code
validation
Start the project, enterhttp://localhost:8500/ui/dc1/servicesConsulThe newly registered service is displayed on the UI