Spring Cloud系列(七):消息总线
在上一篇中,当一个配置中心的客户端启动之后,它所引用的值就无法改变了,但是Spring Cloud 提供了一种手段去解决了这个问题——Spring Cloud Bus。
一、Spring Cloud Bus
先贴出官方文档对消息总线的介绍
Spring Cloud Bus links the nodes of a distributed system with a lightweight message broker. This broker can then be used to broadcast state changes (such as configuration changes) or other management instructions. A key idea is that the bus is like a distributed actuator for a Spring Boot application that is scaled out. However, it can also be used as a communication channel between apps.
内容来源于:https://cloud.spring.io/spring-cloud-bus/reference/html/
大概意思就是Spring Cloud Bus通过一个轻量级的消息代理来连接分布式系统的节点,这个代理可以被用作广播状态的改变(如配置变化)或其他消息指令。其核心思想是通过分布式的启动器对Spring Boot应用进行扩展。它还可以用作应用之间的通讯通道。
Spring Cloud Bus依靠消息队列来发送消息,常用是有RabbitMQ和Kafka,这里我们只介绍RabbitMQ的方式。
二、安装RabbitMQ
RabbitMQ依赖ErLang,如果你的机器还没有安装ErLang需要先安装,或者在安装RanbbitMQ时也会先检查本地的ErLang环境,如果没有即会引导你去安装。在windows上这两个都是傻瓜式安装,这里就不再多说,可以自行搜索教程。安装完成后会自己作为服务启动。

三、添加依赖
我们依然使用之前的项目,在config-client中添加bus的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
四、修改配置
在config-server的application.properties中添加以下配置
management.endpoints.web.exposure.include=bus-refresh
在config-client的application.properties中添加以下配置
management.endpoints.web.exposure.include=bus-refresh
在config-client的bootstrap.properties中添加以下配置
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
继续在config-client中的Controller类加上@RefreshScope注解。
五、启动和测试
启动注册中心、config-server和config-client,我们先访问http://localhost:8001/getFoo,这时返回的值是bar;
接着我们把远程仓库的foo值改为barrrrr,然后访问http://localhost:8001/actuator/bus-refresh进行刷新,但是如果直接在浏览器访问会报异常
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
因为actuator的这个端点不支持GET方法,这时我们可以用curl命令发送POST请求
curl http://localhost:8001/actuator/bus-refresh -X POST
这时在服务的控制台会输出这个日志
Received remote refresh request. Keys refreshed [config.client.version, foo]
再次访问http://localhost:8001/getFoo可以看到返回的值变成了barrrrr。
这是一个客户端的情况,我们也可以通过参数destination来刷新指定的服务,如http://localhost:8001/actuator/bus-refresh?destination=service-id:*就是刷新所有应用名为service-id的服务,不论ip和端口。
六、总结
消息总线的使用就介绍到这里。
源码已经上传到github:https://github.com/spareyaya/spring-cloud-demo/tree/master/chapter7
Spring Cloud系列(七):消息总线的更多相关文章
- spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.config 简介 spring cloud config 分为服务端 ...
- Spring Cloud 系列之 Stream 消息驱动(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Stream 消息驱动(一) 本篇文章讲解 Stream 如何实现消息分组和消息分区. 消息分组 如果有多个消息消费者 ...
- Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】
Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-24 | Spring Cloud Confi ...
- Spring Cloud系列(二) 介绍
Spring Cloud系列(一) 介绍 Spring Cloud是基于Spring Boot实现的微服务架构开发工具.它为微服务架构中涉及的配置管理.服务治理.断路器.智能路由.微代理.控制总线.全 ...
- Spring Cloud 系列之 Consul 配置中心
前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...
- Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇
Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...
- Spring Cloud 系列之 Spring Cloud Stream
Spring Cloud Stream 是消息中间件组件,它集成了 kafka 和 rabbitmq .本篇文章以 Rabbit MQ 为消息中间件系统为基础,介绍 Spring Cloud Stre ...
- Spring Cloud 系列之 Sleuth 链路追踪(二)
本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Sleuth 链路追踪(一) 本篇文章讲解 Sleuth 基于 Zipkin 存储链路追踪数据至 MySQL,Elas ...
- spring cloud系列教程第一篇-介绍
spring cloud系列教程第一篇-介绍 前言: 现在Java招聘中最常见的是会微服务开发,微服务已经在国内火了几年了,而且也成了趋势了.那么,微服务只是指spring boot吗?当然不是了,微 ...
随机推荐
- POJ3277 线段树段更新,点询问+二分离散化+暴力
题意: x轴上有一些矩形,问你这些矩形覆盖的面积和是多少. 思路: 首先范围很大,n很小,果断离散化,然后我们就是求出任意区间的最大值作为当前区间的高,最后在算一遍答案就行了, ...
- POJ3070矩阵快速幂简单题
题意: 求斐波那契后四位,n <= 1,000,000,000. 思路: 简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...
- POJ1789简单小生成树
题意: 给你一些车牌号,然后另一两个车牌号之间的权值就是这两个字符串之间相同位置不同字母的个数,然后求最小生成树. 思路: 裸题,不解释了. #include<stdio ...
- hdu3706基础的单调队列
题意: 解释题意不如直接把这个题粘贴过来,因为题目很短题意很容易懂. Give you three integers n, A and B. Then we define Si = Ai mod B ...
- 依赖倒置原则(Dependence Inversion Principle)
目录 背景 说明 例子 "倒置"的解释 总结 参考资料 背景 这几天组内的人一起学习DDD,里面再次提到了依赖倒置原则,在这学习过程中,大家又讨论了一下依赖倒置原则. 说明 采用依 ...
- Blazor实现未登录重定向到登录页的方法
今天研究了一下blazor,发现他默认启动就是类似于后台管理系统的界面,看到这个页面我就想给他写个登录,有登录就涉及到未登录重定向的问题,但是我没有找到blazor全局路由的设置,知道的老哥可以告诉我 ...
- 关于有符号数和无符号数的转换 - C/C++
转载自:http://www.94cto.com/index/Article/content/id/59973.html 1.引例: 今天在做了一道关于有符号数和无符号数相互转换及其左移/右移的问题, ...
- 发现数据结构与算法之美的第n次重新学习 ——— 初遇数据结构与算法(了解)
你的数据结构怎么学的?提起数据结构,计算机与软件,it行业内无人不知,无人不晓.但是,当你真正的去通过数据结构与算法内容去实践内容时,真的能联系起来吗?那肯定的 不管是考研还是做项目,数据结构都是必学 ...
- 25.Qt Quick QML-500行代码实现"合成大西瓜游戏"
"合成大西瓜"这个游戏在年前很火热,还上过微博热搜,最近便玩了一阵还挺有意思的,所以研究了一下小球碰撞原理,自己亲自手写碰撞算法来实现一个合成大西瓜游戏.并支持任意大小布局,你想玩 ...
- sosreport命令 然后diff 正常的操作系统例如centos
nux学习笔记:有用的linux命令 发表于 2018-06-25 | 分类于 linux| 字数统计: 1,269 | 阅读时长 ≈ 6 写在前面 这着笔记,整理一些网上搜集到有用的linu ...