Spring Cloud 分布式配置中心:Spring Cloud Config
部分内容摘自 Spring Cloud 官方文档中文版
本文源码地址 https://github.com/Wyxwx/springCloud-config
目录
Spring Cloud Config 简介
Spring Cloud Config 为分布式系统中的外部配置提供服务器和客户端的支持。
服务端:分布式配置中心,是一个独立的微服务应用,连接仓库获取配置信息
客户端:微服务架构中的各个微服务应用或基础设施,指定配置中心并获取配置信息。
服务器存储后端的默认实现使用 git,也提供了 SVN 仓库,本地化文件系统的支持。
Spring Cloud Config 基本使用
创建一个空的 Maven 项目
并在其中创建一个名为 config-server 的 Spring Boot Module
选择 Config Server 的依赖
为启动类加上注解 @EnableConfigServer 表示启动 Spring Cloud Config 服务端功能
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
修改配置文件 applica.properties
spring.application.name=config-server
server.port=8888
# git 仓库的路径
spring.cloud.config.server.git.uri=https://github.com/Wyxwx/springCloud-config
# git 仓库下的文件夹位置
spring.cloud.config.server.git.search-paths=configRepo
# 访问 git 仓库的用户名
spring.cloud.config.server.git.username=username
# 访问 git 仓库的密码
spring.cloud.config.server.git.password=password
接下来在 github 上创建一个名为 springCloud-config 的仓库,并在其中添加 configRepo 文件夹
在该文件夹下创建两个配置文件
配置文件内容:
接下来在该 Maven 项目下创建一个名为 config-client 的 Spring Boot Module
引入 Config Client 及 Web 依赖
创建 bootstrap.properties 配置文件
spring.application.name=my
# 若想访问 my.properties 配置文件,将 profile 注释即可
spring.cloud.config.profile=another
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8888/
server.port=8889
客户端访问配置信息的 URL 与配置文件的映射关系:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
application: spring.application.name
profile: spring.cloud.config.profile
label: spring.cloud.config.label(分支名)
使用 bootstrap.properties 而非 application.properties 的原因:
bootstrap.properties 先于 applicaton.properties
bootstrap 用于从本应用 jar 包之外的资源来加载配置信息,默认也不能被本地相同配置覆盖。
接着创建一个 testController
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${tag}")
private String tag;
@RequestMapping("/from")
public String from(){
return this.tag;
}
}
依次启动 config-server 和 config-client
成功获取到 git 仓库的配置
Git 后端
URI 的占位符
config 服务端的配置文件可以利用占位符来实现
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/Wyxwx/springCloud-config/{application}
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
{application}: 应用名,根据客户端的 spring.application.name 来匹配
注意:
{label}: 如果 git 分支和标签名包含 "/",那么 {lable} 参数在 HTTP 的 URL 中应该使用 "(_)" 替代
例如,如果标签为foo/bar
,则替换斜杠将导致标签看起来像foo(_)bar
配置多个仓库
Config Server 支持通过带有通配符的表达式匹配仓库
# 默认仓库位置
spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo
# 仓库 simple
spring.cloud.config.server.git.repos.simple=https://github.com/simple/config-repo
# 仓库 special
spring.cloud.config.server.git.repos.special.pattern=special*/dev*, *special*/dev*
spring.cloud.config.server.git.repos.special.uri=https://github.com/special/config-repo
# 仓库 local
spring.cloud.config.server.git.repos.local.pattern=local*
spring.cloud.config.server.git.repos.local.uri=file:/home/configsvc/config-repo
当 {application}/{profile} 未能匹配到合适的仓库时,将使用默认仓库
配置多个仓库时, 会在启动时直接克隆第一个仓库的配置库,其他配置库只有在请求时才会克隆到本地
安全保护
可以使用如物理网络安全性、OAuth2 承载令牌等来对配置中心实现安全保护。
Spring Security 与 Spring Boot 结合使用十分方便
在 config-server 的 pom.xml 中引入 Spring Security 的依赖包并且在配置文件中设置用户名和密码
此时若是在连接 config-server 的客户端中没有账户名和密码的设置,将会被拦截
config-client 设置账号密码:
注意服务端和客户端账号密码的一致性
spring.cloud.config.username=
spring.cloud.config.password=
加密解密
要想使用加密解密,需要在 JVM 中安装不限长度的 JCE
从Oracle下载“Java加密扩展(JCE)无限强度管理策略文件”,并按照安装说明(实际上将JRE lib / security目录中的2个策略文件替换为您下载的文件)
Config Server可以使用对称(共享)**或非对称**(RSA**对)。非对称选择在安全性方面是更好的,但是使用对称**往往更方便,因为它只是配置的一个属性值。
对称加密
设置**:
encrypt.key=
请求 /encrypt 端点:
localhost:8888/encrypt -d string
得到一串加密后的字符串
接下来请求 /decrypt 端点:
localhost:8888/decrypt -d 加密后字符串
得到 string
相关端点:
/encrypt/status: 查看加密功能状态
/key: 查看**
/encrypt: 加密
/decrypt: 解密
客户端快速失败
在某些情况下,如果服务无法连接到配置服务器,则可能希望启动服务失败。
可以设置引导配置属性 spring.cloud.config.failFast=true
客户端将以异常停止而不加载在连接仓库之前加载的内容。
客户端重试
如果客户端只是由于网络波动而在某个时间点无法连接到配置服务器,这时我们并不希望他异常停止,而是希望可以重新连接
这时需要添加两个依赖到 pom.xml 中
spring-retry
spring-boot-starter-aop
配置文件:
# 开启快速失败
spring.cloud.config.fail-fast=true
# 初始重试间隔时间,默认 1000 毫秒
spring.cloud.config.retry.initial-interval=1000
# 下一间隔的乘数,默认 1.1,即后一次重试间隔是前一次重试间隔的 1.1 倍
spring.cloud.config.retry.multiplier=1.1
# 最大间隔时间,默认 2000 毫秒
spring.cloud.config.retry.max-interval=2000
# 最大重试次数,默认 6 次
spring.cloud.config.retry.max-attempts=6