项目中使用Consul
项目中要想使用Consul作为服务注册中心,只需要引入如下依赖包,在启动类上添加@EnableDiscoveryClient
注解,并在application.yml中添加Consul服务地址即可:
实例
项目结构图:
1)添加依赖
在项目hailtaxi-gateway
添加依赖包:
<!--consul-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
2)添加@EnableDiscoveryClient
注解
在hailtaxi-gateway
启动类GatewayApplication
上添加@EnableDiscoveryClient
注解:
3)配置Consul服务信息
在application.yml
中添加Consul服务信息:
4)验证
此时启动对应项目,并提前启动consul。可以看到gateway已经注册进来了