前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringCloud Bus 消息总线

SpringCloud Bus 消息总线

作者头像
OY
发布2022-03-17 17:45:36
4900
发布2022-03-17 17:45:36
举报
文章被收录于专栏:OY_学习记录OY_学习记录

博客学习参考视频

一、概述

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

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

② 是什么

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

③ 能干嘛

④ 为何被称为总线

二、RabbitMQ 环境配置

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

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

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

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

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

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

新建: cloud-config-client-3366

代码语言:javascript
复制
<?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)

代码语言:javascript
复制
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: "*"

主启动

代码语言:javascript
复制
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
     public static void main(String[] args) {
           SpringApplication.run(ConfigClientMain3366.class, args);
      }
}

3.设计思想

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

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

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

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

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

POM

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

YML

代码语言:javascript
复制
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

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

YML

代码语言:javascript
复制
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

代码语言:javascript
复制
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

YML

代码语言:javascript
复制
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 请求
代码语言:javascript
复制
curl -X POST "http://localhost:3344/actuator/bus-refresh"

一次发送, 处处生效

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

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

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

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

只通知 3355 不通知 3366

2.简单一句话

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

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

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

3.案例

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

只通知 3355,不通知 3366

代码语言:javascript
复制
curl -X POST http://localhost:3344/actuator/bus-refresh/config-client:3355

4.通知总结 All

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、概述
    • ① 上一讲解的加深和扩充, 一言以蔽之
      • ② 是什么
        • ③ 能干嘛
          • ④ 为何被称为总线
          • 二、RabbitMQ 环境配置
          • 三、SpringCloud Bus 动态刷新全局广播
            • 1.必须先具备良好的 RabbitMQ 环境
              • 2.演示广播效果, 增加复杂度, 再以 3355 为模板再制作一个 3366
                • 3.设计思想
                  • 4.给 cloud-config-center-3344 配置中心服务端添加消息总线支持
                    • 5.给 cloud-config-center-3355 客户端添加消息总线支持
                      • 6.给 cloud-config-center-3366 客户端添加消息总线支持
                        • 7.测试
                          • 8.一次修改, 广播通知, 处处生效
                          • 四、SpringCloud Bus 动态刷新定点通知
                            • 1.不想全部通知, 只想定点通知
                              • 2.简单一句话
                                • 3.案例
                                相关产品与服务
                                微服务引擎 TSE
                                微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档