栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

六、服务总线

六、服务总线

一、Spring Cloud Bus 1. 是什么

分布式自动刷新配置;
Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新;

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

2. 能干嘛

Spring Cloud Bus 能管理 和 传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道。

3. 什么是总线

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

  • 基本原理
    Config Client 实例都监听 MQ中同一个 Topic(默认是Spring Cloud Bus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一 Topic的服务就能得到通知,然后去更新自身的配置。
4. WIndows安装 RabbitMQ 4.1 安装Erlang




4.2 安装RabbitMQ


4.3 启动RabbitMQ

rabbitmq-plugins enable rabbitmq_management

4.4 访问

http://localhost:15672/
用户名:guest,密码:guest

5. 动态刷新全局广播通知 5.1 设计思想
  1. 利用消息总线触发一个客户端 Config Client的 /bus/refresh端点,而刷新所有客户端的配置;
  2. 【推荐】利用消息总线触发一个服务端 Config Server的 /bus/refresh端点,而刷新所有客户端的配置;
  • 图二的架构显然更加适合,图一不适合的原因如下:
  1. 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新的职责;
  2. 破坏了微服务各节点的对等性;
  3. 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。
5.2 修改Module-cloud-config-center3344

给配置中心服务端添加消息总线支持;

5.2.1 POM

    org.springframework.cloud
    spring-cloud-starter-bus-amqp

5.2.2 YML
spring:
  # RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
  rabbitmq:
    host: 192.168.137.155
    port: 5672
    username: guest
    password: guest

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
		# 暴露bus刷新配置的端点
        include: 'bus-refresh'
5.3 修改Module-cloud-config-client3355

给客户端添加消息总线支持;

5.3.1 POM

    org.springframework.cloud
    spring-cloud-starter-bus-amqp

5.3.2 YML(bootstrap.yml)
spring:
  # RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
  rabbitmq:
    host: 192.168.137.155
    port: 5672
    username: guest
    password: guest

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
5.4 新建Module-cloud-config-client3366

给客户端添加消息总线支持;

5.4.1 POM


    
        cloud-2020
        com.qs.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config-client3366

    
    	
		
		    org.springframework.cloud
		    spring-cloud-starter-bus-amqp
		
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    


5.4.2 YML(bootstrap.yml)
server:
  port: 3366

spring:
  application:
    name: cloud-config-client
    
  cloud:
    # Config客户端配置http://config3344.com:3344/master/config-dev.yml
    config:
      # 分支名称
      label: master
      # 配置文件名称
      name: config
      # 读取后缀名称
      profile: dev
      # 配置中心地址
      uri: http://localhost:3344
   
  # RabbitMQ 相关配置,15672是Web管理界面的端口,5672是MQ访问的端口
  rabbitmq:
    host: 192.168.137.155
    port: 5672
    username: guest
    password: guest

eureka:
  client:
    service-url:
      # 注册中心地址
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
5.4.3 主启动
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3366 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3366.class, args);
    }
}
5.4.4 ConfigClientController
@RestController
@RefreshScope
public class ConfigClientController {

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

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

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return "serverPort: " + serverPort + "tnnconfigInfo: " + configInfo;
    }
}
5.5 测试
  1. 修改Github上配置文件增加版本号;
  2. curl -X POST “http://localhost:3344/actuator/bus-refresh”
  3. 配置中心
    http://config3344.com:3344/config-dev.yml
  4. 客户端
    http://localhost:3355/configInfo
    http://localhost:3366/configInfo
    一次修改,广播通知,处处生效。

6. 动态刷新定点广播通知
  • 不想全部通知,只通知3355,不通知3366;
    公式:http://localhost:3344/actuator/bus-refresh/{destination}
  • /bus/refresh请求不再发送到具体的服务实例上,而是发给 Config Server,并通过 destination参数类指定需要更新配置的服务或实例;
6.1 测试

刷新3355
curl -X POST “http://localhost:3344/actuator/bus-refresh/cloud-config-client:3355”

7. 通知总结

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/327071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号