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实现蓝桥杯历届试题格子刷油漆

    问题描述 X国的一段古城墙的顶端可以看成 2*N个格子组成的矩形(如下图所示),现需要把这些格子刷上保护漆. 你可以从任意一个格子刷起,刷完一格,可以移动到和它相邻的格子(对角相邻也算数),但不能移动 ...

  2. Java 实现 蓝桥杯 生兔子问题

    生兔子问题 有一对兔子,从出生后第四个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子.假如兔子都不死,计算第十个月兔子的总数? 分析: 四个月开始生兔子,则:F(N) = f(n-1) ...

  3. Linux 系统命令sudo权限

    sudo权限 root把本来只能超级用户执行的命令赋予普通用户执行 sudo的操作对象是系统命令 sudo使用 1.给普通用户赋予所能执行的权限(实质是修改/etc/sudoers文件):vi /et ...

  4. 连接mongoodb并且向数据库添加信息

    连接mongoodb 首先安装mongoose 配置URL 连接Mongoodb数据库 控制台输入指令运行该js文件 像这样就连接成功了 向Mongodb数据库增加信息,首先需要写入表格信息 接着解构 ...

  5. Netty源码学习系列之1-netty的串行无锁化

    前言 最近趁着跟老东家提离职之后.到新公司报道之前的这段空闲时期,着力研究了一番netty框架,对其有了一些浅薄的认识,后续的几篇文章会以netty为主,将近期所学记录一二,也争取能帮未对netty有 ...

  6. java关键字final用法详解

    final关键字在java中也是属于比较常用的一种,因此也算得上是一个比较重要的关键字,有必要对它进行深入的学习. 一.定义:用来说明最终属性,表明一个类不能派生出子类,或者成员方法不能被覆盖,或者成 ...

  7. SmokePing 快速搭建

    SmokePing介绍 smokeping是来监控IDC机房网络质量情况,可以从监控图上的延时与丢包情况分辨出机房的网络是否稳定,是否为多线,是否为BGP机房以及到各城市的三个运行商网络各是什么情况. ...

  8. Elasticsearch 常见错误

    一 read_only_allow_delete" : "true" 当我们在向某个索引添加一条数据的时候,可能(极少情况)会碰到下面的报错: { "error ...

  9. 64位手机无法加载x5(libmttwebview.so is 32-bit instead of 64-bit)

    x5内核暂时不提供64位的so文件,在64位手机上需要让AP以32位模式运行. 具体操作如下: 1.如果使用是Eclipse则需要将所有的.so文件都放置在so加载目录:lib/armeabi文件夹下 ...

  10. [xDebug] php下的Debug的调试

    基础知识 对于php来说,debug的话,有个东西叫xdebug,当然配置这个时候,特别在mac下出了很多坑,这里强烈推荐一个mac下类似phpstudy的东西,叫做MxSrvs,了解一下? Xdeb ...