springcloud 中使用consul作为注册中心
一、服务提供方配置
1、pom.xml 中引入
<!--consul 起步依赖 -->
<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>
2、application.yml 配置
spring:
cloud:
consul:
host: 192.168.9.130
port: 8500
# discovery:
# healthCheckPath: /health
# healthCheckInterval: 15s
# serviceName: ${spring.application.name}
3、springboot启动类上标记 @EnableDiscoveryClient 注解
二、 服务消费方配置
1、 pom.xml
<!--consul 起步依赖 -->
<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>
2、application.yml配置
spring:
cloud:
consul:
host: 192.168.9.130
port: 8500
discovery:
register: false #表示不进行服务注册
3、springboot启动类上标记 @EnableDiscoveryClient注解
ok,接下来就可以在服务消费方调用服务提供方提供的接口了,具体实现方式可以采用resTemplate+ribbon或者feign均可。
需要注意的是,如果consul服务器不是本机(localhost),则需要让consul服务器能够解析本机的主机名。
为简便起见,可以修改consul所在服务器的host文件,添加本地主机名和ip的映射即可。
启动consul命令:
consul agent -dev -client 0.0.0.0 -ui
consul HTTP REST API为8500端口
consul DNS API 为8600端口
UI界面 http://ip:8500/ui


阅读排行


Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1