Spring Cloud Bus将分布式系统的节点与轻量级消息代理链接。可以用于通知状态更改(例如配置更改)或其他管理指令。一个关键的地方是,Bus就像一个分布式执行器,用于扩展的Spring Boot应用程序,同时还可以用作应用程序之间的通信通道…

- Bus

Spring Cloud Bus是通过添加Spring Boot自动配置(Auto Configuration),如果它在class path中被检测到,则可以工作。所有启用Spring Cloud Bus的都需要添加spring-cloud-starter-bus-amqpspring-cloud-starter-bus-kafka依赖管理,并且Spring Cloud负责其余部分。确保代理(RabbitMQ或Kafka)可用和配置:在本地主机上运行,​​您不应该做任何事情,但如果您远程运行使用Spring Cloud连接器或Spring Boot定义经纪人凭据的约定,例如Rabbit

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_bus

- RabbitMQ搭建

RabbitMQ搭建:http://blog.battcn.com/2017/08/20/linux/linux-centos7-ribbitmq/

- 开始

1.拷贝battcn-cloud-config代码进行改造,其中battcn-config-server不需要动

2.pom.xml 导入AMQP包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>

3.添加@RefreshScope注解

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RefreshScope
public class TestController { @Value("${order.name}")
String orderName; @RequestMapping("/test")
public String test() {
return "client ====>>> " + orderName;
}
}

4.给battcn-config-client配置rabbitmq信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server:
port: 9001
spring:
application:
name: battcn-config-client
cloud:
config:
name: config-server
profile: order-default
uri: http://localhost:9000
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true
rabbitmq:
addresses: 192.168.31.86
port: 5672
username: guest
password: guest
management:
security:
enabled: false

- 测试

1.启动consul

2.启动battcn-config-serverbattcn-config-client

3.访问:http://localhost:9001/test 会看到如下内容,表示服正常

1
client ====>>> My Name's Order Service,Are you Afraid?

4.修改battcn-config-serverconfig-server-order-default.yml文件的内容

1
2
order:
name: My Name's Order Service,Are you Afraid?123

5.只重启battcn-config-server 同时发送POST请求:http://localhost:9001/bus/refresh 通知服务刷新配置

6.再次访问:http://localhost:9001/test 会看到如下内容,表示服正常

1
client ====>>> My Name's Order Service,Are you Afraid?123

battcn-config-server日志

1
2
3
4
2017-08-21 20:45:09.048  INFO 11420 --- [nio-9000-exec-3] o.s.cloud.commons.util.InetUtils         : Cannot determine local hostname
2017-08-21 20:45:09.074 INFO 11420 --- [nio-9000-exec-3] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e6721d9: startup date [Mon Aug 21 20:45:09 CST 2017]; root of context hierarchy
2017-08-21 20:45:09.080 INFO 11420 --- [nio-9000-exec-3] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: classpath:/config-server-order-default.yml
2017-08-21 20:45:09.080 INFO 11420 --- [nio-9000-exec-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e6721d9: startup date [Mon Aug 21 20:45:09 CST 2017]; root of context hierarchy

battcn-config-client日志

1
2
3
4
5
6
7
8
9
10
11
12
13
2017-08-21 20:45:54.896  INFO 16768 --- [nio-9001-exec-8] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7dc4513: startup date [Mon Aug 21 20:45:54 CST 2017]; root of context hierarchy
2017-08-21 20:45:54.906 INFO 16768 --- [nio-9001-exec-8] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-08-21 20:45:54.907 INFO 16768 --- [nio-9001-exec-8] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$6f70a08a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-08-21 20:45:55.973 INFO 16768 --- [nio-9001-exec-8] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2017-08-21 20:45:55.994 INFO 16768 --- [nio-9001-exec-8] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:9000
2017-08-21 20:45:57.093 INFO 16768 --- [nio-9001-exec-8] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-server, profiles=[order-default], label=null, version=null, state=null
2017-08-21 20:45:57.093 INFO 16768 --- [nio-9001-exec-8] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='classpath:/config-server-order-default.yml']]]
2017-08-21 20:45:57.094 INFO 16768 --- [nio-9001-exec-8] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2017-08-21 20:45:57.095 INFO 16768 --- [nio-9001-exec-8] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@676107bb: startup date [Mon Aug 21 20:45:57 CST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7dc4513
2017-08-21 20:45:57.096 INFO 16768 --- [nio-9001-exec-8] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-08-21 20:45:57.099 INFO 16768 --- [nio-9001-exec-8] o.s.boot.SpringApplication : Started application in 3.294 seconds (JVM running for 1514.761)
2017-08-21 20:45:57.099 INFO 16768 --- [nio-9001-exec-8] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@676107bb: startup date [Mon Aug 21 20:45:57 CST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7dc4513
2017-08-21 20:45:57.217 INFO 16768 --- [nio-9001-exec-8] o.s.cloud.bus.event.RefreshListener : Received remote refresh request. Keys refreshed []

通过日志可以分析出,battcn-config-server重新加载了classpath:/config-server-order-default.yml文件,并且客户端重新读取了[name='configService', propertySources=[MapPropertySource [name='classpath:/config-server-order-default.yml']]],有兴趣的可以DEBUG源码看详细的处理过程…

- 疑问解答

  • 在测试第四步中,之所以修改yaml后重启,是因为我们的config是本地的而不是GIT仓库,所以项目没法加载,但是这种方式即使关闭了config,调用过的服务依旧是可以正常使用的,包括旧的配置依旧正常读取使用,Config做了本地缓存

  • 博客只演示了一个battcn-config-client的情况,多个client其实效果一样只需要刷新其中一个,剩下的都会跟着刷新,比如battcn-config-client启动了3个实例,分别是9001,9002,9003,访问:http://localhost:9001/bus/refresh,三个实例的结果都是一样

架构图

- 刷新范围

上面的例子中,我们通过向服务实例请求Spring Cloud Bus/bus/refresh接口,从而触发总线上其他服务实例的/refresh。但是有些特殊场景下(比如:灰度发布),我们希望可以刷新微服务中某个具体实例的配置。

Spring Cloud Bus对这种场景也有很好的支持:/bus/refresh接口还提供了destination参数,用来定位具体要刷新的应用程序。比如,我们可以请求/bus/refresh?destination=customers:9000,此时总线上的各应用实例会根据destination属性的值来判断是否为自己的实例名,若符合才进行配置刷新,若不符合就忽略该消息。

destination参数除了可以定位具体的实例之外,还可以用来定位具体的服务。定位服务的原理是通过使用SpringPathMatecher(路径匹配)来实现,比如:/bus/refresh?destination=customers:**(以冒号的路径分隔符:)来确定一个实例是否处理该消息,该配置的请求会触发customers服务的所有实例进行刷新。

- 优化方案

既然访问任意端口都可以通知全部实例,那么我们利用刷新范围的方式,将battcn-config-server也添加上spring-cloud-starter-bus-amqp和 rabbitmq的配置信息,访问:http://localhost:9000/bus/refresh?destination=battcn-config-client:** 结果一样,battcn-config-client依然会去读取最新配置

优化后

主要做了这些改动:

1.在Config Server中也引入Spring Cloud Bus,将配置服务端也加入到消息总线中来。
2./bus/refresh请求不在发送到具体服务实例上,而是发送给Config Server,并通过destination参数来指定需要更新配置的服务或实例。

参考博客:http://blog.didispace.com/springcloud7/

消息总线(Bus)的更多相关文章

  1. Spring Cloud第十二篇 | 消息总线Bus

    ​ ​本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

  2. 学习一下 SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  3. SpringCloud学习系列之五-----配置中心(Config)和消息总线(Bus)完美使用版

    前言 在上篇中介绍了SpringCloud Config的使用,本篇则介绍基于SpringCloud(基于SpringBoot2.x,.SpringCloud Finchley版)中的分布式配置中心( ...

  4. Spring-cloud微服务实战【十】:消息总线Bus

      回忆一下,在上一篇文章中,我们使用了分布式配置中心config来管理所有微服务的配置文件,那这样有没有什么问题?有,那就是无法配置文件无法自动更新,当我的git服务器上的配置文件更新后,不能同步更 ...

  5. SpringCloud入门 消息总线 Bus

    消息总线 1.概述 使用SpringCloud Bus配和Spring Cloud Config使用实现配置的动态刷新 Bus只支持消息处理:RabbitMQ和Kafaka. 能干嘛 能管理和传播分布 ...

  6. SpringCloud之Config配置中心+BUS消息总线原理及其配置

    一.配置中心作用 在常规的开发中,每个微服务都包含代码和配置.其配置包含服务配置.各类开关和业务配置.如果系统结构中的微服务节点较少,那么常规的代码+配置的开发方式足以解决问题.当系统逐步迭代,其微服 ...

  7. SpringCloud系列——Bus 消息总线

    前言 SpringCloud Bus使用轻量级消息代理将分布式系统的节点连接起来.然后可以使用此代理广播状态更改(例如配置更改)或其他管理指令.本文结合RabbitMQ+GitHub的Webhook实 ...

  8. 第七篇: 消息总线(Spring Cloud Bus)

    Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...

  9. Spring Cloud Bus 消息总线 RabbitMQ

    Spring Cloud Bus将分布式系统中各节点通过轻量级消息代理连接起来. 从而实现例如广播状态改变(例如配置改变)或其他的管理指令. 目前唯一的实现是使用AMQP代理作为传输对象. Sprin ...

  10. 第九章 消息总线: Spring Cloud Bus

    在微服务架构的系统中, 我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实例都连接上来, 由于该主题中产生的消息会被所有实例监听和消费, 所以我们称它为消息总线. 在总线上的各 ...

随机推荐

  1. Java实现 LeetCode 234 回文链表

    234. 回文链表 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否 ...

  2. Java实现洛谷 P2802 回家

    P2802 回家 时间限制 1.00s 内存限制 125.00MB 题目描述 小H在一个划分成了n*m个方格的长方形封锁线上. 每次他能向上下左右四个方向移动一格(当然小H不可以静止不动), 但不能离 ...

  3. java实现第四届蓝桥杯买不到的数目

    买不到的数目 题目描述 小明开了一家糖果店.他别出心裁:把水果糖包成4颗一包和7颗一包的两种.糖果不能拆包卖. 小朋友来买糖的时候,他就用这两种包装来组合.当然有些糖果数目是无法组合出来的,比如要买 ...

  4. FT-8900, 8800,7800 , FT-897, 857 e 817 连接中继板接线图

    FT-8900, 8800,7800 , FT-897, 857 e 817 等 车台支持Moto GM950i GM300(只适合接收) GM3688等

  5. 三、TCP协议

    TCP(Transmission Control Protocol)传输控制协议:顾名思义就是对数据的传输进行控制 TCP报头 序号:相当于编号,当TCP数据包过大的时候会进行分段,分段之后按序号顺序 ...

  6. Rust异步之Future

    对异步的学习,我们先从Future开始,学习异步的实现原理.等理解了异步是怎么实现的后,再学习Rust异步编程涉及的2个库(futures.tokio)的时候就容易理解多了. Future rust中 ...

  7. 「雅礼集训 2017 Day4」洗衣服

    题目   点这里看题目. 分析   首先考虑只有洗衣机的情况.我们可以想到,当前洗衣任务结束越早的洗衣机应该被先用,因此可以用堆来动态维护.   再考虑有烘干机的情况.很显然,越晚洗完的衣服应该越早烘 ...

  8. foreach 集合又抛经典异常了,这次一定要刨根问底

    一:背景 1. 讲故事 最近同事在写一段业务逻辑的时候,程序跑起来总是报:集合已修改:可能无法执行枚举操作,硬是没有找到什么情况下会导致这个异常产生,就让我来找一下bug,其实这个异常在座的每个程序员 ...

  9. Python format格式化函数

    参考资料:https://www.runoob.com/python/att-string-format.html 在学习Python的时候碰到了一个很有趣的格式化输入的技巧,下面记录在此. Pyth ...

  10. 容器技术之Docker Machine

    前文我们聊了下docker容器的资源限制,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13138725.html:今天我们来聊一聊docker machine ...