Bus消息总线

好了现在我们接着上一篇的随笔,继续来讲。上一篇我们讲到,我们如果要去更新所有微服务的配置,在不重启的情况下去更新配置,只能依靠spring cloud config了,但是,是我们要一个服务一个服务的发送post请求,

我们能受的了吗?这比之前的没配置中心好多了,那么我们如何继续避免挨个挨个的向服务发送Post请求来告知服务,你的配置信息改变了,需要及时修改内存中的配置信息。

这时候我们就不要忘记消息队列的发布订阅模型。让所有为服务来订阅这个事件,当这个事件发生改变了,就可以通知所有微服务去更新它们的内存中的配置信息。

这时Bus消息总线就能解决,你只需要在springcloud Config Server端发出refresh,就可以触发所有微服务更新了。

Spring Cloud Bus除了支持RabbitMQ的自动化配置之外,还支持现在被广泛应用的Kafka。在本文中,我们将搭建一个Kafka的本地环境,并通过它来尝试使用Spring Cloud Bus对Kafka的支持,实现消息总线的功能。

window下安装kafka和zooker,超详细:https://blog.csdn.net/weixin_33446857/article/details/81982455

kafka:安装下载教程网址(CentOS Linux):https://www.cnblogs.com/subendong/p/7786547.html

zooker的下载安装网址:https://blog.csdn.net/ring300/article/details/80446918

项目: 一个服务端。2个客户端。

pom文件:

<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.demo</groupId>
<artifactId>springcloud_serviceClient2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</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>Finchley.RELEASE</spring-cloud.version>
</properties> <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> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<!--Spring Boot Actuator,感应服务端变化-->
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

client1的配置文件要改为bootstrap.yml,因为这种配置格式,是优先加载的,上一篇随笔有讲过,client2的配置如下:

server:
port: 7008
spring:
application:
name: config2
cloud:
config:
label: master
#启动什么环境下的配置,dev 表示开发环境,这跟你仓库的文件的后缀有关,比如,仓库配置文件命名格式是cloud-config-dev.properties,所以profile 就要写dev
profile: dev
name: cloud-config
discovery:
enabled: true
#这个名字是Config Server端的服务名字,不能瞎写。
service-id: CONFIG-SERVER
management:
#是否需要权限拉去,默认是true,如果不false就不允许你去拉取配置中心Server更新的内容
security:
enabled: false
#注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/

启动类:

package cn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication2 { public static void main(String[] args) {
SpringApplication.run(ClientApplication2.class, args);
System.out.println("启动成功!");

controller类:

package cn.demo.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
@RefreshScope
public class Controller { /*@Value("${spring.cloud}")
private String config; @GetMapping("/test/config")
public String test() {
return config;
}*/ @Value("${name}")
private String name;
@Value("${age}")
private Integer age; @RequestMapping("/test")
public String test(){
return this.name+this.age;
} }

接着将client2中的的代码基本和client的一样,只是暴露服务的端口不同。

然后:

把zooker,kafka启动;(标题下面链接)

把前面的工程,1个注册中心,一个springcloud-config-server,两个springcloud-config-client,springcloud-config-client1启动起来,

可以看到springcloudBus是在0分片上,如果两个config-client启动都出现上面信息,证明启动成功了。

访问:http://localhost:7000/cloud-config-dev.properties

再访问两个client,如下:

http://localhost:7006/test      http://localhost:7008/test

好了,好戏开始了,现在我们去git仓库上修改配置中心的文件,将年龄改为24,如下:

接下来,我们我们用refresh刷新配置服务端7000配置,通知两个client去更新内存中的配置信息。用postman发送localhost:7000/bus/refresh,如下:

注意:spring1.x和spring2.x 刷新的路径不一样。

----------------------------------------------------

看一下返回的结果:

可以看到没有返回什么信息,但是不要担心,这是成功的通知所有client去更新了内存中的信息了。

接着我们分别重新请求config-server,两个client,刷新页面,结果如下:

到目前为止,上面都是刷新说有的配置的信息的,如果我们想刷新某个特定服务的配置信息也是可以的。我们可以指定刷新范围,如下:

指定刷新范围

  上面的例子中,我们通过向服务实例请求Spring Cloud Bus的/bus/refresh接口,从而触发总线上其他服务实例的/refresh。但是有些特殊场景下(比如:灰度发布),我们希望可以刷新微服务中某个具体实例的配置。

  Spring Cloud Bus对这种场景也有很好的支持:/bus/refresh接口还提供了destination参数,用来定位具体要刷新的应用程序。比如,我们可以请求/bus/refresh?destination=服务名字:9000,此时总线上的各应用实例会根据destination属性的值来判断是否为自己的实例名,

若符合才进行配置刷新,若不符合就忽略该消息。

  destination参数除了可以定位具体的实例之外,还可以用来定位具体的服务。定位服务的原理是通过使用Spring的PathMatecher(路径匹配)来实现,比如:/bus/refresh?destination=customers:**,该请求会触发customers服务的所有实例进行刷新。

SpringCloud(八):springcloud-bus消息总线(刷新配置服务)的更多相关文章

  1. SpringCloud学习之Bus消息总线实现配置自动刷新(九)

    前面两篇文章我们聊了Spring Cloud Config配置中心,当我们在更新github上面的配置以后,如果想要获取到最新的配置,需要手动刷新或者利用webhook的机制每次提交代码发送请求来刷新 ...

  2. 多项目如何高效协同合作 | springcloud系列之bus消息总线

    前言 在springcloud config章节中我们完成了配种中心的搭建,以及通过配置中心完成配置的抽离通过springcloud config模块我们将配置抽离到git仓库中我们不必要每次为了改配 ...

  3. 跟我学SpringCloud | 第八篇:Spring Cloud Bus 消息总线

    SpringCloud系列教程 | 第八篇:Spring Cloud Bus 消息总线 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特 ...

  4. SpringCloud之Config配置中心+BUS消息总线原理及其配置

    一.配置中心作用 在常规的开发中,每个微服务都包含代码和配置.其配置包含服务配置.各类开关和业务配置.如果系统结构中的微服务节点较少,那么常规的代码+配置的开发方式足以解决问题.当系统逐步迭代,其微服 ...

  5. [转]springcloud(九):配置中心和消息总线(配置中心终结版)

    https://www.cnblogs.com/ityouknow/p/6931958.html springcloud(九):配置中心和消息总线(配置中心终结版) 我们在springcloud(七) ...

  6. SpringCloud(六)Bus消息总线

    Bus 消息总线 概述 分布式自动刷新配置功能 Spring Cloud Bus 配合 Spring Cloud Config使用可以实现配置的动态刷新 Bus支持两种消息代理:RabbitMQ和Ka ...

  7. Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)

    详见:https://www.w3cschool.cn/spring_cloud/spring_cloud-jl8a2ixp.html 上一篇文章,留了一个悬念,Config Client 实现配置的 ...

  8. Spring Cloud 系列之 Bus 消息总线

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

  9. spring cloud bus 消息总线 动态刷新配置文件 【actuator 与 RabbitMQ配合完成】

    1.前言 单机刷新配置文件,使用actuator就足够了 ,但是 分布式微服务 不可能是单机 ,将会有很多很多的工程 ,无法手动一个一个的发送刷新请求, 因此引入了消息中间件 ,常用的 消息中间件 是 ...

随机推荐

  1. Python之利用Whoosh搭建轻量级搜索

      本文将简单介绍Python中的一个轻量级搜索工具Whoosh,并给出相应的使用示例代码. Whoosh简介   Whoosh由Matt Chaput创建,它一开始是一个为Houdini 3D动画软 ...

  2. 关于ConfigurationSection自定义config的简单使用

    1.1.自定义config结构(参考对应颜色标注),放到configuration根节点下: <test> <testInfos> <" /> <& ...

  3. windows10 双系统安装后,grub2 引导修复(亲自实验);grub2 命令行 手动加载内核;fedora 29 系统grub2引导修复;

    之前在本机有fedora 29的系统,但是由于错误安装,把windows10 启动安装到 linux 所在的硬盘中,导致原来的 efi中的grub启动被破坏,不能进入到linux中:我首先通过磁盘精灵 ...

  4. Mac OS X 快速添加新文件

    本文为 automator (中文名为 自动操作)的练习之作,尚有许多不足. 如果你想在 OS X 系统中快速添加新文件可直接参考此文 简介 本文使用 automator 创建了一个应用程序 auto ...

  5. Java之封装与访问权限控制(二)

    目录 Java之封装与访问权限控制(二) 包:库单元 import import static Java常用包 Java之封装与访问权限控制(二) 访问权限控制是具体实现的隐藏,是封装性的一部分体现. ...

  6. netcore 2.2 使用 Autofac 实现自动注入

    Autofac自动注入是通过名称约定来实现依赖注入 ps:本demo接口层都以“I”开头,以“Service”结尾.服务层实现都以“Service”结尾. 为什么要实现自动注入 大多时候,我们都是 以 ...

  7. Python3 pickle模块用法

    pickle(python3.x)和cPickle(python2.x的模块)相当于java的序列化和反序列化操作. 常采用下面的方式使用: import pickle pickle.dump(obj ...

  8. SpringBoot系列随笔 - BootJar的启动方式

    前言 写完maven的加密插件后,尝试在boot启动时的类加载过程中编写解密代码时,发现了一个平常没有注意的地方. 那就是boot-jar的启动方式与我们平常编写的可执行jar是存在很大差别的. 所以 ...

  9. js-07-事件

    一.js事件绑定在对象上的三种方法 a:将事件绑定在元素标签的属性上 <h3 onclick="console.log('奥特曼打怪兽')">海绵宝宝历险记</h ...

  10. FCC---CSS Flexbox: Add Flex Superpowers to the Tweet Embed

    To the right is the tweet embed that will be used as the practical example. Some of the elements wou ...