pause
访问管理后台:http://localhost:8500/ 看到下图意味着我们的 Consul 服务启动成功了。
初始化配置
点击链接观看:初始化配置信息视频(获取更多请关注公众号「哈喽沃德先生」)
创建基本目录
使用 Consul 作为配置中心,第一步我们先创建目录,把配置信息存储至 Consul。
点击菜单 Key/Value
再点击 Create
按钮。
创建 config/
基本目录,可以理解为配置文件所在的最外层文件夹。
创建应用目录
点击 config
进入文件夹。
再点击 Create
按钮。
创建 orderService/
应用目录,存储对应微服务应用的 default
环境配置信息。
多环境应用目录
假设我们的项目有多环境:default
、test
、dev
、prod
,在 config
目录下创建多环境目录。
orderService
文件夹对应 default
环境orderService-test
文件夹对应 test
环境orderService-dev
文件夹对应 dev
环境orderService-prod
文件夹对应 prod
环境
初始化配置
以 dev
环境为例,点击 orderService-dev
进入文件夹。
点击 Create
按钮准备创建 Key/Value
配置信息。
填写 Key:orderServiceConfig
填写 Value:
name: order-service-dev
mysql:
host: localhost
port: 3006
username: root
password: root
假设以上内容为订单微服务的配置信息,下面我们通过案例来加载 Consul 配置中心中的配置信息。
consul-config-demo
聚合工程。SpringBoot 2.2.4.RELEASE
、Spring Cloud Hoxton.SR1
。
点击链接观看:Consul 配置中心实践视频(获取更多请关注公众号「哈喽沃德先生」)
需要从 Consul 获取配置信息的项目主要添加 spring-cloud-starter-consul-config
依赖,完整依赖如下:
order-service
和 order-service02
依赖一致。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>order-service</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.example</groupId>
<artifactId>consul-config-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
老规矩,配置文件必须叫 bootstrap.yml
我们除了使用 Consul 配置中心功能之外,把微服务也注册到 Consul 注册中心去。order-service
和 order-service02
的配置项中除了端口和注册实例 id 之外,其余配置项一致,完整配置如下:
server:
port: 9090
spring:
application:
name: order-service
profiles:
active: dev
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
prefix: config
default-context: orderService
profile-separator: '-'
format: YAML
data-key: orderServiceConfig
watch:
enabled: true
delay: 1000
discovery:
register: true
instance-id: ${spring.application.name}-01
service-name: ${spring.application.name}
port: ${server.port}
prefer-ip-address: true
ip-address: ${spring.cloud.client.ip-address}
配置文件实体类
order-service
和 order-service02
实体类代码一致。
package com.example.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "mysql")
public class MySQLProperties {
private String host;
private Integer port;
private String username;
private String password;
public String getHost() {
return host;
public void setHost(String host) {
this.host = host;
public Integer getPort() {
return port;
public void setPort(Integer port) {
this.port = port;
public String getUsername() {
return username;
public void setUsername(String username) {
this.username = username;
public String getPassword() {
return password;
public void setPassword(String password) {
this.password = password;
order-service
和 order-service02
控制层代码一致。
注意需要添加 @RefreshScope
注解用于重新刷新作用域实现属性值自动刷新。
package com.example.controller;
import com.example.config.MySQLProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class ConfigController {
@Autowired
private MySQLProperties mySQLProperties;
@Value("${name}")
private String name;
@GetMapping("/name")
public String getName() {
return name;
@GetMapping
("/mysql")
public MySQLProperties getMySQLProperties() {
return mySQLProperties;
order-service
和 order-service02
启动类代码一致。
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
修改配置信息前
访问:http://localhost:9090/name 结果如下:
访问:http://localhost:9090/mysql 结果如下:
修改配置信息
修改 Consul 配置中心 orderService-dev
环境的配置信息为:
name: order-service-dev-2.0
mysql:
host: localhost
port: 3006
username: root123
password: root123
修改配置信息后
控制台打印信息如下:
[TaskScheduler-1] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-config/order-service-dev/'}, BootstrapPropertySource {name='bootstrapProperties-config/order-service/'}, BootstrapPropertySource {name='bootstrapProperties-config/orderService-dev/'}, BootstrapPropertySource {name='bootstrapProperties-config/orderService/'}]
[TaskScheduler-1] o.s.boot.SpringApplication : The following profiles are active: dev
[TaskScheduler-1] o.s.boot.SpringApplication : Started application in 3.748 seconds (JVM running for 142.28)
[TaskScheduler-1] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [name, mysql.password, mysql.username]
Consul 使用 Spring 定时任务 Spring TaskScheduler
来监听配置文件的更新。
默认情况下,它是一个定时任务线程池 ThreadPoolTaskScheduler
,其poolSize
值为 1
。要更改TaskScheduler
,请创建一个 TaskScheduler
使用 ConsulConfigAutoConfiguration.CONFIG_WATCH_TASK_SCHEDULER_NAME
常量命名的 bean 类型。
访问:http://localhost:9090/name 结果如下:
访问:http://localhost:9090/mysql 结果如下:
HashiCorp 公司的 Consul 可谓是一款全能组件。可用于提供服务发现和服务配置的工具。用 go 语言开发,具有很好的可移植性,被 Spring Cloud 纳入其中。
在注册中心方面,Netflix Eureka 停止新版本开发,Consul 成为了优秀的可替代方案。
在配置中心方面,Consul 亦可替代 Spring Cloud Config 作为配置中心使用,且无需配合 Git、SVN 等工具,无需配合 Bus 消息总线即可实现集群配置更新。
本文采用 知识共享「署名-非商业性使用-禁止演绎 4.0 国际」许可协议
。
大家可以通过 分类
查看更多关于 Spring Cloud
的文章。
🤗 您的点赞
和转发
是对我最大的支持。
📢 扫码关注 哈喽沃德先生
「文档 + 视频」每篇文章都配有专门视频讲解,学习更轻松噢 ~