springCloud学习-消息总线(Spring Cloud Bus)
1、简介
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。
Spring Cloud Bus 可选的消息代理组建包括 RabbitMQ 、 AMQP 和Kafka 等。本节 讲述的是用 RabbitMQ 作为 Spring Cloud 的消息组件去刷新更改微服务的配置文件。 因此需要安装RabbitMQ,点击RabbitMQ下载。安装和使用方式请自行搜索。
2、改造config-client
1、此功能只需要改造config-client工程,首先在pom.xml中引入基于rabbitMQ实现的 spring cloud bus的起步依赖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.lishun</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>com.lishun</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies> </project>
2、在配置文件bootstrap.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并需要加上spring.cloud.bus的三个配置,具体如下:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
3、最后 , 需要在更新的配置类上加@ RefreshScope 注解 , 只有加上了该注解,才会在不重启服务的情况下更新配置 ,代码如下
@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
} @Value("${id}")
String id;
@RequestMapping(value = "/hi")
public String hi(){
return id;
}
}
4、依次启动eureka-server、confg-cserver,启动两个config-client,端口为:8881、8882。
5、访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:
config-test
6、将github上的配置文件id的值改为config-test-11,如果是传统的做法,需要重启服务,才能达到配置文件的更新。们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,在控制台你会发现config-client会重新读取配置文件。
7、这时我们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:
config-test-11
/actuator/bus-refresh接口可以指定服务,即使用”destination”参数,比如 “/actuator/bus-refresh?destination=customers:**” 即刷新服务名为customers的所有服务。
springCloud学习-消息总线(Spring Cloud Bus)的更多相关文章
- SpringCloud学习(八)消息总线(Spring Cloud Bus)(Finchley版本)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- 【SpringCloud 】第八篇: 消息总线(Spring Cloud Bus)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- 原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f8-bus/ 本文出自方志朋的博客 转载请标明出处: Spr ...
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc08-bus/ 本文出自方志朋的博客 最新Finchley版本请 ...
- SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
一.安装rabbitmq 二.pom父文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...
- SpringCloud 教程 (一) 消息总线(Spring Cloud Bus)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- 通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka或RocketMQ)
如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...
- 一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)
上一章节,我们讲解了分布式配置中心spring cloud config,我们把配置项存放在git或者本地,当我们修改配置时,需要重新启动服务才能生效.但是在生产上,一个服务部署了多台机器,重新启动比 ...
- 第七篇: 消息总线(Spring Cloud Bus)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
随机推荐
- [Swift通天遁地]五、高级扩展-(5)获取互补色、渐变色、以及图片主题颜色
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Java8080端口被占用解决办法
netstat -ano | findstr 8080 taskkill -pid 3196-f
- 能力 or say 职业 规划
2019.5.8 黑盒测试,白盒测试,接口测试,自动化测试,性能测试.. 往测试工程师发展,再是测试开发,高级测试开发.. 要是真的喜欢前端,可以再转吧.前端后端应该要清楚它们的区别 前端:广度, ...
- Spring思维课程导图——bean属性的设置
- Linux-fork()函数详解,附代码注释
// // main.c // Project_C // // Created by LiJinxu on 16/8/13. // Copyright © 2016年 LiJinxu-NEU. All ...
- Android:用签名打包后微信分享失效
刚开始使用微信分享,申请的微信appid也可以在直接使用,分享成功! 当我使用自己的签名打包分享时却分享失败,一闪而过,好郁闷的说,为什么之前没有打包就可以,签名打包后就不可以了... 开始查找各种资 ...
- Raspberry Pi开发之旅-空气温湿度检测(DHT11)
一.首先,简单介绍下DHT11: DHT11是一个温湿度传感器,分为3个接口,分别为:VCC, DATA, GND 引脚号 名称 类型 说明 1 VCC 电源 +级,输入3V-5.5V 2 DATA ...
- Core 项目下使用SQl语句
public static DataTable ExeQuery(string connectionString, CommandType cmdType, string cmdText) { usi ...
- java攻城师之路--复习java web之request_respone
Servlet技术 两条主线1.HTTP协议 2.Servlet生命周期 init() 方法中参数 ServletConfig 对象使用通过ServletConfig 获得 ServletContex ...
- MVC5+EasyUI+EF6+Linq通用权限系统出炉(1)
1.先晒一下结构吧,