一、Consul安装
下载地址:https://www.consul.io/downloads.html
打开CMD,进入consul.exe所在的目录,执行命令启动Consul服务。
# 进入Consul.exe所在的目录 cd C:consul_1.6.2.windows_amd64 # 启动服务,-dev表示开发模式运行,另外还有-server标识服务模式运行 consul agent -dev
启动成功后,访问http://localhost:8500
二、monitor改造
在pom.xml中添加spring cloud和consul注册中心依赖。
<!--consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
<!--srping cloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml
server: port: 8000 spring: application: name: jansens-monitor cloud: consul: host: localhost port: 8500 discovery: serviceName: ${spring.application.name} # 注册到consul的服务名称
修改启动类,添加@EnableDiscoveryClient注解
JansensMonitorApplication.java
@EnableAdminServer @EnableDiscoveryClient @SpringBootApplication public class JansensMonitorApplication { public static void main(String[] args) { SpringApplication.run(JansensMonitorApplication.class, args); } }
三、backup改造
在pom.xml中添加spring cloud和consul注册中心依赖。
<!--consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
<!--srping cloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml
# tomcat server: port: 8002 spring: application: name: jansens-backup boot: admin: client: url: "http://localhost:8000" cloud: consul: host: localhost port: 8500 discovery: serviceName: jansens-backup # 注册到consul的服务名称 # 开放健康检查接口 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS # backup datasource jansens: backup: datasource: host: localhost userName: root password: root database: jansens
修改启动类,添加@EnableDiscoveryClient注解
四、admin改造
在pom.xml中添加spring cloud和consul注册中心依赖。
<!--consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
<!--srping cloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml
server: port: 8001 spring: application: name: jansens-admin boot: admin: client: url: "http://localhost:8000" datasource: name: druidDataSource type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/jansens?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8&useSSL=false username: root password: root filters: stat,wall,log4j,config max-active: 100 initial-size: 1 max-wait: 60000 min-idle: 1 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: select 'x' test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true max-open-prepared-statements: 50 max-pool-prepared-statement-per-connection-size: 20 zcloud: consul: host: localhost port: 8500 discovery: serviceName: ${spring.application.name} # 注册到consul的服务名称 # 开放健康检查接口 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
修改启动类,添加@EnableDiscoveryClient注解