除了使用 git 作为配置文件的管理中心外,也可以使用关系型数据库、非关系型数据库实现配置中心,以及配置中心的扩展。包括:客户端自动刷新、客户端回退、安全认证、客户端高可用、服务端高可用等。
git 的版 config 有多种配置:
uri 占位符模式匹配多残酷路径搜索占位符源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-config/spring-cloud-placeholder
公共依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>Spring Cloud Config Server 支持占位符的使用,支持 {application}、{profile}、{label},这样的话就可以在配置 uri 的时候,通过占位符使用应用名称来区分应用对应的仓库进行使用。
*** config server ***
spring: cloud: config: server: git: uri: https://gitee.com/laiyy0728/{application} # {application} 是匹配符,匹配项目名称 search-paths: config-simple application: name: spring-cloud-placeholder-server server: port: 9090*** config client ***
bootstrap.yml
spring: cloud: config: label: master uri: http://localhost:9090 name: config-repo profile: dev使用 {application} 时,需要注意,在 config client 中配置的 name,既是 config 管理中心的 git 名称,又是需要匹配的配置文件名称。即:远程的 config git 管理中心地址为:https://gitee.com/laiyy0728/config-repo ,在仓库中 config-simple 文件夹下,必须有一个 config-simple.yml 配置文件。否则 config client 会找不到配置文件。
config server
spring: profiles: active: native # 本地配置仓库,在测试本地配置仓库之前,需要注释掉这一行 cloud: config: server: git: uri: https://gitee.com/laiyy0728/config-repo search-paths: config-simple repos: simple: https://gitee.com/laiyy0728/simple special: pattern: special*/dev*,*special*/dev* uri: https://gitee.com/laiyy0728/special native: search-locations: C:/Users/laiyy/AppData/Local/Temp/config-simple # 本地配置仓库路径 application: name: spring-cloud-placeholder-server server: port: 9090架构图:
config server mysql
公共依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>其余配置、config client 与之前一致即可。
启动 config server、config client,可以看到,config server 打印日志如下:
... Executing prepared SQL query Executing prepared SQL statement [SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE application = ? AND profile = ? AND lable = ?] Setting SQL statement parameter value: column index 1, parameter value [config-simple], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 2, parameter value [dev], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 3, parameter value [master], value class [java.lang.String], SQL type unknown Executing prepared SQL query Executing prepared SQL statement [SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE application = ? AND profile = ? AND lable = ?] Setting SQL statement parameter value: column index 1, parameter value [config-simple], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 2, parameter value [default], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 3, parameter value [master], value class [java.lang.String], SQL type unknown Executing prepared SQL query Executing prepared SQL statement [SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE application = ? AND profile = ? AND lable = ?] Setting SQL statement parameter value: column index 1, parameter value [application], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 2, parameter value [dev], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 3, parameter value [master], value class [java.lang.String], SQL type unknown Executing prepared SQL query Executing prepared SQL statement [SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE application = ? AND profile = ? AND lable = ?] Setting SQL statement parameter value: column index 1, parameter value [application], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 2, parameter value [default], value class [java.lang.String], SQL type unknown Setting SQL statement parameter value: column index 3, parameter value [master], value class [java.lang.String], SQL type unknown ...访问 http://localhost:9091/get-config-info ,返回数据如下:
spring cloud config mysql
以 mongodb 为例,需要 spring cloud config server mongodb 依赖,github 地址:https://github.com/spring-cloud-incubator/spring-cloud-config-server-mongodb
collection 名称:springcloud
数据:
{ "label": "master", "profile": "prod", "source": { "com": { "laiyy": { "gitee": { "config": "I am the mongdb configuration file from dev environment. I will edit." } } } } }config client 与之前的一致
访问 http://localhost:9091/get-config-info ,返回值为:
mongo result
作者:laiyy0728 链接:https://www.jianshu.com/p/b69450259416 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。