Spring Cloud:Config+Bus 配置中心和消息总线(广播-定点)

    技术2022-07-14  78

    概述:

    Spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。目前唯一实现的方式是用AMQP消息代理作为通道,同样特性的设置(有些取决于通道的设置)在更多通道的文档中。

    Spring cloud bus被国内很多都翻译为消息总线,也挺形象的。大家可以将它理解为管理和传播所有分布式项目中的消息既可,其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。

    环境准备

    RabbiMQ

    docker pull rabbitmq:3-management docker run -d -p 5672:5672 -p 15672:15672 --name=sb_rabbimq RabbitMQ的ID

    consul

    Config-Center

    POM

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-consul-discovery</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>

    YAML

    management.endpoints.web.exposure.include:"bus-refresh":暴露bus-refresh端点,利用消息总线(Bus)触发ConfigCenter的bus-refresh端点来刷新所有客服端的配置
    server: port: 3344 spring: application: name: config-center-bus cloud: consul: host: localhost port: 8500 discovery: prefer-ip-address: true service-name: ${spring.application.name} config: server: git: uri: https://github.com/DeadAndLive/springcloud-config.git search-paths: - springcloud-config label: master rabbitmq: host: 123.57.x.x password: x username: x management: endpoints: web: exposure: include: "bus-refresh"

    启动类

    @EnableConfigServer
    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigCenterBusMain3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterBusMain3344.class,args); } }

    Config-Client(集群,端口一个3355,一个3366)

    POM

    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-consul-discovery</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>

    YAML

    server: port: 3355 spring: application: name: config-client-bus cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name} prefer-ip-address: true config: label: master name: application profile: dev uri: http://localhost:3344 rabbitmq: host: 123.57.x.x password: xxx username: xxx management: endpoints: web: exposure: include: "*"

    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 Controller3355 { @Value("${config.info}") private String configInfo; @Value("${server.port}") private String serverPort; @GetMapping(value = "/getConfigInfo") public String getConfigInfo() { return "Server Port: " + serverPort + "==>" + configInfo; } }

    测试

    广播通知

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

    定点通知

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

    公式:

    curl -X POST "http://配置中心地址/actuator/bus-refresh/微服务名:端口"
    Processed: 0.024, SQL: 9