SpringCloud组件之消息总线Bus(Hoxton版本)

574 阅读2分钟

1.Spring Cloud Bus简介

Spring Cloud Bus 是 Spring Cloud 体系内的消息总线,用来连接分布式系统的所有节点。 Spring Cloud Bus 将分布式的节点用轻量的消息代理(RibbitMQ、Kafka)连接起来。可以通过消息代理广播配置文件的更改,或服务之间的通讯,也可以用于监控。解决了微服务数据变更,及时同步的问题。 目前 Spring Cloud Bus 支持两种消息代理:RabbitMQ 和 Kafka,下面以 RabbitMQ 为例来演示下使用Spring Cloud Bus 动态刷新配置的功能。

2.搭建演示工程

2.1 环境准备

由于本文中使用 RabbitMQ 作为消息代理,故需要安装RabbitMQ,安装部署参考: blog.csdn.net/ThinkWon/ar…

本文案例基于上一篇文章中Config Server与Config Client工程改造。并启动Eureka Server。

2.2 Config Server工程改造

引入bus相关依赖,具体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>spring-cloud-demo</artifactId>
        <groupId>com.hxmec</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-config-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- config server依赖包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>


        <!-- Spring Cloud Bus-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在配置文件application.yml中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并需要加上spring.cloud.bus的配置,具体如下:

server:
  port: 9005
spring:
  application:
    name: config-server
  cloud:
    config:
      label: master
      server:
        git:
          #github配置地址
          uri: https://github.com/ty1972873004/spring-cloud-config.git
          #配置文件路径
          search-paths: appconfig
          #username:
          #password:
          #git分支
          default-label: master
    bus:
      enabled: true
      trace:
        enabled: true
  # 消息队列
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtual-host: /

# 度量指标监控与健康检查
management:
  endpoints:
    web:
      base-path: /actuator    # 访问端点根路径,默认为 /actuator
      exposure:
        include: bus-refresh  # 需要开启的端点

logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/

2.3 Config Client工程改造

引入bus相关依赖,具体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>spring-cloud-demo</artifactId>
        <groupId>com.hxmec</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-config-client</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- config client依赖包-->
        <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>

        <!-- SpringCloud Bus -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在配置文件application.yml中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并需要加上spring.cloud.bus的配置,具体如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://127.0.0.1:9005/
      label: master
      profile: dev
      name: ${spring.application.name}
      discovery:
        enabled: true
        service-id: config-server
    bus:
      enabled: true
      trace:
        enabled: true
  # 消息队列
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
    virtual-host: /

management:
  endpoints:
    web:
      exposure:
        include: 'refresh'
        
server:
  port: 9006

logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'

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

在获取动态配置的类或者启动类中添加@RefreshScope注解用于刷新配置

@RestController
@RequestMapping("/demo")
@Slf4j
@RefreshScope
public class DemoController {

    @Value("${hx.name:#{null}}")
    private String name;

    @GetMapping(value = "/test1")
    public String  test1(){
        return "Hello " + name;
    }
}

2.4 工程演示

启动Config Server,Config Client工程 此时配置文件如下 访问config client中接口http://127.0.0.1:9006/demo/test1 可以看下如下结果。 修改github中配置文件,如下 此时访问访问接口还是返回没有更改前的结果。此时,我们发送post请求:http://localhost:9005/actuator/bus-refresh ,再次请求接口http://127.0.0.1:9006/demo/test1 ,可以看下如下结果,说明验证通过。

3.项目git地址

github.com/ty197287300…

4.参考文档