第八篇: 消息总线(Spring Cloud Bus)(Finchley版本)V2.0_dev

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

一、准备工作

本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq官网下载也可用我的下载好的。至于怎么使用 rabbitmq,可以参考我的操作文档。

Erlang:
链接:https://pan.baidu.com/s/1JevmH4MyAII1tIjzfUKx_Q
提取码:h9g2

Rabbitmq:
链接:https://pan.baidu.com/s/1dbK7PL6FHD4qO2KY9qXigA
提取码:s2bo

Rabbitmq操作手册:
RabbitMQ基本概念(二):windows下安装

创建父工程并引入依赖:

<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/></parent><modules><module>config-server</module><module>eureka-server</module><module>config-client</module></modules><groupId>com.gblfy</groupId><artifactId>sc-f-chapter8</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>sc-f-chapter8</name><description>Demo project for Spring Boot</description><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>Finchley.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></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>

二、改造config-client

第1步:在pom文件加上spring-cloud-starter-bus-amqp起步依赖,如下:

        <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>

第2步:在配置文件bootstrap.yml中加上RabbitMq的配置+打开自动刷新

RabbitMq的地址localhost
用户名guest
密码guest
端口5672

包括RabbitMq的地址,用户名、密码、端口。并需要加上spring.cloud.bus的三个配置,具体如下:

eureka:client:service-url:defaultZone: http://localhost:8761/eureka
server:port: 8881
spring:application:name: config-clientcloud:config:discovery:enabled: trueservice-id: CONFIG-SERVERprofile: devbus:enabled: truetrace:enabled: falserabbitmq:host: localhostusername: guestpassword: guestport: 5672
#打开自动刷新
management:endpoints:web:exposure:include: bus-refresh

第3步:ConfigClientApplication启动类代码如下:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}//    测试链接:curl -v -X POST "http://localhost:8882/actuator/bus-refresh"
//    在命令行执行以上命令即可
}

第4步:在配置类GirlConfig上面添加@RefreshScope注解

@Data
@Component
@ConfigurationProperties(prefix = "girl")
@RefreshScope
public class GirlConfig {private String name;private Integer age;
}

第5步:web访问类GirlController

@RestController
@RequestMapping("/girl")
public class GirlController {@Autowiredprivate GirlConfig girlConfig;@GetMapping("/info")public String getGirlInfo() {return "name:" + girlConfig.getName() + ",age:" + girlConfig.getAge();}
}

第6步:依次启动

  • eureka-server
  • config-server
  • 启动两个config-client实例
  • 端口为:8881、8882

访问http://localhost:8881/girl/info 或者http://localhost:8882/girl/info

浏览器显示:

name:yuxin age:1

第7步:这时我们登录远程代码仓库将girl.name的值和girl.age的值分别修改为yuxin2,age2( 即改变配置文件name和age的值)。
在这里插入图片描述
如果是传统的做法,需要重启服务,才能达到配置文件的更新。

第8步:我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh或者http://localhost:8882/actuator/bus-refresh
在这里插入图片描述
温馨提示:

  1. 在测试之前请先把config-server和config-client的控制台清空
  2. 通过postman或者命令窗口的方式进行http请求
  3. 你会发现config-client会重新读取配置文件

第9步:这时我们再访问:http://localhost:8881/girl/info或者http://localhost:8882/girl/info

  • 浏览器显示:
name:yuxin2 age:2

在这里插入图片描述

三、此时的架构:

在这里插入图片描述
流程简述:

  1. 当git文件更改的时候
  2. 通过pc端用post 向端口为8882的config-client发送请求/bus/refresh/
  3. 客户端8882接收到请求从Server端更新配置并且发送给Spring Cloud Bus
  4. Spring Cloud bus接到消息并通知给其它客户端
  5. 其它客户端接收到通知,请求Server端获取最新配置
  6. 全部客户端均获取到最新的配置,从而使整个微服务集群都达到更新配置文件。

架构演进优化:
在这里插入图片描述
根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:

1.提交代码触发post给客户端A发送bus/refresh
2. 客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus
3. Spring Cloud bus接到消息并通知给其它客户端
4. 其它客户端接收到通知,请求Server端获取最新配置
5. 全部客户端均获取到最新的配置,从而使整个微服务集群都达到更新配置文件。

本文源码下载:

dev分支(最新企业实战版本):
https://github.com/gb-heima/springcloud-practical-column/tree/dev/sc-f-chapter8

master分支(入门版本):
https://github.com/gb-heima/springcloud-practical-column/tree/master/sc-f-chapter8

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/524482.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IOS – OpenGL ES 图像哈哈镜效果 GPUImageStretchDistortionFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…

要闻君说:facebook迎来“全球宕机”惊险时刻;吸引大牛!拼多多成立了技术委员会;胡晓明卸任庚接任,阿里云计算迎来法人变更...

关注并标星星CSDN云计算每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 大家好&#xff01;偶是要闻君。近日网传原百度智能硬件事业部总经理、渡鸦科技创始人吕骋已成立AI娱乐公司一家&#xff0c;并已获得来自YCombinator中国等投资&#xff1b;据早前…

第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

这篇文章主要讲述服务追踪组件zipkin&#xff0c;Spring Cloud Sleuth集成了zipkin组件。 一、简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案&#xff0c;并且兼容支持了 zipkin&#xff0c;你只需要在pom文件中引入相应的依赖即可。 二、服务追踪分…

IOS – OpenGL ES 图像水晶球效果 GPUImageGlassSphereFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…

大数据下的中国女人,看完惊呆了

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者 | 辣道娘 转自 | 凯叔讲故事&#xff08;ID&#xff1a;kaishujianggushi&#xff09;这个时代的女人比任何一个时代更累。为什么这么呢&#xff1f;因为她们身兼多职。——凯叔美国国家统计局曾对各国劳动人口的总数和人口参…

IOS – OpenGL ES 图像球形折射 GPUImageSphereRefractionFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…

第十篇: 高可用的服务注册中心(Finchley版本)V2.0_dev

一、准备工作 Eureka通过运行多个实例&#xff0c;使其更具有高可用性。事实上&#xff0c;这是它默认的熟性&#xff0c;你需要做的就是给对等的实例一个合法的关联serviceurl。 二、创建eureka-server 引入依赖 <dependency><groupId>org.springframework.cl…

云漫圈 | 谈谈怎么做【服务隔离】

戳蓝字“CSDN云计算”关注我们哦&#xff01;转自&#xff1a; 孤独烟引言OK&#xff0c;如下图所示那显而易见&#xff0c;做服务隔离的目的就是避免服务之间相互影响。毕竟谁也不能说自己的微服务百分百可用&#xff0c;如果不做隔离&#xff0c;一旦一个服务出现了问题&…

linux搭建SonarQube_Oracle

文章目录一、安装声明二、下载软件2.1. sonarqube2.2. sonar-scanner-cli三、SonarQube实战3.1. 解压3.2. 配置3.3. 环境变量3.4. 启动Sonarqube3.5. 访问Sonarqube四、SonarQube 整合Oracle4.1. 创建命名空间4.2. 创建用户和赋予权限4.3. 添加数据库配置4.4. 添加Oracle数据库…

IOS – OpenGL ES 图像色调分离噪点效果 GPUImagePosterizeFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…

数据解读京东上最受欢迎的面包

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者&#xff1a; 黄超 就职顶新集团智能科技公司 数据从业者前言什么样的面包品牌最好卖&#xff1f;什么样的口感最受欢迎&#xff1f;相信每一位喜欢面包的朋友都会关心这些问题。本文通过爬取京东面包类数据&#xff0c;一方面回…

linux 下载mysql5.7.22

一、软件下载方式 第一种方式&#xff1a; 下载到本地&#xff0c;再从本地上传服务器上 第二种方式(推荐使用)&#xff1a; 使用wget软件链接直接下载到传服务器上 二、软件下载流程 官网地址 https://dev.mysql.com/downloads/mysql/ 第一种方式&#xff1a;点击即可…

IOS – OpenGL ES 图像CGA色彩滤镜 GPUImageCGAColorspaceFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…

大数据背后的无奈与焦虑:“128元连衣裙”划分矮穷挫与白富美?

戳蓝字“CSDN云计算”关注我们哦&#xff01;作者&#xff1a; 刘丹我们通过卖东西收集数据&#xff0c;数据是阿里最值钱的财富。——马云春天万物复苏&#xff0c;沉睡了一个冬季的爱美之心呼之欲出&#xff0c;连衣裙在姑娘的心里发了芽&#xff0c;不过这次有些意外&#x…

Linux安装SonarQube和sonar-scanner详细安装及配置

文章目录技术选型一、快速入门1.1 上传、解压、创建用户、启动1.2 创建用户(elasticsearch不能以root用户启动)&#xff0c;并赋予权限1.3 切换用户&#xff0c;启动SonarQube1.4. 浏览器验证二、配置数据库信息&#xff1a;2.1 编辑sonar.properties文件2.2 添加数据库信息&am…

IOS – OpenGL ES 图像柏林噪点/花边噪点 GPUImagePerlinNoiseFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…

win10 下安装、配置、启动mysql5.7

文章目录1. 下载MYSQL2. 解压文件3. 文件移动4. 新建my.ini5. 配置环境变量6. MYSQL安装7. my.ini配置1. 下载MYSQL 前期准备&#xff1a;安装链接&#xff1a; https://blog.csdn.net/qq_28569585/article/details/79072805 https://dev.mysql.com/downloads/mysql/5.7.html#d…

要闻君说:华为与故宫携手共建智慧紫禁城;央视315曝光瞄准大数据黑市;华为官宣称自己也有操作系统了;美国夸口启动6G研发?...

关注并标星星CSDN云计算每周三次&#xff0c;打卡即read更快、更全了解泛云圈精彩newsgo go go 大家好&#xff01;偶是要闻君。据说刚刚过去不久的315大曝光还真是“惊掉下巴”的节奏&#xff0c;这不要闻君也精选了一条&#xff0c;随着看看&#xff1f;文/要闻君近日&#x…

IOS – OpenGL ES 图像浮雕3d效果 GPUImageEmbossFilter

目录 一.简介二.效果演示三.源码下载四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 转场 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目…