A list,
- Official introduction, Chinese translation, official download
- In general, Consul can provide enrollment services. It can also be monitored through the web. It’s a good alternative to Eureka.
- It is also worth noting that this is a product that guarantees AP (high availability, fault tolerance of partitions) and dismisses C (high consistency).
Two, installation and use
1. Install
- Consul is installed on centos7 in order to simulate a real production environment. You can click the official download address, according to their actual environment to choose.
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul
Copy the code
2. Enable Consul
-dev
Indicates that the environment is currently under development,- client 0.0.0.0
Indicates that all IP addresses are allowed to access. This step is mandatory; otherwise, remote access cannot be performed.
Consul agent-dev-client 0.0.0.0Copy the code
3. Monitoring center
- Obtain the IP address of Consul’s running machine
ifconfig
Copy the code
- In the local browser, add port number 8500 according to the IP address to view the monitoring. If the following page is displayed, it is successfully enabled.
Registration center
- The operation on Java is not much different from Eureka or ZK.
1. The package
- Most of all, of course
spring-cloud-starter-consul-discovery
<dependencies>
<! --SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<! -- SpringBoot integrate Web components -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<! Jar package configuration -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Copy the code
2. Yaml configuration file
spring.cloud.consul.host
Based on the actual situation, the IP address is the same as that obtained before. If the port number is not changed, it is always 8500.- It is worth mentioning that the heartbeat needs to be enabled; otherwise, unhealthy services will be displayed on the monitoring interface.
# Consul service port number
server:
port: 8006
spring:
application:
name: cloud-payment-service
# Consul Registry address
cloud:
consul:
host: 192.168233.137.
port: 8500
discovery:
# the hostname: 127.0.0.1
service-name: ${spring.application.name}
# Start heartbeat
heartbeat:
enabled: true
Copy the code
3. Main startup class
@SpringBootApplication
@EnableDiscoveryClient// This annotation is used to register services when using Consul or ZooKeeper as a registry
public class PaymentApplication8006 {
public static void main(String[] args) { SpringApplication.run(PaymentApplication8006.class, args); }}Copy the code
4. Check the monitoring
- There are already two services with one instance each.