暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringCloud学习笔记——配置中心使用详解

CodeWu 2021-06-04
473

Git仓库配置详解

占位符支持

Config Server的占位符支持{application}、{profile}和{label}。

spring:
cloud:
config:
fail-fast: false
server:
git:
uri: https://gitee.com/xxxxxx/{application}
username: xxxxxx
password: xxxxxx
application:
name: config-server
server:
port: 4001
复制

使用上述配置方式,即可支持一个应用对应一个Git仓库。同理,也可支持一个profile对应一个Git仓库。

搜索目录

很多场景下, 我们把需要加载的配置文件信息存放在配置中心不同目录下,可以通过search-path对搜索目录进行配置(支持使用占位符)

模式匹配通常指带有通配符的{application}/{profile}名称的列表。如果不匹配{application}/{profile}中的任何模式,则使用spring.cloud.config.server.uri中定义的URI信息。

spring:
cloud:
config:
server:
git:
uri: gitUri
username: 用户名
password: 密码
search-paths: config,*version2
#应用名称
application:
name: config-server
#配置中心应用端口
server:
port: 4001
复制

通过上述配置信息,会从配置中心根目录、config目录、以version2为后缀的目录中查找配置文件。

启动时加载配置文件

默认情况下,只有当用户调用配置中心客户端提供的接口,配置中心客户端才通过配置中心相关配置信息加载指定的配置文件,也可让配置中心启动时根据相关配置信息加载指定的配置文件。

spring:
cloud:
config:
server:
git:
uri: gitUri
username: 用户名
password: 密码
repos:
config-a:
clone-on-start: true
uri: configURI
#应用名称
application:
name: config-server
#配置中心应用端口
server:
port: 4001
复制

通过将clone-on-start参数配置为true,即可让配置中心应用启动时加载uri指定的仓库,也可使用

spring:
cloud:
config:
server:
git:
clone-on-start: true
uri: gitURI
复制
进行全局配置。
复制

快速识别无效配置

将以下包的日志级别设置为DEBUG,可打印配置中心请求Git仓库的细节。通过这样的方式,可以快速识别无效仓库配置。

logging:
level:
org.springframework.cloud: DEBUG
org.springframework.boot: DEBUG
复制

健康状况指示器

Config Server自带了一个健康状况指示器,用于检查所配置的EnvironmentRepository是否正常工作。可使用Config Server的/health端点查询当前健康状态。默认情况下,健康指示器向EnvironmentRepository请求的{application}是app,{profile}和{label}是EnvironmentRepository实现的默认值(对于Git,profile是default,label是master)。

同样也可以自定义健康状况指示器的配置,从而检查更多的配置参数信息。例如:

server:
port: 8080
spring:
application:
name: microservice-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/99967706/spring-cloud-config-repo/ # 配置Git仓库的地址
username: # Git仓库的账号
password: # Git仓库的密码
health:
repositories:
service-name: #服务名称随意
label: config-version2.0 #{label}
name: config #{application}
profiles: dev #{profile}
#enabled: false #禁用健康状况指示器
复制

配置内容加解密

上述配置信息是在Git仓库中以明文的方式进行存储,某些情况下需要对敏感信息(数据库账号、密码)进行加密存储。

配置中心为配置内容的加密与解密提供了支持。

JCE安装

从https://www.oracle.com/java/technologies/javase-jce8-downloads.html下载文件,解压后替换JAVA_HOME/jre/lib/security/policy下目录中的文件。

配置中心加解密端点

端点
描述
/encrypt/statusGET请求,查看JCE状态
/key
GET请求,对密钥信息加密
/encrypt
加密请求体
/decrypt
解密请求体

配置中心提供了加密与解密的端点,分别是/encrypt和/decrypt。可以通过在配置中心添加bootstrap.yml配置文件,配置相应内容(需要加密的明文内容)提供加密功能:

encrypt:
key: xxxxxx
复制

通过curl http://localhost:4001/encrypt/status,查看JCE状态,响应结果如下:

{
"status": "OK"
}
复制

通过curl http://localhost:4001/encrypt -d wukl对明文wukl执行加密操作,响应结果如下:

4307697d622c270572127479874a8afd11ab75e0cb572574de7af8439268fcfc
复制

通过curl http://localhost:4001/decrypt -d 加密结果 执行解密操作,响应结果如下:


wukl
复制

配置文件存储加密内容

对称加密

配置文件中的数据库连接密码信息通常不应使用明文的形式进行存储,需要对连接密码信息进行加密。在配置中心仓库中创建config-db.yml配置文件,保存数据库连接信息,通过curl http://localhost:4001/encrypt -d xxxxxx 对明文密码信息进行加密,加密结果替换config-db.yml中对应的密码配置内容({cipher}后追加加密结果)。

jdbc:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: '{cipher}78af74d4059933ddeded5520504630e9845695c43df82293e53d25f9edbdc7ea'
复制

如果是yml文件需要将密码配置内容加单引号,properties文件无需单引号。

访问http://localhost:4001/config/db,响应结果如下

{
"name":"config",
"profiles":[
"db"
],
"label":null,
"version":"921652d5d40ebb64b4fc23949be73b0bc6e70059",
"state":null,
"propertySources":[
{
"name":"https://gitee.com/99967706/spring-cloud-config-repo/config-db.yml",
"source":{
"jdbc.driver":"com.mysql.jdbc.Driver",
"jdbc.url":"jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
"jdbc.username":"root",
"jdbc.password":"WU830118"
}
}
]
}
复制

说明配置中心能够自动解密加密内容,如果需要直接返回密文本身,可进行如下配置:

spring.cloud.config.server.encrypt.enabled:false
复制

非对称加密

使用JDK自带非对称加密工具对明文密码进行非对称加密。

keytool -genkeypair -alias testkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass 123456 -keystore dbconfig.jks -storepass WU830118
复制
参数名称
参数描述
-alias
别名
-keyalg
加密算法
-keypass
密钥获取使用密码
-keystore
jks文件存放地址及文件名称
-storepass
密钥数据库访问密码

命令行当前目录下会生成dbconfig.jks文件。将dbconfig.jks文件复制到配置中心resource目录下,修改bootstrap.yml配置文件,内容如下

encrypt:
keyStore:
location: 文件存放路径
password: 与storepass参数内容一致
alias: 与alias参数内容一致
secret: 与keypass参数内容一致
复制

通过curl http://localhost:4001/encrypt -d WKL830118对密码进行加密,响应结果如下:

AQAxka+afEE5fBKecJjIvlbvw38dYMcxPtCjNf/2aLGa4fihmzGok0LWt7PALl7FaHqKaEfzpxW8CJS+xFnIQf/iAAu82MVw8oSTDG0w3RSfE9LvQt/BSNA5D1A995UGtPf/A1a+UY4/MXqMBwIaVVt2eC5tPM1rb5eBXlhFHxkIlIrNIJJSD+9MVjfuqhiG6ctDFBA05Gr/0lGQP2ZJDxfdXha+QUQgpKnGZRam5VJMEnyZyf/YRXm20JGrAExwfJmRgnvyfCPpcfIty6o/I5VTq2FywvlFYC49/r89BGwcmiA9vZr5u24/Qu4O7ajpea5wagy2LRYB5pjnuIz8H5UUlumso8cZHrTToJqnwZZAJXJDP+R57OIxTAL/UFq3KL8=
复制

端点刷新配置

很多场景下,需要在运行期间动态更新配置,如果配置发生了修改,需要对多个服务实例进行重启。

Refresh端点刷新

在配置中心客户端添加项目依赖

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制

该项目依赖包含/refresh端点,用于配置刷新。

在控制器上添加@RefreshScope注解,即可在配置更新后,手动刷新/refresh端点,及时获取更新的配置信息。

Spring Cloud Bus自动刷新

上一个环节对手动通过/refresh端点刷新配置进行了讨论,如果所有服务实例都通过手动刷新端点的方式进行配置更新,需要很大的工作量。因此,需要引入Spring Cloud Bus实现配置的自动刷新。

Spring Cloud Bus使用轻量级的消息代理(RabbitMQ、Kafka等)连接分布式节点,用于广播配置文件内容的更改或服务的监控管理。

项目依赖

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
复制

配置文件(bootstrap.yml)

spring:
application:
name: config
cloud:
config:
label: master
fail-fast: false
profile: default
uri: http://localhost:4001/
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
server:
port: 4002
复制

通过手动刷新/actuator/bus-refresh端点,所有配置中心客户端均可以获取更新后的配置信息内容。

局部刷新

在某些特殊情况(灰度发布)下,我们只想更新部分服务实例配置信息,可以通过/actuator/bus-refresh端点的destination参数来指定需要刷新配置的服务实例。如/actuator/bus-refresh?destination=customers:9000,其中customers:9000为ApplicationContextId(通常为服务实例名:端口号),此处将刷新服务名为customers,端口为9000的服务实例配置信息。




文章转载自CodeWu,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论