Spring Cloud 入门 -Consul 服务注册发现与配置中心 (Hoxton 版本)
Consul 是 HashiCorp 公司推出的开源软件,提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之 Consul 提供了一种完整的服务网格解决方案。
Spring Cloud Consul 具有如下特性:
支持服务治理:Consul 作为注册中心时,微服务中的应用可以向 Consul 注册自己,并且可以从 Consul 获取其他应用信息;
支持客户端负责均衡:包括 Ribbon 和 Spring Cloud LoadBalancer;
支持 Zuul:当 Zuul 作为网关时,可以从 Consul 中注册和发现应用;
支持分布式配置管理:Consul 作为配置中心时,使用键值对来存储配置信息;
支持控制总线:可以在整个微服务系统中通过 Control Bus 分发事件消息。
安装并运行 Consul
首先我们从官网下载 Consul,地址:https://www.consul.io/downloads.html
下载完成后只有一个 exe 文件,双击运行;
在命令行中输入以下命令可以查看版本号:
consul --version
查看版本号信息如下:
Consul v1.6.2
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
使用开发模式启动:
consul agent -dev
通过以下地址可以访问 Consul 的首页:http://localhost:8500
创建应用注册到 Consul
我们通过改造 user-service 和 ribbon-service 来演示下服务注册与发现的功能,主要是将应用原来的 Eureka 注册中心支持改为 Consul 注册中心支持。
创建 consul-user-service 模块和 consul-ribbon-service 模块;
修改相关依赖,把原来的 Eureka 注册发现的依赖改为 Consul 的,并添加 SpringBoot Actuator 的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
修改配置文件 application.yml,将 Eureka 的注册发现配置改为 Consul 的:
server:
port: 8206
spring:
application:
name: consul-user-service
cloud:
consul:
将服务注册到 consul
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
运行两个 consul-user-service 和一个 consul-ribbon-service,在 Consul 页面上可以看到如下信息:
负载均衡功能
由于我们运行了两个 consul-user-service,而 consul-ribbon-service 默认会去调用它的接口,我们调用 consul-ribbon-service 的接口来演示下负载均衡功能。
多次调用接口:http://localhost:8308/user/1 ,可以发现两个 consul-user-service 的控制台交替打印如下信息。
2019-12-29 18:20:47.834 INFO 10460 --- [io-8207-exec-10] c.j.s.controller.UserController : 根据 id 获取用户信息,用户名称为:jourwon
我们通过创建 consul-config-client 模块,并在 Consul 中添加配置信息来演示下配置管理的功能。
创建 consul-config-client 模块
在 pom.xml 中添加相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<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>
添加配置文件 applicati
on.yml,启用的是 dev 环境的配置:
spring:
profiles:
active: dev
添加配置文件 bootstrap.yml,主要是对 Consul 的配置功能进行配置:
server:
port: 9101
spring:
application:
name: consul-config-client
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: consul-config-client
config:
是否启用配置中心功能
enabled: true
设置配置值的格式
format: yaml
设置配置所在目录
prefix: config
设置配置的分隔符
profile-separator: ':'
配置 key 的名字,由于 Consul 是 K/V 存储,配置存储在对应 K 的 V 中
data-key: data
创建 ConfigClientController,从 Consul 配置中心中获取配置信息:
@RefreshScope
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
在 Consul 中添加配置
在 consul 中添加配置存储的 key 为:
config/consul-config-client:dev/data
评论