「这是我参与2022首次更文挑战的第11天,活动详情查看:2022首次更文挑战」
Spring Could Config是什么
SpringCloud Config 为微服务架构中微服务提供了集中化的外部配置支持,配置服务器为各个不同微服务应用的的所有环境提供了一个中心化的外部配置。
如何使用?
Spring Cloud 分为服务端和客户端两部分
服务端也称为分布式配置中心,它是一个独立的微服务应用, 用来连接配置服务器并为客户端提供获取配置信息,加密/ 解密信息等访问接口。
客户端则是通过制定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取好加载配置信息配置服务器默认采用 git 来存储配置信息,这样就有助于缓解配置惊醒版本管理,并且可以通过 git 客户端工具来方便管理和访问配置内容。
Spring Could Config 能干什么
- 集中管理配置文件
- 当配置发生变动,服务不需要重启即可感知配置的并发并应用新的配置
- 不同环境的不同配置,动态化的配置更新,分环境部署比如:/dev/test/prod/beta/release
- 运行期间动态调整配置,不再需要在每个服务器部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
实战
- 服务端 1.pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.配置文件
server:
port: 8011
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/jackgeeks/config.git #github仓库上面的git仓库名字
##搜索目录
search-paths:
- cloud-config
#读取分支
label: master
配置规则
/{name}-{profiles}.yml
/{application}/{profile}[/{label}]
/{lable}-{name}-{profiles}.yml
lable:分支
name : 服务名
profiles : 环境
3.启动类@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4.启动访问http://localhost:8011/application-dev.yml
- 客户端 1.pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.配置文件bootstrap.yml
server:
port: 8012
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 上述3个综合:master分支上config-dev.yml的配置文件被读取
uri: http://localhost:8011 #配置中心地址
3.配置文件读取
@RestController
public class ConfigClientController {
@Value("${config.port}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
4.启动访问http://localhost:8012/configInfo
客户端值动态刷新
手动版
pom中加入actuator
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改Controller 加入@RequestScope注解
@RequestScope
@RestController
public class ConfigController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return this.configInfo;
}
}
修改Github配置文件
发送post请求localhost:8012/actuator/refresh更新配置
自动版
引入Spring Cloud Bus进行解决
Spring Cloud Bus 配合过SpringCloud Config 可以实现配置的动态刷新
Spring Cloud Bus 是用来将分布式系统的节点与轻量级消息系统链接起来的框架,
它整合了Java的事件处理机制和消息中间中间件功能
Spring Cloud Bus 目前支持 RabbitMQ 和Kafka
什么是总线
在微服务架构的系统中,通常会使用 轻量级的消息代理 来构建一个共用的消息主题, 炳然系统同中所有的微服务实例都连接上来,由于该主题中产生的消息会被所有实例监听和消费,所以它成为消息总线。在总线的各个实例,都可以方便地广播一些需要让其他链接在该主题上的实例都知道的消息。
基本原理
Config Clinet 实例会监听MQ中同一个 Topic (默认是 springCloudBus ). 当一个服务刷星数据的时候,会把这个信息放入到 Topic 汇总,这样其他监听同一个 topic 服务就能得到通知,然后更新自身的配置。 服务端pom加入Spring Cloud Bus
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-busamqp</artifactId>
</dependency>
修改配置文件
#rabbit相关配置 15672是web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: 127.0.0.1
port: 5672
username: admin
password: admin
#rabbitmq相关配置,暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: 'bus-refresh' #凡是暴露监控、刷新的都要有actuator依赖,bus-refresh就是actuator的刷新操作
启动服务修改Github配置文件后:
- 手动发送post请求
http://localhost:8011/actuator/bus-refresh更新配置文件 - 配置Github文件,仓库当配置文件修改发送post请求给config服务端