SpringCloud Bus 消息总线

博客参考学习视频: https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=4388336378730572330

上一篇SpringCloud config 分布式配置中心 https://blog.csdn.net/qq_45738810/article/details/109190817

一、概述

① 上一讲解的加深和扩充, 一言以蔽之

​ 分布式自动刷新配置功能,Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动
态刷新。

② 是什么

image-20201020153327094

Bus 支持两种消息代理: RabbitMQ 和 Kafka

③ 能干嘛

image-20201020153357948

④ 为何被称为总线

image-20201020153420586

二、RabbitMQ 环境配置

安装采用的是 Linux CentOS7 的 Docker 容器,具体安装请参考这篇博客:https://oy6090.top/2020/10011634411798.html

安装完成之后,测试:你的linux地址:15672

image-20201020205507919

输入账号密码并登录: guest guest

三、SpringCloud Bus 动态刷新全局广播

1.必须先具备良好的 RabbitMQ 环境

2.演示广播效果, 增加复杂度, 再以 3355 为模板再制作一个 3366

新建: cloud-config-client-3366

<?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">
    <parent>
        <artifactId>clould</artifactId>
        <groupId>com.oy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3366</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oy</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

YML (bootstrap.yml)

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称 ,注意最新GitHub对master进行了调整,改为main了。
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址

#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

主启动

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
     public static void main(String[] args) {
           SpringApplication.run(ConfigClientMain3366.class, args);
      }
}

3.设计思想

  1. 利用消息总线触发一个客户端 /bus/refresh , 而刷新所有的客户端的配置

image-20201020211346665

2.利用消息总线触发一个服务端 ConfigServer 的 /bus/refresh 端点,而刷新所有客户端的配置(更加推荐)

image-20201020211514950

图二的架构显然更加合适, 图一不适合的原因如下

  1. 打破了微服务的职责单一性,因为微服务本身的业务模块,它本身不应该承担配置刷新职责。
  2. 破坏了微服务个节点的对等性
  3. 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。

4.给 cloud-config-center-3344 配置中心服务端添加消息总线支持

POM

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

YML

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://github.com/18731049401/springcloud-config.git #GitHub上面的git仓库名字
        ####搜索目录
          search-paths:
            - springcloud-config
      ####读取分支
      label: main # 注意最新GitHub对master进行了调整,改为main了。
 
 
#rabbitmq相关配置
 rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

##rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'

5.给 cloud-config-center-3355 客户端添加消息总线支持

POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

YML

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址k

#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

6.给 cloud-config-center-3366 客户端添加消息总线支持

POM

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

YML

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: main #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址

#rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

7.测试

  • 修改 Github 上配置文件增加版本号
  • 发送 Post 请求

image-20201020215950394

curl -X POST "http://localhost:3344/actuator/bus-refresh"

一次发送, 处处生效

  • 配置中心 http://config-3344.com/config-dev.yml

image-20201020220828382

  • 客户端 http://localhost:3355/configInfo , http://localhost:3366/configInfo

image-20201020220841648

image-20201020220855665

获取配置信息, 发现都已经刷新了

8.一次修改, 广播通知, 处处生效

四、SpringCloud Bus 动态刷新定点通知

1.不想全部通知, 只想定点通知

只通知 3355
不通知 3366

2.简单一句话

指定具体某一个实例生效而不是全部

公 式 : http://localhost: 配 置 中 心 的 端 口 号 /actuator/bus-refresh/{destination}

/bus/refresh 请求不再发送到具体的服务实例上, 而是发给 config server 并通过 destination 参数类指定需要更新配置的服务或实例

3.案例

我们这里以刷新运行在 3355 端口上的 config-client 为例

只通知 3355,不通知 3366

curl -X POST http://localhost:3344/actuator/bus-refresh/config-client:3355  

image-20201020220718869

4.通知总结 All

image-20201020220737963

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值