文章目录

  • 消息总线(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. VC++运算符与优先级
  2. Bochs调试Linux内核初级入门2、bochs调试断点和单步指令、0x7c00、关中断和开中断指令
  3. boost::adjacent_find相关的测试程序
  4. Android之解决ScrollView嵌套RecycleView导致滑动冲突或者显示不全的问题
  5. [转载] Python列表操作
  6. linux添加用户命令_为Linux的cp和mv命令添加进度条
  7. MongoDB学习记录:入门(一)——五叶草
  8. 牛客网-公司真题-买帽子
  9. PHP文件上传类(页面和调用部分)
  10. Hbase教程(一) Hbase入门教程
  11. Houdini11:材质
  12. oracle 计算时间差 毫秒,Oracle计算时间差为毫秒的实现代码
  13. input标签 设置纯数字输入
  14. matlab 写netcdf,写入 netCDF 属性
  15. 英语日常口语对话(2)
  16. IIc通信协议(一)
  17. WebP支持:超乎你想象
  18. 猪猪的机器学习(十九)卷积神经网络
  19. Arduino的智能语音输入实现——Arduino与LU-ASR01的连接
  20. 视觉伺服控制工具Visual Servoing Platform---VISP(6)----基于4个平面点的姿态估计

热门文章

  1. php formdata 多个图片保存_PHP-FPM是什么?
  2. java遍历对象属性_java开发中遍历一个对象的所有属性并set值 缓存优化
  3. 香帅的北大金融学课笔记17 -- 公司治理
  4. 数据结构与算法(C++)– 图(Graph)
  5. 公众号推荐:Python入门、统计学、推荐系统、机器学习、深度学习、数据分析...
  6. 【数据竞赛】CCF乘用车细分市场销量预测竞赛总结
  7. 【深度学习】用于小目标检测的一个简单高效的网络
  8. 【NLP】5 分钟理解百度 ERNIE 核心思想
  9. 【算法基础】数据结构导论第三章-栈、队列和数组.pptx
  10. AI入门:不用任何公式把主成分分析讲清楚