一、为什么要统一管理微服务配置
对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。
微服务的配置管理一般有以下需求:
1.集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的。
2.不同环境不同配置,比如数据源配置在不同环境(开发,生产,测试)中是不同的。
3.运行期间可动态调整。
4.配置修改后可自动更新。
好在Spring Cloud Config已经全部实现了上面几点。
二、Spring Cloud Config简介和使用
2.1原理
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Client 和 Config Server两个部分。原理是所有的配置信息都存储在Config Server,所有的微服务都指向Config Server,
各个微服务启动时都会请求Config Server来获取配置信息,然后缓存到本地以提高性能。
2.2编写Config Server
1.在Git仓库https://gitee.com/StarskyBoy/cloud-config-repo
(可以使用自己的仓库、本地文件系统等)新建几个配置文件,例如:
内容分别为(在测试中可能做了些修改):
profile=profile: dev-1.0-change16
profile=production-1.0
profile=test-1.0
profile=default-1.0
2.在cloud项目中创建cloud-config-server微服务,并添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
3.在启动类添加 @EnableConfigServer注解
4.编写application.yml文件
server:
port: 8050
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
#git地址
uri: https://gitee.com/StarskyBoy/cloud-config-repo
#git用户名
username: ******
#git密码
password: ******
这样就完成了,可以使用端点来获取配置文件,端点与配置文件的映射规则如下:
/{application}/{profile}[/{lable}]
/{application}-{profile}.yml
/{lable}/{application}-{profile}.yml
/{application}-{profile}.properties
/{lable}/{application}-{profile}.properties
{application}表示微服务的名称,{profile}代表环境,{lable}表示Git仓库的分支,默认是master。
本例如果要访问cloud-foo-dev.properties,则可以访问这些URL:
http://localhost:8050/cloud-foo/dev
http://localhost:8050/cloud-foo-dev.properties
http://localhost:8050/cloud-foo-dev.yml
备注:
源码见cloud项目cloud-config-server微服务。
2.3编写Config Client
1.创建cloud-config-client微服务,并添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--健康监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--web模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--使用Spring Cloud Bus自动刷新配置,取消注释-->
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-bus-amqp</artifactId>-->
<!--</dependency>-->
2.编写配置文件bootstrap.yml
server:
port: 8051
spring:
application:
#对应Config Server所获取的配置文件的{application}
name: cloud-foo
cloud:
config:
#指定Config Server的地址,默认是http://127.0.0.1:8888
#本项目,此处端口注意切换
#8050端口是普通Config Server
#8052端口是bus refresh
#8053端口是authenticating
uri: http://192.168.43.171:8053/
#对应Config Server所获取的配置文件的{profile}
profile: dev
#指定Git仓库的分支,对应Config Server所获取的配置文件的{label}
label: master
#启动用户认证,取消注释
# username: midou
# password: midouJava
#使用Spring Cloud Bus自动刷新配置,取消注释
# rabbitmq:
# host: 192.168.43.37
# port: 5672
# username: admin
# password: admin
#关闭安全认证
management:
security:
enabled: false
#值得注意的是,以上属性配置在bootstrap.yml,而不是application.yml中,否则部分配置就不能正常工作。。
4.编写Controller
@RestController
//是否使用/refresh端点刷新配置
//@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String hello() {
return this.profile;
}
}
这里通过注解 @Value("${profile}") 来绑定Git仓库的profile属性。
5.测试
先启动cloud-config-server,再启动cloud-config-client,访问http://localhost:8050/profile即可获得以下结果。
dev-1.0-change16
说明能够正常的获取Git仓库的配置信息。
备注:
源码见cloud项目cloud-config-client微服务。
三、配置文件的手动刷新和自动刷新
3.1通过/refresh端点手动刷新
1.为cloud-config-client微服务添加spring-boot-starter-actuator依赖,如果有了就不添加了。
2.启用Controller类上@RefreshScope注解
3.修改Git仓库中cloud-foo-dev.properties文件的内容,然后发送POST请求到http://localhost:8051/refresh,再访问http://localhost:8051/profile即可获取最新的配置。
备注
源码见cloud项目cloud-config-client微服务。
发POST可用curl命令或者Postman
3.2使用Spring Cloud Bus 实现自动刷新配置
这里是配置Config Server,你也可以配置Config Client,原理是差不多的,cloud项目就不做配置了,具体可参考Config Server实现。
1.首先安装RabbitMQ,安装步骤这里不介绍我的csdn博客里有。
2.复制cloud-config-server微服务修改为cloud-config-server-refresh-cloud-bus微服务,为项目添加以下依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
3.在application.yml中添加以下内容
server:
port: 8052
spring:
application:
name: cloud-config-server-refresh-cloud-bus
cloud:
config:
server:
git:
#git地址
uri: https://gitee.com/StarskyBoy/cloud-config-repo
#git用户名
username: ******
#git密码
password: ******
bus:
trace:
enabled: true
rabbitmq:
host: 192.168.43.37
port: 5672
username: admin
password: admin
management:
security:
enabled: false
4.在cloud-config-client微服务中,添加以下依赖及配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
rabbitmq:
host: 192.168.43.37
port: 5672
username: admin
password: admin
5.cloud-config-client微服务中,controller中的@RefreshScope注解依旧打开。
6.修改Git仓库中cloud-foo-dev.properties文件的内容,然后发送POST请求到http://localhost:8052/bus/refresh,再访问http://localhost:8052/profile即可获取最新的配置。
备注
源码见cloud项目cloud-config-server-refresh-cloud-bus微服务。
发POST可用curl命令或者Postman
四、Config的用户认证
1.复制cloud-config-server微服务修改为cloud-config-server-authenticating微服务。
2.添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.配置application.yml
server:
port: 8053
spring:
application:
name: cloud-config-server-authenticating
cloud:
config:
server:
git:
#git地址
uri: https://gitee.com/StarskyBoy/cloud-config-repo
#git用户名
username: ******
#git密码
password: ******
security:
basic:
enabled: true
user:
name: midou
password: midouJava
4.config client配置
-
方式一
spring:
application:
#对应Config Server所获取的配置文件的{application}
name: cloud-foo
cloud:
config:
uri: http://midou:[email protected]:8053/
-
方式二
spring:
application:
#对应Config Server所获取的配置文件的{application}
name: cloud-foo
cloud:
config:
uri: http://192.168.43.171:8053/
username: midou
password: midouJava
访问config server会弹出认证窗口,具体自己体验一把。
备注
源码见cloud项目cloud-config-server-authenticating微服务。
五、Config Server高可用
六、Config Server配置内容加解密
备注
五、六部分可见google,就不在这里阐述了。
源码获取
1.gitee:https://gitee.com/StarskyBoy
2.github: https://github.com/StarskyBoy
3.csdn:https://blog.csdn.net/StarskyBoy
更多精彩内容请关注我,扫我有惊喜