一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)
上一章节,我们讲解了分布式配置中心spring cloud config,我们把配置项存放在git或者本地,当我们修改配置时,需要重新启动服务才能生效。但是在生产上,一个服务部署了多台机器,重新启动比较麻烦且会短暂影响用户体验。spring cloud生态在发展,肯定有对应的解决之法,接下来将要讲解的Spring Cloud Bus就是为了解决这一难题而存在的。
Spring Cloud Bus(消息总线)通过一个轻量级的消息中间件可以连接分布式系统中的各个节点。使用该总线来广播某些状态的改变(比如配置信息发生变更)或其他管理指令。可以说,消息总线是spring boot应用扩展“道路”上的推进器,而且也把它用来作应用间相互通信的消息管道。
一、项目搭建:
1. 环境准备
本章还是基于上一章来实现的,上一章讲解了git和本地配置两种方式,配置刷新原理都是一样的,这次我们只讲git配置修改后进行刷新。
上一章节内容可以参考:一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
<?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> <parent>
<groupId>com.haly</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent> <groupId>com.haly</groupId>
<artifactId>springcloud-feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-ribbon-client</name>
<description>新建一个springcloud项目</description> <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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.haly</groupId>
<artifactId>springcloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3. 在springcloud-feign-client模块的application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并加上spring.cloud.bus的三个配置:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=young
spring.rabbitmq.password=young spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
4. springcloud-feign-client模块的启动类上加上注解:@RefreshScope
package com.haly; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@RefreshScope
public class SpringcloudFeignClientApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudFeignClientApplication.class, args);
} }
5. springcloud-feign-client模块的测试类FeignController,测试上一章节的/testconfig方法,具体内容可以参考:一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
package com.haly.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import com.haly.romote.FeignRemoteService; @RestController
public class FeignController { @Autowired
FeignRemoteService feignRemoteService; @Value("${configword}")
String configword; @GetMapping(value = "/getHello")
public String getHello(@RequestParam String name) {
return feignRemoteService.hello(name);
} @GetMapping(value = "/testzuul")
public String testzuul(@RequestParam String name) {
return name +",这是springcloud-feign-client的服务接口";
} @GetMapping(value = "/testconfig")
public String testconfig(@RequestParam String name) {
return name +",git配置值:" + configword ;
}
}
6. 运行项目
启动springcloud-eureka-server,启动springcloud-config-server,启动springcloud-config,最后启动springcloud-feign-client模块
为了测试配置修改,多个服务实例都能更新,就启动两个 springcloud-feign-client 实例,端口分别是9600,9601,sts启动两个实例(端口为9600时启动项目,然后将端口改成9601,再启动项目)。
浏览器输入:http://localhost:9600/testconfig?name=young码农 或者输入 http://localhost:9601/testconfig?name=young码农
页面展示结果:young码农,git配置值:NewConfig !
7. 修改git配置,重新运行项目
这时我们去代码仓库将configword的值改为“update config”,即改变配置文件configword的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。
现在,我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,你会发现springcloud-feign-client会重新读取配置文件,接着我们查看页面运行结果。
浏览器输入:http://localhost:9600/testconfig?name=young码农 或者输入 http://localhost:9601/testconfig?name=young码农
页面展示结果:young码农,git配置值:update config !
二、总结:
使用"destination"参数,/actuator/bus-refresh接口可以指定服务,例如 “/actuator/bus-refresh?destination=client:**”, 即刷新服务名为client的所有服务。
通过上面的测试,我们可以知道当git文件更改的时候,用post 向端口为8882的config-client发送请求/bus/refresh/;此时8882端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。
引入程序猿DD 画的一张图片,简单理解一下刷新原理:

最后项目目录结构:

一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)的更多相关文章
- 第七篇: 消息总线(Spring Cloud Bus)
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版本请 ...
- 通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka或RocketMQ)
如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案就不太适合了.使用 ...
- SpringCloud学习(八)消息总线(Spring Cloud Bus)(Finchley版本)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- SpringCloud 教程 (一) 消息总线(Spring Cloud Bus)
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现通知微服务 ...
- SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
一.安装rabbitmq 二.pom父文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...
- Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus
在本教程第三讲Spring Cloud 入门教程(三): 配置自动刷新中,通过POST方式向客户端发送/refresh请求, 可以让客户端获取到配置的最新变化.但试想一下, 在分布式系统中,如果存在很 ...
随机推荐
- 4、NameNode启动过程详解
NameNode 内存 本地磁盘 fsimage edits 第一次启动HDFS 格式化HDFS,目的就是生成fsimage start NameNode,读取fsimage文件 start Data ...
- Parametric and Nonparametric Algorithms
即参数化算法和非参数化算法. 参数化机器学习算法 可以大大简化学习过程,也可以限制可以学到的东西,将函数简化为已知形式的算法称为参数化机器学习算法.算法包括两个步骤: 为函数选择一个form. 从训练 ...
- P4848 崂山白花蛇草水
题意:支持修改的矩形第 \(k\) 大. 题解:动态开点权值线段树 套 Kd-tree. 然后也没什么难的但就是写不对...调了两天才调出来然后发现跑的巨慢,于是又%了一发Claris'题解,跑的真快 ...
- [后端]gitlab之gitlab-ci自动部署
转发:https://www.jianshu.com/p/df433633816b 简介 gitlab-ci全称是gitlab continuous integration的意思,也就是持续集成.中心 ...
- 微信小程序开发工具“当前系统代理不是安全代理”
(1)删除注册表中以proxy开头的项目再次重启 regedit进入[HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Inter ...
- mov offset和lea的区别
mov offset和lea的区别 原文地址:https://www.cnblogs.com/fanzi2009/archive/2011/11/29/2267725.html 全局变量取地址用mo ...
- 平安银行Java面试-社招-五面(2019/09)
个人情况 2017年毕业,普通本科,计算机科学与技术专业,毕业后在一个二三线小城市从事Java开发,2年Java开发经验.做过分布式开发,没有高并发的处理经验,平时做To G的项目居多.写下面经是希望 ...
- session与cookie之间的关系
一.客户端与服务端请求响应的关系 USER(客户端) 请求 tomcat(服务器), 属于HTTP请求.http请求是无状态的,即每次服务端接收到客户端的请求时,都是一个全新的请求,服务器并不知道客户 ...
- 第09组 Beta冲刺(2/4)
队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...
- go -- go 程序 启动docker容器
package main import ( "io" "log" "os" "time" "github.co ...