consul集群搭建和springboot服务发现
由于系统准备重构库存模块,以微服务的方式处理出入库和库存占用情况。但是单个服务容易宕机导致整个应用中断,所以需要用consul做服务注册和发现防止单点故障。
1. 准备3台centos7的虚拟机
- 192.168.5.211
- 192.168.2.60
- 192.168.2.61
2. 3台虚拟机安装consul
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install consul
consul --version
3. 3台机器关闭防火墙
systemctl firewalld stop
4. 建立集群,分别在ip上执行
consul agent -server -bootstrap-expect=3 -data-dir=/tmp/consul -config-file=/usr/src/consul -node=192.168.5.211 -bind=0.0.0.0 -client=0.0.0.0 -ui -rejoin
consul agent -server -bootstrap-expect=3 -data-dir=/tmp/consul -config-file=/usr/src/consul -node=192.168.2.61 -bind=0.0.0.0 -client=0.0.0.0 -ui -rejoin -join=192.168.5.211
consul agent -server -bootstrap-expect=3 -data-dir=/tmp/consul -config-file=/usr/src/consul -node=192.168.2.60 -bind=0.0.0.0 -client=0.0.0.0 -ui -rejoin -join=192.168.5.211
如果需要添加client,执行如下代码
consul agent -data-dir=/tmp/consul -config-file=/usr/src/consul -node=192.168.2.85 -bind=0.0.0.0 -client=0.0.0.0 -ui -join=192.168.5.211
consul agent -data-dir=/tmp/consul -config-file=/usr/src/consul -node=192.168.2.89 -bind=0.0.0.0 -client=0.0.0.0 -ui -join=192.168.5.211
然后访问 http://192.168.5.211:8500/
5. springboot建立空项目
添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
添加配置文件 application.properties
spring.application.name=consul-springboot-demo
server.port=18888
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.host=192.168.5.211
spring.cloud.consul.port=8500
#spring.cloud.consul.discovery.health-check-path=/health
#spring.cloud.consul.discovery.health-check-interval=15s
spring.cloud.consul.discovery.hostname=demo
spring.cloud.consul.discovery.register-health-check=true
spring.cloud.consul.discovery.prefer-ip-address=true
spring.cloud.consul.discovery.ip-address=192.168.2.127