Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitMQ、Kafka等)连接分布式系统的节点,这样就可以通过Spring Cloud Bus广播配置的变化或者其他的管理指令。使用Spring Cloud Bus后的架构如图9-2所示。

图9-2 使用Spring Cloud Bus的架构图

由图可知,微服务A的所有实例通过消息总线连接到了一起,每个实例都会订阅配置更新事件。当其中一个微服务节点的/bus/refresh端点被请求时,该实例就会向消息总线发送一个配置更新事件,其他实例获得该事件后也会更新配置。

下面我们以RabbitMQ为例,为大家讲解如何使用Spring Cloud Bus实现配置的自动刷新。

(1) 安装RabbitMQ。RabbitMQ的安装非常简单,本书不再赘述。

(2) 创建项目microservice-config-client-refresh-cloud-bus

(3) 为项目添加spring-cloud-starter-bus-amqp 的依赖。

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

(4) 在bootstrap.yml中添加以下内容:

spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

management:
    security:
      enabled: false #是否开启actuator安全认证

 

测试

(1) 启动microservice-config-server

(2) 启动microservice-config-client-refresh-cloud-bus,可发现此时控制台打印类似于以下的内容:

[           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/bus/refresh],methods=[POST]}" onto public void org.springframework.cloud.bus.endpoint.RefreshBusEndpoint.refresh(java.lang.String)

说明此时有一个/bus/refresh 端点

(3) 将microservice-config-client-refresh-cloud-bus的端口改成8082,再启动一个节点。

(4) 访问http://localhost:8081/profile ,可获得结果:dev-1.0。

(4) 将git仓库中的microservice-foo-dev.properties文件内容改为profile=dev-1.0-bus

(5) 发送POST请求到其中一个Config Client节点的的/bus/refresh端点,例如:

curl -X POST http://localhost:8081/bus/refresh

借助Git仓库的WebHook,我们就可轻松实现配置的自动刷新。如图9-3所示。

图9-3 Git WebHooks设置

借助Git仓库的WebHook,我们就可轻松实现配置的自动刷新。如图9-3所示。(6) 访问两个Config Client节点的/profile端点,会发现两个节点都会返回dev-1.0-bus ,说明配置内容已被刷新。

局部刷新

某些场景下(例如灰度发布),我们可能只想刷新部分微服务的配置,此时可通过/bus/refresh端点的destination参数来定位要刷新的应用程序。

例如:/bus/refresh?destination=customers:9000 ,这样消息总线上的微服务实例就会根据destination参数的值来判断是否需要要刷新。其中,customers:9000 指的是各个微服务的ApplicationContext ID。

destination参数也可以用来定位特定的微服务。例如:/bus/refresh?destination=customers:**,这样就可以触发customers微服务所有实例的配置刷新。

扩展阅读:关于ApplicationContext ID

默认情况下,ApplicationContext ID是spring.application.name:server.port,详见org.springframework.boot.context.ContextIdApplicationContextInitializer.getApplicationId(ConfigurableEnvironment)方法。

http://www.itmuch.com/spring-cloud-code-read/spring-cloud-code-read-spring-cloud-bus/

架构改进

在前面的示例中,我们通过请求某个微服务的/bus/refresh端点的方式来实现配置刷新,但这种方式并不优雅。原因如下:

(1) 打破了微服务的职责单一性。微服务本身是业务模块,它本不应该承担配置刷新的职责。

(2) 破坏了微服务各节点的对等性。

(3) 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就不得不修改WebHook的配置。

我们不妨改进一下我们的架构。

图9-4 使用Spring Cloud Bus的架构图

如图9-4,我们将Config Server也加入到消息总线中,并使用Config Server的/bus/refresh端点来实现配置的刷新。这样,各个微服务只需要关注自身的业务,而不再承担配置刷新的职责。

1.在springcloud-configServer中配置bus,加入pom依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.springcloud-configServer中配置bus ym文件

spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
security:
enabled: false #是否开启actuator安全认证

3.在微服务端springcloud-ssmServer配置bus接收

pom.xml

<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>

application.properties

#mq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest management.security.enabled=false

在需要自动刷新的位置加入@RefreshScope 例如

package com.pupeiyuan.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RefreshScope
public class ConfigClientController { @Value("${multiple.datasource.master.InitialSize}")
private String profile; @GetMapping("/profile")
public String getProfile() {
return this.profile;
}
}

利用webhook自动请求,配置/bus/refresh到springcloud-configServer项目,springcloud-configServer在利用bus传播特性,通过mq传播到指定客户端进行更新

跟踪总线事件

一些场景下,我们可能希望知道Spring Cloud Bus事件传播的细节。此时,我们可以跟踪总线事件(RemoteApplicationEvent的子类都是总线事件)。

跟踪总线事件非常简单,只需设置spring.cloud.bus.trace.enabled=true ,这样在/bus/refresh端点被请求后,访问/trace端点就可获得类似如下的结果:

{
"timestamp": 1481098786017,
"info": {
"signal": "spring.cloud.bus.ack",
"event": "RefreshRemoteApplicationEvent",
"id": "66d172e0-e770-4349-baf7-0210af62ea8d",
"origin": "microservice-foo:8081",
"destination": "**"
}
},{
"timestamp": 1481098779073,
"info": {
"signal": "spring.cloud.bus.sent",
"type": "RefreshRemoteApplicationEvent",
"id": "66d172e0-e770-4349-baf7-0210af62ea8d",
"origin": "microservice-config-server:8080",
"destination": "**:**"
}
}...

这样,我们就可清晰地知道事件的传播细节。

spring cloud 使用spring cloud bus自动刷新配置的更多相关文章

  1. Springboot整合Spring Cloud Kubernetes读取ConfigMap,支持自动刷新配置

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Docker & Kubernetes相关文章:容器技术 之前介绍了Spring Cloud Config的用法,但 ...

  2. SpringBoot整合Nacos自动刷新配置

    目的 Nacos作为SpringBoot服务的注册中心和配置中心. 在NacosServer中修改配置文件,在SpringBoot不重启的情况下,获取到修改的内容. 本例将在配置文件中配置一个 cml ...

  3. Spring Cloud Bus 自动更新配置

    ---恢复内容开始--- Spring Cloud Config 结合 Spring Cloud bus 实现 git 仓库提交配置文件 触发消息队列 应用自动更新配置 1. config 服务端 添 ...

  4. 带你入门SpringCloud 之 通过SpringCloud Bus 自动更新配置

    前言 在<带你入门SpringCloud统一配置 | SpringCloud Config>中通过 SpringCloud Config 完成了统一配置基础环境搭建,但是并没有实现配置修改 ...

  5. 【Spring Cloud】Spring Cloud使用总结

    项目概要 项目环境信息 IDEA ultimate 2018.3.2 springboot 2.1.7.RELEASE springCloud Greenwich.SR2 Eureka 介绍 基于ne ...

  6. 通过总线机制实现自动刷新客户端配置(Consul,Spring Cloud Config,Spring Cloud Bus)

    通过总线机制实现自动刷新客户端配置 方案示意图 利用Git服务的webhook通知功能,在每次更新配置之后,Git服务器会用POST方式调用配置中心的/actuator/bus-refresh接口,配 ...

  7. Spring Cloud 学习 之 Spring Cloud Bus实现修改远程仓库后配置自动刷新

    ​ 版本号: ​ Spring Boot:2.1.3.RELEASE ​ Spring Cloud:G版 ​ 开发工具:IDEA 搭建配置中心,这里我们搭建一个简单版的就行 POM: <?xml ...

  8. Spring Cloud Config 自动刷新所有节点

    全局刷新 详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p160-9.9.2节 1.使用Spring Cloud Config 客户端时,可以使用 /refresh ...

  9. spring cloud - config 属性自动刷新

    启动config-server,启动成功后就不需要在管了; 在config-client做些修改: 在使用的controller或service的类上加上一个注解@RefreshScope 在pom中 ...

随机推荐

  1. constexpr和常量表达式

    常量表达式:值不会改变并且在编译过程就能得到计算结果的表达式. 字面值属于常量表达式,用常量表达式初始化的const对象也是常量表达式. 一个对象(或表达式)是不是常量表达式由它的数据类型和初始值共同 ...

  2. [TensorFlow笔记乱锅炖] tf.multinomial(logits, num_samples)使用方法

    tf.multinomial(logits, num_samples) 第一个参数logits可以是一个数组,每个元素的值可以简单地理解为对应index的选择概率,注意这里的概率没有规定加起来的和为1 ...

  3. Django实战(一)-----用户登录与注册系统2(数据模型、admin后台、路由视图)

    使用Django开发Web应用的过程中,很多人都是急急忙忙地写视图,写前端页面,把最根本的模型设计给忽略了. 模型中定义了数据如何在数据库内保存,再直白点说就是数据表的定义.这部分工作体现在Djang ...

  4. ROI Pool和ROI Align

    这里说一下ROI Pool和ROI Align的区别: 一.ROI Pool层: 参考faster rcnn中的ROI Pool层,功能是将不同size的ROI区域映射到固定大小的feature ma ...

  5. Python运维开发基础08-文件基础【转】

    一,文件的其他打开模式 "+"表示可以同时读写某个文件: r+,可读写文件(可读:可写:可追加) w+,写读(不常用) a+,同a(不常用 "U"表示在读取时, ...

  6. MFC不可不会

    这些可能会很抽象,你既然学MFC,给你几个不可少的技术点 1.Dynamic Creation2.Runtime Type Imformation3.Persistence4.Message Mapp ...

  7. LabVIEW中下拉列表和枚举的区别(两点)

    第一:如图,在表示法上,下拉列表表示的数据范围要大,枚举只能是U32,U16, U8 第二:在vi的动态调用过程中,常用下拉列表,因为枚举控件不能动态的增加或者减少项目,而下拉列表则可以.

  8. 【原创】大叔经验分享(34)hive中文注释乱码

    在hive中查看表结构时中文注释乱码,分为两种情况,一种是desc $table,一种是show create table $table 1 数据库字符集 检查 mysql> show vari ...

  9. JS调用摄像头并上传图片到服务器

    本功能只能把图片转成base64码上传,如何上传图片还没有修改出来,有兴趣的朋友弄出来了,请给我留下言,谢谢了! 直接上代码,需要的朋友直接复制就可以使用了. <!DOCTYPE html> ...

  10. restful中的分页

    普通分页 普通分页类似于Django中的分页 源码 class PageNumberPagination(BasePagination): """ A simple pa ...