首页 > 技术文章 > Spring生态系列文章 >

SpringCloud极简入门(十三)SpringCloud Bus 消息总线

更新时间:2018-09-13 | 阅读量(1,399)

#### 一.什么是消息总线 在微服务架构中,为了更方便的向微服务实例广播消息,我们通常会构建一个消息中心,让所有的服务实例都连接上来,而该消息中心所发布的消息都会被微服务实例监听和消费,我们把这种机制叫做消息总线(SpringCloud Bus),在总线上的每个服务实例都可以去广播一些让其他服务知道的消息,消息总线可以为微服务做监控,或实现应用之间的通信,和其他的一些管理工作,SpringCloud Bus可选的消息组件包括RabbitMQ,Kafka等,本章节讲述使用 RabbitMQ 作为Spring Cloud的消息组件实现刷新更改微服务的配置文件。 #### 二.为什么要用SpringCloud Bus刷新配置 例如有成十上百个微服务实例,当更改配置时,需要重启多个微服务实例,会非常麻烦。SpringCloud Bus 就能让这个过程变得非常简单,当我们Git仓库中的配置更改不后,只需要某个微服务实例发送一个POST请求,通过消息组件通知其他微服务实例重新获取配置文件,以达到配置的自动刷新效果。实现原理如下: ![image.png](https://upload-images.jianshu.io/upload_images/11046204-0bd9777b92fc3aa9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) #### 三.实现配置刷新 >1.本章节基于第十章“配置中心”的基础上进行修改,给 config-client应用添加SpringCloud Bus相关依赖 spring-cloud-starter-bus-amqp和 spring-boot-starter-actuator ``` org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 Finchley.RELEASE org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin ``` >2.搭建RabbitMQ服务,不会使用 RabbitMQ 的同学请先百度一下,RabbitMQ使用非常简单(体现你们自学能力的时候到了) >3.修改配置文件 bootstrap.properties,增加RabbitMQ连接配置 ``` #分支 spring.cloud.config.label=master #dev开发环境配置文件 dev开发环境配置文件 ,test测试环境,pro正式环境 spring.cloud.config.profile=dev spring.cloud.config.name=config-client #配置服务地址 spring.cloud.config.uri=http://localhost:5555/ #增加 rabbitmq相关配置 spring.rabbitmq.host=localhost #mq连接地址 spring.rabbitmq.port=5672 #mq端口 spring.rabbitmq.username=guest spring.rabbitmq.password=guest spring.cloud.bus.enabled=true #开启 bus spring.cloud.bus.trace.enabled=true management.endpoints.web.exposure.include=bus-refresh #暴露刷新地址,完整访问路径: xxx/actuator/bus-refresh ``` >4.在要属性的配置的配置类上打上 标签@RefreshScope,否则不能自动刷新 ``` @RestController @RefreshScope public class HelloController { @Value("${notify}") //从远程配置中取值 private String notify; @RequestMapping("/hello") public String hello(){ return notify; } } ``` >5.启动 config-server ,再启动 config-client ,你可以从控制台看到 ![image.png](https://upload-images.jianshu.io/upload_images/11046204-7e5c6e624c2b62d6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)说明RabbitMQ配置起效了访问 config-client :http://localhost:7777/hello ,可以看到响应内容为 “You are successful for dev” ,接下来修改 Git中的配置 config-client-dev.properties中的 notify为 : ![image.png](https://upload-images.jianshu.io/upload_images/11046204-8391787190722ceb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) >6.请求配置刷新地址,使用post请求 :http://localhost:7777/actuator/bus-refresh ,如我这里使用了postman进行测试:![image.png](https://upload-images.jianshu.io/upload_images/11046204-1514645b75077e73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ,观察控制台会出现变化,![image.png](https://upload-images.jianshu.io/upload_images/11046204-a0fb0262755dd96b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)说明刷新的新的配置 再次访问http://localhost:7777/hello你可以看到 “You are successful for dev hai bus test” ,已经发生变化,说明我们从 git中获取的配置属性值已经被刷新
叩丁狼学员采访 叩丁狼学员采访
叩丁狼头条 叩丁狼头条
叩丁狼在线课程 叩丁狼在线课程