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(七):配置中心 ...
随机推荐
- 【C/C++】之C语言库函数
这个帖子记录一下 C语言 中经常用到的函数库中的函数及其用法. 1.<math.h> math.h是进行数学操作的函数库.使用这个函数库,需要先导入包: #include <math ...
- 《手把手教你》系列练习篇之8-python+ selenium自动化测试 -压台篇(详细教程)
1. 简介 本文是练习篇的最后一篇文章,虽然练习篇的文章到此就要和大家说拜拜了,但是我们的学习之路才刚刚开始.不要停下你的脚步,大步朝前走吧!比你优秀的人还在走着,我们有什么理由停下自己的脚步了,生命 ...
- SMProxy,让你的数据库操作快三倍!
SMProxy GITHUB:https://github.com/louislivi/smproxy 喜欢请star 中文 | English /$$$$$$ /$$ /$$ /$$$$$$$ /$ ...
- HotStuff共识协议详解
1. 前言 HotStuff提出了一个三阶段投票的BFT类共识协议,该协议实现了safety.liveness.responsiveness特性.通过在投票过程中引入门限签名实现了O(n)的消息验证复 ...
- TortoiseGit 设置ssh方式
TortoiseGit使用扩展名为ppk的密钥,而不是ssh-keygen生成的rsa密钥. 也就是说使用 ssh-keygen -t rsa -C "576953565@qq.com& ...
- NSURLSession使用(整理版)
NSURLSession使用 1.NSURLSession诞生于2013年,但其在前几年一直生活在NSURLConnection的阴影下,直到iOS9的出现,NSURLConnection被官方宣布弃 ...
- 初步认知jQuery
jQuery:是JavaScript的一个类库全写JavaScript query write less do more JavaScript查询写的更少做的更多 第一步先导入js文件: < ...
- hdu2546
Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负) ...
- Python3 并发编程小练习
实现基于TCP协议套接字,服务端实现接收客户端的连接并发 # server.py import socket from threading import Thread server = socket. ...
- 【系列专题】JavaScript设计模式 重温系列(9篇全)
JavaScript 设计模式 [JS]164-JavaScript设计模式--单体模式 [JS]165-JavaScript设计模式--工厂模式 [JS]166-JavaScript设计模式--迭代 ...