zoukankan      html  css  js  c++  java
  • SpringCloud Bus 消息总线

    一、能干嘛?

    二、什么是总线?

    三、动态刷新全局广播

    注意:必须先具备良好的RabbitMQ环境

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

    1、新建cloud-config-client-3366

    1)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>cloud2020</artifactId>
            <groupId>com.atguigu.springcloud</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>
            <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.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</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>
    pom文件

    2)YML

    server:
      port: 3366
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          name: config
          profile: dev
          uri: http://localhost:3344
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    3)主启动

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

    4)controller

    @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+"	
    
     configInfo: "+configInfo;
        }
    }

    2、设计思想

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

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

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

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

    3、代码改造

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

    POM

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    增加mq依赖

    YML

    server:
      port: 3344
    
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri: https://github.com/jwen1994/sprincloud-config.git
              search-paths:
                - springcloud-config
              username: 526464869@qq.com
              password: wj_870096
          label: master
      rabbitmq:
        host: 192.168.216.130
        port: 5672
        username: admin
        password: password
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
    management:
      endpoints:
        web:
          exposure:
            include: "bus-refresh"

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

    POM

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    增加mq依赖

    YML

    server:
      port: 3355
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          name: config
          profile: dev
          uri: http://localhost:3344
      rabbitmq:
        host: 192.168.216.130
        port: 5672
        username: admin
        password: password
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"

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

    POM

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    增加mq依赖

    YML

    server:
      port: 3366
    
    spring:
      application:
        name: config-client
      cloud:
        config:
          label: master
          name: config
          profile: dev
          uri: http://localhost:3344
      rabbitmq:
        host: 192.168.216.130
        port: 5672
        username: admin
        password: password
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    4、测试

    • 修改Github上配置文件增加版本号
    • curl -X POST "http://localhost:3344/actuator/bus-refresh"
    • 一次发送,处处生效

    四、动态刷新定点通知

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

    • 只通知3355
    • 不通知3366

    公式:http://配置中心IP地址/actuator/bus-refresh/{destination}

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

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

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

    五、总结

  • 相关阅读:
    JVM
    Java反射和动态代理
    JMS(Java消息服务)
    Java多线程及并发
    zookeeper集群自动启动脚本
    zookeeper命令行客户端
    Zookeeper集群搭建步骤及相关知识点深入了解
    MySQL server PID file could not be found!
    Linux下jdk、Tomcat、MySQL的安装
    修改Linux的基本配置
  • 原文地址:https://www.cnblogs.com/jwen1994/p/14437967.html
Copyright © 2011-2022 走看看