springCloud→Consul:项目中使用Consul


项目中使用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已经注册进来了

在这里插入图片描述


文章作者: fFee-ops
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 fFee-ops !
评论
  目录