SpringCloud微服务(七)——Bus服务消息总线

SpringCloud Bus动态刷新全局广播

SpringCloud

Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新,通知一处,处处生效。而不用一个一个去通知。

Spring Cloud Bus是消息总线,广播通知都可以集成,不止用于实现配置的动态刷新。

简介

分布式自动刷新配置功能

SpringCloud Bus是用来将分布式系统的节点与轻量级消息连接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。

SpringCloud Bus目前支持RabbitMQ和Kafka。

在这里插入图片描述

什么是总线:

在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由该主题中产生的消息会所有的实例监听和消费,所以称它为消息总线,在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的商息。

ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus).当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic们服务就能得到通知,然后去更新自身的配置。
https://www.bilibilicom/ideo/avss9767007from=search&seid=15010075915728605208

在这里插入图片描述

设计思想

2种:

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

在这里插入图片描述

二、利用消息总线触发一个服务端ConfigServer的/bus/refesh端点,而刷新所有客户端的配置

在这里插入图片描述

显然第二种方式更加适合,第一种不适合的原因如下:

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

实现配置的动态刷新

这里是运用rabbitmq消息中间件的,先搭建rabbitmq环境

客户端

创建多个客户端微服务,测试范例:

依赖配置:

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

<!-- config -->
<dependency>      
    <groupId>org.springframework.cloud</groupId>      
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>            
    <groupId>org.springframework.boot</groupId          
    <artifactId>spring-boot-starter-actuator</artifactId>    
</dependency>

bootstrap.yml

server:
  port: 3366

spring:
#    application:
#      name: config-client3366
  cloud:
    config:
      label: master # 分支名称
      name: application # 配置文件名称,可自定义,最好application/config
      profile: dev # 文件中的dev profile内容,上述三个综合,为master分支上的application.yml的配置文件中的dev内容被读取http://localhost:3344/application/dev/master
      uri: http://localhost:3344 #配置中心的地址

  rabbitmq: #rabbitmq相关配置,15672是web管理端口,5672是mq访问端口
    port: 5672
    host: 192.168.169.135
    username: guest
    password: guest

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

management:
  endpoints:
    web:
      exposure:
        include: "*"

controller

/**
 *
 * @author zzyy
 * @version 1.0
 * @create 2020/03/06
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/applicationName")
    public String getApplicationName(){
        return serverPort + ":  " + applicationName;
    }

}

服务端

依赖配置:

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

<!-- config server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

<dependency>            
    <groupId>org.springframework.boot</groupId          
    <artifactId>spring-boot-starter-actuator</artifactId>    
</dependency>

application.yml

server:
  port: 3344

spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/wzq-55552/springcloud-config.git #配置远程的仓库地址springcloud-config
          search-paths:
            - springcloud-config
      label: master #进到仓库的主分支

  rabbitmq:
    host: 192.168.169.135
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

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

启动类加入注解@EnableConfigServer

测试

微服务全部启动

修改github上的配置文件内容:

spring:
  profiles: dev     #开发环境
  application: 
    name: github-dev-config-bus-application-name-2

服务端可以同步改变,但是客户端得重新启动才可以同步修改,默认客户端是无法立即生效的,查看controller:

在这里插入图片描述

这时运维人员执行(3344是配置中心的端口):curl -X POST "http://localhost:3344/actuator/bus-refresh"可以cmd执行测试,这里我使用postman:

http://localhost:3344/actuator/bus-refresh 必须是post请求

在这里插入图片描述

再一次查看所有客户端controller(返回配置信息):

在这里插入图片描述

查看rabbitmq的交换机,这时默认有了springCloudBus交换机:

在这里插入图片描述

动态刷新定点通知

不想全部通知,只想定点通知,只通知3355,不通知3366。

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

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

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

curl -X POST “http://localhost:3344/actuator/bus-refresh/微服务名:微服务端口”

后面格式是微服务名:微服务端口

测试:

修改测试客户端:

    @Value("${server.port}")
    private String serverPort;

    @Value("${spring.config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return serverPort + ":  " + configInfo;
    }

微服务配置修改为获取test环境,全部启动

github修改内容,初始test:

在这里插入图片描述

postman 发送post请求http://localhost:3344/actuator/bus-refresh/config-client3355:3355 修改3355微服务的配置:

在这里插入图片描述

在这里插入图片描述

定点通知只能修改一个微服务的配置

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Cloud是一个基于Spring Boot的微服务框架,它提供了一整套用于构建和管理微服务的组件。下面是Spring Cloud微服务架构图的介绍: 1. 配置管理(Config Server):用于集中管理微服务的配置信息,可以将配置信息存储在Git、SVN等版本控制系统中,并通过Config Server进行统一管理和分发。 2. 服务注册与发现(Service Discovery):微服务架构中的各个服务需要能够自动注册和发现,以便实现服务之间的通信。Spring Cloud提供了服务注册与发现的组件,如Eureka、Consul等。 3. 断路器(Circuit Breaker):在微服务架构中,当某个服务出现故障或不可用时,为了避免级联故障,需要使用断路器模式进行故障隔离。Spring Cloud中的Hystrix组件提供了断路器的实现。 4. 智能路由(Intelligent Routing):微服务架构中的服务可能会有多个实例,智能路由可以根据负载均衡策略将请求分发到不同的实例上,以实现负载均衡。Spring Cloud中的Ribbon和Zuul组件提供了智能路由的功能。 5. 微代理(Micro Proxy):微服务架构中的服务之间需要进行通信,为了简化通信过程,可以使用微代理来处理请求和响应。Spring Cloud中的Feign组件提供了微代理的功能。 6. 控制总线(Control Bus):微服务架构中的各个服务需要能够实时通信和协调,控制总线可以提供消息传递和事件驱动的机制。Spring Cloud中的Spring Cloud Bus组件提供了控制总线的功能。 7. 全局锁(Distributed Lock):在分布式环境中,为了保证数据的一致性和并发控制,需要使用全局锁。Spring Cloud中的Zookeeper和Redis等组件提供了全局锁的实现。 8. 决策竞选(Leader Election):在分布式环境中,为了保证系统的高可用性,需要选举出一个主节点来处理请求。Spring Cloud中的Zookeeper和Election等组件提供了决策竞选的功能。 9. 分布式会话(Distributed Session):在微服务架构中,用户的会话信息可能需要在多个服务之间共享。Spring Cloud中的Spring Session组件提供了分布式会话的支持。 10. 集群状态管理(Cluster State Management):在微服务架构中,需要对集群中的各个服务进行监控和管理。Spring Cloud中的Spring Cloud Cluster组件提供了集群状态管理的功能。 以上是Spring Cloud微服务架构图的介绍,希望对你有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wzq_55552

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值