Spring Cloud Config & Spring Cloud Bus
发布日期:2021-06-29 11:14:43 浏览次数:2 分类:技术文章

本文共 3286 字,大约阅读时间需要 10 分钟。

Spring Cloud Config 简介

 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

 在 Spring Cloud 中,有分布式配置中心组件 spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。在 spring cloud config 组件中,分两个角色,一是 config server,二是 config client。
 Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用 SVN 存储,或者是本地文件存储。
 Config Client 是 Config Server的客户端,用于操作存储在 Config Server 中的配置内容。微服务在启动时会请求 Config Server 获取配置文件的内容,请求到后再启动容器。
 具体运行模式,如下图:
在这里插入图片描述
 在请求到具体的微服务时,微服务会先请求 Config Server 获取配置文件的内容,然后将配置文件中的内容嵌入到代码中,再进行编译。

配置服务端

1)将配置文件上传到码云

 使用GitHub时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况。如果我们希望体验Git飞一般的速度,可以使用国内的Git托管服务——码云(gitee.com)。
 和GitHub相比,码云也提供免费的Git仓库。此外,还集成了代码质量检测、项目演示等功能。对于团队协作开发,码云还提供了项目管理、代码托管、文档管理的服务。
① 首先要注册一个账户
② 创建一个仓库,用来保存各个微服务的配置文件
在这里插入图片描述
③ 上传配置文件
文件命名规则:{application}-{profile}.yml或{application}-{profile}.properties。application为应用名称,profile指的开发环境(用于区分开发环境,测试环境、生产环境等)
在这里插入图片描述
④ 复制 git 地址,备用
在这里插入图片描述
2)配置 Config Server
① 引入依赖

org.springframework.cloud
spring-cloud-config-server

② 创建启动类

@EnableConfigServer@SpringBootApplicationpublic class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class); }}

③ 编写配置文件

server:  port: 12000spring:  application:    name: tensquare-config  cloud:    config:      server:        git:          uri: https://gitee.com/vijayZhang/tensquare_dev.git

uri 就是存放配置文件的仓库地址

④ 测试
浏览器测试:http://localhost:12000/base-dev.yml 可以看到配置内容

配置客户端

1)引入依赖

org.springframework.cloud
spring-cloud-starter-config

2)添加bootstrap.yml ,删除application.yml

因为 application.yml 已经上传到码云了,就不必再在本地存留了。

spring:  cloud:    config:      name: base      profile: dev      label: master      uri: http://127.0.0.1:12000  #config Server的地址

Spring Cloud Bus

为什么要用 Spring Cloud Bus

如果我们更新码云中的配置文件,那客户端工程是否可以及时接受新的配置信息呢?我们现在来做有一个测试,修改一下码云中的配置文件中mysql的端口 ,然后测试 http://localhost:9001/label 数据依然可以查询出来,证明修改服务器中的配置并没有立刻更新到工程,只有重新启动程序才会读取配置。 那我们如果想在不重启微服务的情况下更新配置如何来实现呢? 我们使用 Spring Cloud Bus 来实现配置的自动更新。

配置服务端

修改 Config Server 工程的 pom.xml,加入依赖

org.springframework.cloud
spring-cloud-bus
org.springframework.cloud
spring-cloud-stream-binder-rabbit

修改 Config Server 的 application.yml

server:  port: 12000spring:  application:    name: tensquare-config  cloud:    config:      server:        git:          uri: https://gitee.com/vijayZhang/tensquare_dev.git  # 以下是新增的内容  rabbitmq:    host: 192.168.227.129management:  endpoints:    web:      exposure:        include: bus-refresh #暴露触发消息总线的地址
配置客户端

1)进入依赖

org.springframework.cloud
spring-cloud-bus
org.springframework.cloud
spring-cloud-stream-binder-rabbit
org.springframework.boot
spring-boot-starter-actuator

2)在码云上的配置文件中加入 rabbitMQ 的地址

在这里插入图片描述
3)测试
使用 postman 测试:,Method:post
如果没有问题的话,那么客户端将重新编译

运行原理

在这里插入图片描述其实还是运用了消息中间件,只要修改了配置文件的内容,就需要手动向消息中间件发送一个消息,地址就是: http://127.0.0.1:12000/actuator/bus-refresh ,而微服务一直在监听消息队列,只不过这些工作 Spring Cloud Bus 都帮我们做了。当微服务监听到配置文件有改动的时候,就可以在不重启微服务的情况下,更新配置文件,让其立刻生效,无需停掉服务再手动编译

自定义配置的读取

如果只是上面那样,那么只有框架中约定好的配置信息可以自动更新,而自定义的配置信息,还是没有更新(如 上面配置文件中的 ip)

解决方法: 只需要在 controller 上加上一个注解
在这里插入图片描述
这样自定义的配置也可以自动更新了

转载地址:https://blog.csdn.net/zwj_jyzl/article/details/104250294 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:微服务容器部署与 jenkins 持续集成
下一篇:Spring Cloud Zuul

发表评论

最新留言

很好
[***.229.124.182]2024年04月13日 09时32分48秒