Spring Cloud Config实现集群配置中心
Spring Cloud Config为分布式系统提供了配置服务器和配置客户端,可以管理集群中的配置文件。
使用Git、SVN等版本管理系统存放配置文件,配置服务器会到版本管理系统获取配置,集群中的配置客户端再到配置服务器中获取配置。
开发工具:IntelliJ IDEA 2019.2.2
一、创建配置服务器
1、SVN服务器添加项目和配置文件

config-client-dev.yml内容:
server:
port: 8092
test:
user:
name: aa
config-client-test.yml
server:
port: 8093
test:
user:
name: bb
2、创建项目
IDEA中创建一个新的SpringBoot项目,名称为“spring-config-server”,SpringBoot版本选择2.1.10,在选择Dependencies(依赖)的界面勾选Spring Cloud Config -> Config Server。
pom.xml会引入spring-cloud-config-server依赖项,再在pom.xml中加入org.tmatesoft.svnkit依赖项,pom.xml完整内容如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.10.1</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3、修改配置application.yml
spring-cloud-config-server提供了4种配置,可以通过不同名字来激活:
(1)git:默认值,表示去Git仓库读取配置文件;
(2)subversion:表示去SVN仓库读取配置文件;
(3)native:表示去本地文件系统读取配置文件;
(4)vault:表示去Vault(一种资源控制工具)中读取配置文件;
server:
port: 8091
spring:
application:
name: config-server
profiles:
active: subversion
cloud:
config:
server:
svn:
uri: https://localhost/svn/test-project
username: abc
password: 123456
default-label: default-config
4、修改启动类代码
增加注解@EnableConfigServer
package com.example.springconfigserver; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
} }
可以使用Config Server的端点获取配置文件的内容,端点与配置文件的映射规则如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
{application} 是应用名称,对应配置文件的名称部分,本例是config-client。
{profile} 是配置文件的版本,本例是dev和test。
{label} 表示分支,如果是git则默认是master分支。
启动服务,浏览器访问(把下面test换为dev,结果类似)下面地址,分别输出如下:
http://localhost:8091/config-client/test
{"name":"config-client","profiles":["test"],"label":null,"version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}
http://localhost:8091/config-client/test/default-config
{"name":"config-client","profiles":["test"],"label":"default-config","version":"6","state":null,"propertySources":[{"name":"https://localhost/svn/test-project/default-config/config-client-test.yml","source":{"server.port":8093,"test.user.name":"bb"}}]}
http://localhost:8091/config-client-test.yml
server:
port: 8093
test:
user:
name: bb
http://localhost:8091/default-config/config-client-test.yml
server:
port: 8093
test:
user:
name: bb
二、配置客户端读取SVN配置
1、创建项目
IDEA中创建一个新的SpringBoot项目,名称为“spring-config-client”,SpringBoot版本选择2.1.10,在选择Dependencies(依赖)的界面勾选Web -> Spring Web,Spring Cloud Config -> Config Client。
pom.xml会引入spring-boot-starter-web和spring-cloud-starter-config依赖项,pom.xml完整内容如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
2、修改启动类代码
增加测试方法
package com.example.springconfigclient; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
public class SpringConfigClientApplication { @Autowired
private Environment env; public static void main(String[] args) {
SpringApplication.run(SpringConfigClientApplication.class, args);
} @RequestMapping("/")
public String home(){
return env.getProperty("test.user.name");
}
}
3、添加配置bootstrap.yml
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8091
profile: dev
设置了应用名称config-client,使用spring.cloud.config.uri来设置配置服务器的地址,使用spring.cloud.config.profile来读取指定的配置。最终,配置客户端会到SVN服务器的test-project/default-config目录下读取config-client-dev.yml(.properties)。
启动服务,浏览器访问:http://localhost:8092/(这里端口8092在config-client-dev.yml中已指定),页面输出:aa
也可以使用spring.cloud.config.name代替spring.application.name,结果一样。
spring:
cloud:
config:
uri: http://localhost:8091
profile: dev
name: config-client
如果spring.cloud.config.name和spring.application.name都不提供,则默认读取application-dev.yml。
在SVN的test-project/default-config目录下新增文件application-dev.yml,内容
server:
port: 8092
test:
user:
name: cc
启动服务,浏览器访问:http://localhost:8092/,页面输出:cc
可以设置spring.client.config.label来覆盖服务器的default-lable属性,另外上面profile也可改为下面写法。
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8091
lable: default-config
name: config-client
profiles:
active: dev
三、使用/refresh端点手动刷新配置
1、在上面配置客户端的pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、application.yml(或bootstrap.yml)添加配置
management:
endpoints:
web:
exposure:
include: "*"
3、在Controller上添加注解@RefreshScope
package com.example.springconfigclient; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @SpringBootApplication
@RestController
@RefreshScope
public class SpringConfigClientApplication { @Autowired
private Environment env; public static void main(String[] args) {
SpringApplication.run(SpringConfigClientApplication.class, args);
} @RequestMapping("/")
public String home(){
return env.getProperty("test.user.name");
}
}
4、修改SVN服务器上config-client-dev.yml内容
把name的值由aa修改为aa11,提交SVN修改。
5、/refresh只支持POST请求,发送POST请求到http://localhost:8092/actuator/refresh
使用Postman发送POST请求,如果SVN没有修改,返回[],如果有修改,返回结果如下:

刷新浏览器地址:http://localhost:8092/,结果已由aa,变成了aa11。
四、使用Spring Cloud Bus自动刷新配置
上面使用/refresh端点手动刷新配置只是刷新一个客户端的配置,如果有很多微服务的客户端,则需要一个个需要手动刷新。
使用Spring Cloud Bus可以实现刷新一个客户端配置后,自动刷新其余的客户端配置。
Spring Cloud Bus使用消息代理(RabbitMQ、Kafka等)连接分布式系统的客户端,广播传播状态的更改或其他的管理指令。
Spring Cloud Bus的结构图如下,所有客户端通过消息总线连接到了一起,每个客户端会订阅配置更新事件,当其中一个客户端的actuator/bus-refresh被请求时,会向消息总线发送一个配置更新事件,其他客户端获得该事件后也会更新配置。

使用实例:
1、SVN服务器只保留config-client-dev.yml,内容如下:
test:
user:
name: aa
2、上面客户端项目“spring-config-client”的pom.xml加入依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3、application.yml添加配置(如果rabbitmq服务器在本机,端口和账号没改的话,下面也可不添加)
Spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
4、修改启动类代码,支持启动时输入端口号
IDEA 配置可以启动多次:IDEA -> Edit Configurations -> Run/Debug Configuration -> 勾选Allow parallel run
package com.example.springconfigclient; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.Scanner; @SpringBootApplication
@RestController
@RefreshScope
public class SpringConfigClientApplication { @Autowired
private Environment env; public static void main(String[] args) {
//SpringApplication.run(SpringConfigClientApplication.class, args);
Scanner scan = new Scanner(System.in);
String port = scan.nextLine();
new SpringApplicationBuilder(SpringConfigClientApplication.class).properties("server.port=" + port).run(args);
} @RequestMapping("/")
public String home(){
return env.getProperty("test.user.name");
}
}
5、测试
(1)客户端项目配置可以启动多次:IDEA -> Edit Configurations -> Run/Debug Configuration -> 勾选Allow parallel run
(2)启动启动端spring-config-client,输入端口号9001,浏览器访问http://localhost:9001/,页面显示aa
(3)再次启动启动端spring-config-client,输入端口号9002,浏览器访问http://localhost:9002/,页面显示aa
(4)修改SVN的config-client-dev.yml的name值为aa123
(5)使用Postman发送POST请求到http://localhost:9001/actuator/bus-refresh

(6)浏览器访问http://localhost:9001/和http://localhost:9002/,两个页面都已显示aa123。
Spring Cloud Config实现集群配置中心的更多相关文章
- Spring Cloud config之一:分布式配置中心入门介绍
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...
- Spring Cloud Config 1 (分布式配置中心)
spring cloud config是spring cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两部分. 服务端也被称为 ...
- Spring Cloud Eureka的集群配置(六)
1.再次创建2个Eureka工程 工程名:microservicecloud-eureka-7002 工程名:microservicecloud-eureka-7003 2.pom.xml文件 < ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心
Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...
- Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】
Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-26 | 本文接之前的<Spring Clou ...
- Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】
Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-24 | Spring Cloud Confi ...
- Spring Cloud Alibaba | Nacos集群部署
目录 Spring Cloud Alibaba | Nacos集群部署 1. Nacos支持三种部署模式 2. 集群模式下部署Nacos 2.1 架构图 2.2 下载源码或者安装包 2.3 配置集群配 ...
- Spring Cloud第十一篇 | 分布式配置中心高可用
本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- Spring Cloud(九):配置中心(消息总线)【Finchley 版】
Spring Cloud(九):配置中心(消息总线)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-05-07 | 我们在 Spring Cloud(七):配置中心 ...
随机推荐
- Java基础面试题及答案(五)
Java Web 64. jsp 和 servlet 有什么区别? jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器 ...
- Python模块——loguru日志模块简单学习
Python loguru模块简单学习 首先安装模块:pip install logoru,然后引入模块: from loguru import logger 1.直接输出到console logge ...
- CSS块级-内联元素,盒子模型
CSS元素 元素分类和区别 常用的块状元素有: <div>.<p>.<h1>~<h6>.<ol>.<ul>.<li> ...
- 压缩打包介绍、gzip、bzip2、xz压缩工具
第5周第1次课(4月16日) 课程内容: 6.1 压缩打包介绍6.2 gzip压缩工具6.3 bzip2压缩工具6.4 xz压缩工具 6.1 压缩打包介绍 为什么要给文件进行压缩呢?首先压缩和不压缩空 ...
- PHP获取绝对路径dirname(__FILE__)和__DIR__比较
我们都知道,要获取当前PHP脚本所在目录的绝对路径,dirname(__FILE__) 和 __DIR__都可以实现.那么什么情况下用 dirname(__FILE__),什么情况下用 __DIR__ ...
- 第四章 开始Unity Shader学习之旅(3)
1. 程序员的烦恼:Debug 调试(debug),大概是所有程序员的噩梦.而不幸的是,对一个Shader进行调试更是噩梦中的噩梦.这也是造成Shader难写的原因之一--如果发现得到的效果不对,我们 ...
- golang数据结构之队列
队列可以用数组或链表实现,遵从先入先出. 目录结构: 在main中调用queue包中的属性和方法,如何调用参考另一篇文章: https://www.cnblogs.com/xiximayou/p/12 ...
- 基于H.ui.Admin UI模板的网站管理后台
最近接手一个跨境电商平台开发,客户侧重电商网站UI设计,对管理后台要求不高,由我们决定选哪一款后台模板.找来找去,感觉还是H.ui靠谱一些,主要是这个模板清爽,不需要过多选择.其他的流行后台模板也看了 ...
- 使用PDO连接数据库
PDO(PHP数据对象) 是一个轻量级的.具有兼容接口的PHP数据连接拓展,是一个PHP官方的PECL库,随PHP 5.1发布,需要PHP 5的面向对象支持,因而在更早的版本上无法使用.它所提供的数据 ...
- 华为云BigData Pro解读: 鲲鹏云容器助力大数据破茧成蝶
华为云鲲鹏云容器 见证BigData Pro蝶变之旅大数据之路顺应人类科技的进步而诞生,一直顺风顺水,不到20年时间,已渗透到社会生产和人们生活的方方面面,.然而,伴随着信息量的指数级增长,大数据也开 ...