前置条件:首先根据 搭建Consul集群 搭建好
Consul
集群。
1 注册服务
下面使用Spring Cloud Consul
实现服务注册/发现。
Spring Cloud
版本:Greenwich.SR5
Spring Boot
版本:2.1.12.RELEASE
(不是越新越好,要和Spring Cloud
版本对应)Consul
版本:1.6.3
引入依赖
首先需要引入几个依赖:
<!-- spring cloud consul 相关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- 健康检查相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 如果需要提供web服务,需要引入web相关依赖 -->
<dependency>
<groupId>org.springframework.boot