zoukankan      html  css  js  c++  java
  • (十五)、SpringCloud Bus消息总线

    SpringCloud Bus消息总线

    Bus消息总线是什么

    一言以蔽之,分布式自动刷新配置功能。

    Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新。

    img

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

    能干嘛

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

    img

    为何被称为总线

    什么是总线

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

    基本原理

    ConfigClient实例都监听MQ中同一个topic(默认是Spring Cloud Bus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。

    Bus之RabbitMQ环境配置

    rabbitmq-plugins.bat enable rabbitmq_management
    

    这样就可以添加可视化插件。

    image-20210916112308042

    15672是管理界面端口 5672是服务端口

    Bus动态刷新全局广播的设计思想和选型

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

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

    1.新建cloud-config-client-3366

    2.POM

    <?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>cloud2021</artifactId>
            <groupId>com.ylc.cloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-config-client-3366</artifactId>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <!--添加消息总线RabbitMQ支持-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
            <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>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>
    

    3.YAML

    server:
      port: 3366
    
    spring:
      application:
        name: config-client
      cloud:
        #Config客户端配置
        config:
          label: master #分支名称
          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: "*"
    

    4.主启动类

    package com.ylc.cloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @SpringBootApplication
    public class ConfigClientMain3366
    {
        public static void main(String[] args)
        {
            SpringApplication.run(ConfigClientMain3366.class,args);
        }
    }
    

    5.controller

    package com.ylc.cloud.controller;
    
    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.RestController;
    
    /**
     */
    @RestController
    @RefreshScope
    public class ConfigClientController
    {
        @Value("${server.port}")
        private String serverPort;
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/configInfo")
        public String configInfo()
        {
            return "serverPort: "+serverPort+"	
    
     configInfo: "+configInfo;
        }
    
    }
    

    设计思想

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

    img

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

    img

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

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

    Bus动态刷新全局广播配置实现

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

    POM

    <!--添加消息总线RabbitNQ支持-->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-bus-amap</artifactId>
    </dependency>
    <dependency>
    	<groupId>org-springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    YML

    bootstrap.yml

    server:
      port: 3344
    
    spring:
      application:
        name:  cloud-config-center #注册进Eureka服务器的微服务名
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/yanglingcong/spring-cloud-config #GitHub上面的git仓库名字
              #https://github:com/zzyybs/springcloud-config:
              ####搜索目录
              search-paths:
                - springcloud-config
          ####读取分支
          label: master
    
    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    
    ##rabbitmq相关配置,暴露bus刷新配置的端点<--------------------------
    management:
      endpoints: #暴露bus刷新配置的端点
        web:
          exposure:
            include: 'bus-refresh'
    
    #rabbitmq相关配置<--------------------------
    rabbitmq:
      host: localhost
      port: 5672
      username: guest
    

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

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        #Config客户端配置
        config:
          label: master #分支名称
          name: config #配置文件名称
          profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
          uri: http://localhost:3344 #配置中心地址k
    
    
    #服务注册到eureka地址
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka
    
    # 暴露监控端点
    management:
      endpoints:
        web:
          exposure:
            include: "*"
    
      #rabbitmq相关配置 15672是Web管理界面的端口;5672是MQ访问的端口<----------------------
      rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    
    

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

    server:
      port: 3366
    
    spring:
      application:
        name: config-client
      cloud:
        #Config客户端配置
        config:
          label: master #分支名称
          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: "*"
    

    测试

    • 启动

      • EurekaMain7001
      • ConfigcenterMain3344
      • ConfigclientMain3355
      • ConfigclicntMain3366
    • 修改Github上配置文件内容,增加版本号,发送POST请求

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

    —次修改,广播通知,处处生效

    Bus动态刷新定点通知

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

    • 只通知3355
    • 不通知3366

    简单一句话 - 指定具体某一个实例生效而不是全部

    通知总结

    img

  • 相关阅读:
    三剑客之Grep家族
    Linux默认权限命令:umask
    Linux特殊权限管理SUID|SGID|STICKY
    zabbix的web界面出现乱码解决方案
    启动zabbix-server/agent报错:cannot open "/etc/zabbix/logs/zabbix_server.log": [13] Permission denied
    Zabbix-Agent配置文件详解
    使用yum安装报错:[Errno 256] No more mirrors to try
    grep/sed/awk命令查看指定时间段的日志
    数据库SQL练习(一):数据查询
    连接数据库报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  • 原文地址:https://www.cnblogs.com/cg-ww/p/15359039.html
Copyright © 2011-2022 走看看