配置中心介绍
-
配置修改后 实时生效,灰度发布
-
分环境,分 集群管理配置 等进行全面的 集中化 管理
-
分布式系统,部署在N台服务器上
-
集中配置不同环境 和 不同集群配置,修改配置后 将实时动态推送到应用上进行刷新。
-
Spring Cloud Config
-
netflix archaius
-
ctrip apollo
-
disconf
-
Spring Cloud Config 和 Apollo
Spring Cloud Config ctrip apollo (携程 阿波罗) 统一,多维度 管理 需git,数据库 支持 本地配置缓存 无 支持 配置锁 支持 无 配置生效时间 重启,手动刷新 实时 配置定时拉取 无 支持 授权,审核,审计 需git,数据库 界面提供发布历史和回滚按钮 灰度发布 不支持 支持 告警通知 不支持 邮件警告 配置界面 需要git 统一界面
配置中心功能
-
open api
-
业务无关性
-
配置生效监控
-
一致性 k-v 存储中心
-
统一配置实时推送
-
配置灰度更新与回滚
-
配置全局回滚,备份与历史
-
高可用集群
spring cloud config 概述
-
集中化 外部配置的 分布式系统
-
服务端和客户端构成
-
不依注册中心
-
支持多种存储配置信息: jdbc,vault,native,svn,git
-
客户端 向服务端 发起请求
-
服务端 将 git上的文件 克隆到本地一个临时目录(git的本地仓库目录)
-
服务端 读取本地文件 返回给客户端
-
好处:git服务器故障,网络异常,仍然可以正常工作。
入门案例
config server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
@EnableConfigServer
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/zhongzunfa/spring-cloud-config.git
#username:
#password:
search-paths: SC-BOOK-CONFIG #搜索 XX目录下 所有满足条件的配置文件(多个目录逗号隔开)
application:
name: sc-config-git
server:
port: 9090
-
git 仓库下创建文件夹:SC-BOOK-CONFIG
- config-info-dev.yml (client客户端会配置,name: config-info)
- config-info-prod.yml
- config-info-test.yml
- spring-cloud-config.yml
-
读取路径
"{[/{name}-{profiles}.properties],methods=[GET]}" "{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}" "{[/{name}/{profiles:.*[^-].*}],methods=[GET]}" "{[/{name}-{profiles}.json],methods=[GET]}" "{[/{name}/{profiles}/{label:.*}],methods=[GET]}" "{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}" "{[/{label}/{name}-{profiles}.properties],methods=[GET]}" "{[/{label}/{name}-{profiles}.json],methods=[GET]}" "{[/{name}/{profile}/{label}/**],methods=[GET],produces=[application/octet-stream]}" "{[/{name}/{profile}/{label}/**],methods=[GET]}" "{[/{name}/{profile}/**],methods=[GET],params=[useDefaultLabel]}" /{application}/{profile}[/{label}] /{application}-{profile}.yml 和 /{application}-{profile}.properties /{label}/{application}-{profile}.yml 和 /{label}/{application}-{profile}.properties
http://localhost:9090/config-info/dev/master
{
"name": "config-info",
"profiles": ["dev"],
"label": "master",
"version": "c6398b8f9192f7a3dbcb9ef6feb95235967a8f7d",
"state": null,
"propertySources": [{
"name": "https://gitee.com/huaxi21/spring-cloud-config.git/SC-BOOK-CONFIG/config-info-dev.yml",
"source": {
"cn.springcloud.book.config": "I am the git configuration file from dev environment. I will edit it. 88889106-hua-dev环境"
}
}]
}
C:\Users\zhangsan\AppData\Local\Temp\config-repo-2303827764238252992
uri: https://gitee.com/zhongzunfa/spring-cloud-config.git
https://gitee.com/zhongzunfa/spring-cloud-config/blob/master/SC-BOOK-CONFIG/config-info-dev.yml
config client
xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
属性读取和展示control
@Component
@ConfigurationProperties(prefix = "cn.springcloud.book")
public class ConfigInfoProperties {
private String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
@RestController
public class ConfigClientController {
@Autowired
private ConfigInfoProperties configInfoValue;
@RequestMapping("/getConfigInfo")
public String getConfigInfo(){
return configInfoValue.getConfig();
}
}
application.yml
server:
port: 9091
spring:
application:
name: ch11-1-config-client
bootstrap.yml
spring:
cloud:
config:
label: master
uri: http://localhost:9090
name: config-info #哪个文件
profile: dev #文件的分支
#即访问 config-info-dev.yml,如果多个文件,用逗号隔开。
- bootstrap.yml 优先加载。
刷新配置中心
config client 更改后,接口刷新
pom 多加入一个依赖
<!-- 做简单的安全和端点开放 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<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> //客户端必须加入
application增加配置
management.endpoints.web.exposure.include=*
# 包含所有的端点。默认只有:info,health
management.endpoint.health.show-details=always
# 显示详细的信息
添加安全配置
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); //关闭端点的安全校验
}
}
controler 和 properties类增加注解
@RefreshScope
@RestController
@RequestMapping("configConsumer")
public class ConfigClientController {
@Autowired
private ConfigInfoProperties configInfoValue;
@RequestMapping("/getConfigInfo")
public String getConfigInfo(){
return configInfoValue.getConfig();
}
}
@Component
@RefreshScope
public class ConfigInfoProperties {
@Value("${cn.springcloud.book.config}")
private String config;
public String getConfig() {
return config;
}
public void setConfig(String config) {
this.config = config;
}
}
- RefreshScope 延迟加载,只有在第一次访问时才会被初始化。下次访问会 创建一个新的对象。
测试
-
不配置 security 也可以,全部默认。
-
不加RefreshScope 也可以,一定得请求刷新的接口。
-
改过git里的配置后,请求这个接口即可。
post http://localhost:9091/actuator/refresh
如果有变动,返回为:
[
"config.client.version",
"cn.springcloud.book.config"
]
Spring Cloud Bus 热刷新
- config-server通过 git pull 获取到git 上的配置
- 更新配置(git)
- git的 web hook推送
- config server 开始 publish Message
- notify 通知给 客户端 (利用bus)
- 客户端重新加载 server
引入依赖(公共)
//web 和 actuator
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
config server
//除了上面的
还有: config-server 必须引用
spring-boot-starter-security 做简单的端点开放
编写类
@EnableConfigServer 必须有
//同样的配置
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
application.properties
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
application.yml
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/zhongzunfa/spring-cloud-config.git
#username:
#password:
search-paths: SC-BOOK-CONFIG
bus:
trace:
enabled: true
application:
name: ch11-3-config-server-bus
## 配置rabbitMQ 信息
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
server:
port: 9090
config client
config-client 必引入依赖
start-security 安全和端点开放
controller 依然加
@RefreshScope
security配置相同
bootstrap.yml
spring:
cloud:
config:
label: master
uri: http://localhost:9090
name: config-info
profile: dev
application.yml
application.properties 相同
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
server:
port: 9095
spring:
application:
name: ch11-3-config-client-bus-refresh
## 配置rabbitMQ 信息
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
bus:
trace:
enabled: true #多配置的,自动刷新
测试
- git 修改后,执行端点
http://localhost:9090/actuator/bus-refresh
-
bus-refresh?destination=** 刷新所有客户端
-
使用码云,settings ,找到 webHooks,add 添加规则