2019独角兽企业重金招聘Python工程师标准>>>

1 消息总线介绍

消息总线是一种通信工具,可以在机器之间互相传输消息、文件等。消息总线扮演着一种消息路由的角色,拥有一套完备的路由机制来决定消息传输方向。发送端只需要向消息总线发出消息而不用管消息被如何转发。Spring Cloud Bus 通过轻量消息代理连接各个分布的节点。管理和传播所有分布式项目中的消息,本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。

2 改造配置客户端(Config Client)

2.1基于上次工程进行改造,在config-client的pom文件中添加spring-cloud-starter-bus-amqp依赖:

<?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"><modelVersion>4.0.0</modelVersion><groupId>com.laravelshao.springcloud</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-client</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR4</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.2 在配置文件bootstrap.yml中添加rabbitmq相关配置信息:

server:port: 8881
management:security:enabled: false # 设置是否启用安全限制spring:application:name: config-clientcloud:config:uri: http://localhost:8888/ # 配置服务中心地址label: master # 远程仓库分支profile: dev # 指定环境discovery:enabled: true # 从配置中心读取文件service-id: config-server # 配置中心服务id# rabbitmq配置rabbitmq:host: localhostport: 5672
#      username:
#      password:
eureka:client:serviceUrl:defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址

management.security.enabled:设置是否启用安全限制

注意事项:需要配置management.security.enabled: false不启用安全限制,否则在请求刷新bus时会显示:

{

"timestamp": 1512118086064,

"status": 401,

"error": "Unauthorized",

"message": "Full authentication is required to access this resource.",

"path": "/bus/refresh"

}

后台也会提示添加安全策略或者设置不启用安全限制。

Full authentication is required to access actuator endpoints. Consider adding Spring Security or set 'management.security.enabled' to false.

2.3 在启动类添加@RefreshScope注解

@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}@Value("${hello}")String hello;@RequestMapping("/hi")public String hi() {return hello;}
}

2.4 依次启动eureka-server、config-server、config-client(启动两个实例,端口为8881、8882)

请求http://localhost:8881/hi 或 http://localhost:8882/hi,返回信息:

hello config server

2.5修改配置仓库中config-client-dev.properties中hello对应的值为hello config server change,使用POST请求http://localhost:8881/bus/refresh,会发现config-client重新读取了配置文件

再次请求http://localhost:8881/hi 或 http://localhost:8882/hi,返回更新后的信息:

hello config server change

本文源码下载地址:

https://github.com/laravelshao/spring-cloud-learning/tree/master/setion07-bus

转载于:https://my.oschina.net/LaravelShao/blog/1583629

Spring Cloud学习:07消息总线(Spring Cloud Bus)相关推荐

  1. spring cloud学习之消息总线(Finchley版本),以及postman下载与使用

    首先来学习链接: 方志朋:https://blog.csdn.net/forezp/article/details/81041028 程序猿DD:http://blog.didispace.com/s ...

  2. spring cloud bus_Spring Cloud学习笔记--消息总线(Bus)

    Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can the ...

  3. SpringCloud学习笔记 - 消息总线 - Spring Cloud Bus

    1. 消息总线简介 消息代理中间件构建了一个共用的消息主题让所有微服务实例订阅,当该消息主题产生消息时会被所有微服务实例监听和消费. 消息代理又是什么?消息代理是一个消息验证.传输.路由的架构模式,主 ...

  4. Spring Boot 学习之路之 Spring Security(二)加入mybatis

    上一篇 Spring Security 基础配置:  http://t.csdn.cn/m9oq5​​​​​​​ 在上文Spring Boot 学习之路之 Spring Security(一)中完成了 ...

  5. SpringCloud学习(八)消息总线(Spring Cloud Bus)(Finchley版本)

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

  6. 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)

    首先:欢迎各位学习java和大数据的程序员朋友们加入Java交流学习群: 721506929,群内提供免费的架构学习资料,有需要的朋友可以进群来学习. https://www.fangzhipeng. ...

  7. 原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/ 本文出自方志朋的博客 转载请标明出处: Spr ...

  8. 消息总线(Spring Cloud Bus)

    spring CloudBus 将分布式的节点和轻量的消息代理连接起来.这可以用于广播配置文件的更改或者其他的管理工作.一个关键的思想就是,消息总线可以为微服务做监控,也可以作为应用程序之间相互通讯. ...

  9. 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)

    转:https://blog.csdn.net/forezp/article/details/70148235 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播 ...

最新文章

  1. 经1503 20151453 张开拓
  2. 终于找到一个还算满意的blog来安家了
  3. JAVA获取资源的方法
  4. 使用PageHeap.EXE或GFlags.EXE检查内存越界错误 (转)
  5. 【ArcGIS Pro微课1000例】0007:ArcGIS Pro 2.5质量检查:拓扑创建与编辑案例教程
  6. mysql 控制id复原_清空mysql表后,自增id复原
  7. Leetcode PHP题解--D29 973. K Closest Points to Origin
  8. 谷歌在外贸中的实战解析
  9. html5自动旋转图片,HTML5画布旋转图片
  10. UVa11988 Broken Keyboard (a.k.a. Beiju Text)
  11. springboot map数据类型注入_Spring Boot(五):春眠不觉晓,Mybatis知多少
  12. 三菱plc pwm指令_三菱PLC高速处理指令编程
  13. 公用方法:得到汉字的首字母(大写) 方案3
  14. 实验7-3-8 输出大写英文字母
  15. 【计算机三级网络】考前看一看,必过60分
  16. 【 Android 10 生物识别 】系列 -- Fingerprint_指纹录入流程
  17. DIV+CSS系统学习:转载
  18. BK7256,上海博通-音视频Wi-Fi6combo-soc,内置Flash,RGB屏驱\720P
  19. 计算机学院运动会解说词,学院运动会入场解说词
  20. 日紫白飞星算法_地理紫白飞星择日口诀解析

热门文章

  1. Strange Class
  2. grafana结合zabbix打造炫酷监控界面
  3. 【办公效率篇】将全校所有学生成绩的Excel总表,按名字拆分为数个单独的Excel表点对点地发送给每个学生
  4. 四大组件以及 Application和Context的全面理解
  5. python discuz发帖_python:Discuz发帖器的实现
  6. 1过程流程图 3 apqp_汽车研发|APQP流程解析、管理及应用全解析!
  7. HTML+CSS 仿淘宝静态首页
  8. √【大学物理】电磁学 12-16章笔记
  9. 1683. 无效的推文 1693. 每天的领导和合伙人 1699. 两人之间的通话次数 1709. 访问日期之间最大的空档期
  10. python实现抢购nike鞋子_用Python实现淘宝秒杀功能