文章目录

  • 消息总线(Bus)介绍
  • 项目示例
    • config-client-bus

代码地址:github-spring-cloud地址

前言:前面文章讲了Spring Cloud Config配置中心如何使用,当我们在更新git上面的配置,想要获取最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新客户端,客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了。使用Spring Cloud Bus可以完美解决这一问题。使用消息总线需要依赖mq,本章文章将采用RabbitMq,具体怎么安装RabbitMq可以查看此文章:安装RabbitMq

消息总线(Bus)介绍

Spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用AMQP消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。

项目示例

本章节消息总线集成在客户端,本章节在前几张SpringCloud基础上改进。

config-client-bus

添加pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-cloud-learn</artifactId><groupId>com.sl.learn.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.sl.learn.cloud</groupId><artifactId>config-client-bus</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency></dependencies>
</project>

添加配置文件bootstrap.properties


spring.application.name=config-client-bus
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8088/
server.port=8098eureka.client.service-url.defaultZone=http://localhost:8080/eureka/management.endpoints.web.exposure.include= *
management.endpoint.health.show-details= always
management.endpoint.shutdown.enabled= true
info.app.name =  spring-boot-actuator
info.app.version =  1.0.0
info.app.test =  test## 开启消息跟踪
spring.cloud.bus.trace.enabled=truespring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

启动类Application

@SpringBootApplication
@RestController
@RefreshScope
@EnableEurekaClient
@EnableDiscoveryClient
public class ConfigClientBusApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientBusApplication.class,args);}@Value("${helloConfig}")private String helloConfig;@RequestMapping(value = "/hi")public String helloConfig() {return helloConfig;}
}
  • 创建配置文件注意两点:
    rabbitmq的默认端口为:5672

  • 监控 actuator 一定要配下面这几个,不然访问http://localhost:8097/actuator会报404
    management.endpoints.web.exposure.include= *
    management.endpoint.health.show-details= always
    management.endpoint.shutdown.enabled= true

客户端分别启动两个端口8097,8098
分别访问地址:http://localhost:8098/hi和http://localhost:8097/hi
返回结果为:1233

我们对端口为8098的服务发送一个/actuator/bus-refresh的POST请求,在win10下使用下面命令来模拟webhook。curl -v -X POST http://localhost:8098/actuator/bus-refresh

返回结果为可以看到已经把最新的结果返回:

注意一点springboot2.0访问地址为:actuator/bus-refresh

本篇文章的消息总线在配置中心的使用机制用图表示(图片来源于网上)

SpringCloud教程-消息总线Bus 客户端(client)刷新(SpringCloud版本Greenwich.SR4)相关推荐

  1. SpringCloud教程-消息总线Bus 服务端(server)刷新(SpringCloud版本Greenwich.SR4)

    文章目录 项目示例 config-server-bug 代码地址:github-spring-cloud地址 前言:本篇文章在上一篇文章基础上进行修改,因为虽然我们做到了利用一个消息总线触发刷新,而刷 ...

  2. SpringCloud config 配置中心集群配置以及整合消息总线BUS实现关联微服务配置自动刷新

    一.SpringCloud Config 基本配置中的问题 在上一章节<SpringCloud config 配置中心介绍与基本配置使用>中我们现实了配置中心的配置集中管理.调用微服务应用 ...

  3. 499、Java分布式和集群12 -【SpringCloud视图微服务 - 消息总线Bus】 2021.06.01

    目录 0.RabbitMQ 1.先运行,看到效果,再学习 2.pom.xml 3.bootstrap.yml 4.application.yml 5.ProductDataServiceApplica ...

  4. java技术--SpringCloud:消息总线Bus简介及代码实现(18)

    1.消息总线Bus简介 (1)消息总线Bus的作用<1>在没有使用消息总线的时候,如果需要修改某个配置1.1.如果涉及修改的微服务节点比较多,需要手动的逐个节点的刷新非常麻烦1.2.在微服 ...

  5. Chapter2 消息总线 ConfigClient配置自动刷新

    Chapter2 消息总线ConfigClient配置自动刷新 Spring Cloud Bus: Spring Cloud Bus提供了批量刷新配置的机制,它使用轻量级的消息代理(例如RabbitM ...

  6. iframe的src动态修改并刷新_微服务中配置中心Config+消息总线Bus,实现分布式自动刷新配置

    技术/杨33 一.分布式配置中心Config 一套集中的.动态的配置管理,实现统一配置微服务中的每个子服务. Spring Cloud Config为微服务架构提供了集中化的外部配置支持,配置服务器为 ...

  7. Spring Cloud Bus 消息总线实现配置自动刷新

    why 当微服务太多的时候,服务之间需要建立通信或一个服务的改变需要广播到所有其它服务,这时就需要有一个总线来承担对应的职责. what spring cloud bus 是通过轻量消息代理连接各个分 ...

  8. SpringCloud消息总线——Bus

    Bus 本专栏学习内容来自尚硅谷周阳老师的视频 有兴趣的小伙伴可以点击视频地址观看 在SpringCloud Config学习过程中,还遗留下来一个问题:当运维更新git上的配置信息时,要想更改所有的 ...

  9. SpringCloud之消息总线

    [前面的话]书接上文SpringCloud之Config,如果没有看过可以先移步去看一下.在上一篇文章中提到了配置刷新的问题,如果需要刷新配置就需要客户端执行refresh,我们可以利用webhook ...

最新文章

  1. rmd文件怎么转换html文件,提取.Rmd文件的html依赖项(包含htmlwidgets)
  2. 第九课.Python文件操作
  3. Vue项目build后静态资源文件路径或新建文件夹图片路径找不到的问题
  4. 根据客户中英文系统进入中英文页面
  5. mysql的单行注释_MySQL基础--会这些就够了
  6. Skaffold:让K8S开发工作变得简单
  7. MySQL百一题库_「灵魂拷问」MySQL面试高频一百问(工程师方向)
  8. codeforces 1040a (回文数的舞蹈)
  9. figma设计_设计原型的最简单方法:Figma速成课程
  10. 如何利用.NETCore向Azure EventHubs准实时批量发送数据?
  11. Maven实现热部署需要的依赖
  12. 【Json工具】一个json格式化查看工具——HIJSON
  13. android加音乐歌词代码,Android简易音乐播放器实现代码
  14. matlab 行 读取文件 跳过_matlab中textscan跳行使用
  15. 计算机误删怎么恢复数据,电脑误删除数据怎么恢复_电脑误删除数据恢复方法...
  16. 计算机幂函数xn,幂函数
  17. 算术平方根的整数部分(简单)*求平方根的三种方法**整数与小数取绝对值*
  18. 文昌京东配送小哥的那些骄傲事
  19. Codeforces Round #548 (Div. 2) C. Edgy Trees(思维+dfs)
  20. 我眼中的匈牙利命名法

热门文章

  1. python 命名管道_Python:检查命名管道是否有数据
  2. 怎么将多个html组合_技巧分享之在HTML元素中添加逼真阴影的教程
  3. matlab怎么给函数自变量赋值_MATLAB的变量及赋值
  4. 各种神经网络优化算法:从梯度下降到Adam方法
  5. Python-OpenCV 笔记9 -- 模板匹配
  6. 第四范式受邀参加信息技术大讲堂 共探新基建发展趋势
  7. rust(58)-凯撒密码
  8. 51nod 1022 石子合并v2
  9. 【深度学习】一文详解RNN及股票预测实战(Python)!
  10. AI自动标注神器!支持多通道、大尺幅数据