Spring Cloud Config实现集群配置中心,
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完整内容如下:

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

可以使用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完整内容如下:

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添加配置
management: endpoints: web: exposure: include: "*"
3、在Controller上添加注解@RefreshScope

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。
相关文章
- 暂无相关文章
用户点评