在上一篇中,当一个配置中心的客户端启动之后,它所引用的值就无法改变了,但是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系列(七):消息总线的更多相关文章

  1. spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.config 简介 spring cloud config 分为服务端 ...

  2. Spring Cloud 系列之 Stream 消息驱动(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Stream 消息驱动(一) 本篇文章讲解 Stream 如何实现消息分组和消息分区. 消息分组 如果有多个消息消费者 ...

  3. Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】

    Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-24 |  Spring Cloud Confi ...

  4. Spring Cloud系列(二) 介绍

    Spring Cloud系列(一) 介绍 Spring Cloud是基于Spring Boot实现的微服务架构开发工具.它为微服务架构中涉及的配置管理.服务治理.断路器.智能路由.微代理.控制总线.全 ...

  5. Spring Cloud 系列之 Consul 配置中心

    前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...

  6. Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  7. Spring Cloud 系列之 Spring Cloud Stream

    Spring Cloud Stream 是消息中间件组件,它集成了 kafka 和 rabbitmq .本篇文章以 Rabbit MQ 为消息中间件系统为基础,介绍 Spring Cloud Stre ...

  8. Spring Cloud 系列之 Sleuth 链路追踪(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Sleuth 链路追踪(一) 本篇文章讲解 Sleuth 基于 Zipkin 存储链路追踪数据至 MySQL,Elas ...

  9. spring cloud系列教程第一篇-介绍

    spring cloud系列教程第一篇-介绍 前言: 现在Java招聘中最常见的是会微服务开发,微服务已经在国内火了几年了,而且也成了趋势了.那么,微服务只是指spring boot吗?当然不是了,微 ...

随机推荐

  1. POJ2391 Floyd+离散化+二分+DINIC

    题意:       有n个猪圈,每个猪圈里面都有一定数量的猪(可能大于当前猪圈的数量),每个猪圈都有自己的容量,猪圈与猪圈之间给出了距离,然后突然下雨了,问多久之后所有的猪都能进圈. 思路:     ...

  2. 表单模块 layui-form

    使用 layui针对各种表单元素做了比较全面的Ui支持,在Ui渲染只要求一点.,在表单体所在父元素加上class="layui-form" 监听事件 提交按钮监听,注意需要加·la ...

  3. 自带的 print 函数居然会报错?

    前言 最近用 Python 写了几个简单的脚本来处理一些数据,因为只是简单功能所以我就直接使用 print 来打印日志. 任务运行时偶尔会出现一些异常: 因为我在不同地方都有打印日志,导致每次报错的地 ...

  4. 技术博客:Azure Functions + Azure Storage 开发

    Azure GitHub wiki 同步发布 传送门 Azure Functions 通过 Functions(一个事件驱动型无服务器计算平台,还可以解决复杂的业务流程问题)更加高效地进行开发.在本地 ...

  5. Go 函数详解

    一.函数基础 函数由函数声明关键字 func.函数名.参数列表.返回列表.函数体组成 函数是一种类型.函数类型变量可以像其他类型变量一样使用,可以作为其他函数的参数或返回值,也可以直接调用执行 函数名 ...

  6. [re模块、json&pickle模块]

    [re模块.json&pickle模块] re模块 什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则 ...

  7. 透过“锁”事看InnoDB对并发的处理?

    一. 并发场景下的问题 相对于串行处理方式,并发的事务处理可显著提升数据库的事务吞吐量.提高资源利用率.在MySQL实际应用中,根据场景的不同,可以分为以下几类: 读读并发 读写并发 写写并发 在这些 ...

  8. node.js的包加载机制

    加载一个模块 require('moduleName'); 现在核心模块中加载,如果核心模块中没有,那么就去node_modules目录下去找,核心模块的优先级最高. 如果加载模块式省略了文件的后缀名 ...

  9. lsblk         查看分区和磁盘

    lsblk                                                                             查看分区和磁盘 df -h     ...

  10. 013.Kubernetes认证授权

    一 Kubernetes认证系统介绍 1.1 访问控制 Kubernetes API的每个请求都会经过多阶段的访问控制之后才会被接受,这包括认证.授权以及准入控制(Admission Control) ...