消息总线 —— SpringCloud Bus

Bus 简介

Spring Cloud Bus 是 Spring Cloud 体系内的消息总线,支持 RabbitMQ 和 Kafka 两种消息中间件。所谓消息总线,简单理解就是一个消息中心,众多微服务实例都可以连接到总线上,实例可以往消息中心发送或接收信息,例如:实例 A 发送一条消息到总线上,总线上的实例 B 可以接收到信息(实例 B 订阅了实例 A),消息总线充当一个中间者的角色,使得实例 A 和实例 B 解耦


Spring Cloud Bus 实战

Spring Cloud Bus 可以将 Spring 事件机制和 Stream 结合在一起,具体机制如下:

  • 在需要发布或者监听事件的应用中增加 @RemoteApplicationEventScan 注解,通过该注解
    可以启动 Stream 中消息通道的绑定
  • 对于事件发布,需要承 ApplicationEvent 的扩展类 RemoteApplicationEvent,通过 ApplicationContext.publishEvent() 发布事件时,Spring Cloud Bus 会对所要发布的事件进行包装,形成消息,通过默认的 Spring Cloud Bus 消息通道发送到消息中间件
  • 对于事件监听者,则不需要进行任何变更,仍旧按照 Spring 的方式实现消息的监听i

安装并启动 ZooKeeper 和 Kafka,创建事件发布者项目,引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>

定义用户事件类 UserEvent,实现 RemoteApplicationEvent

@Data
@Slf4j
@EqualsAndHashCode(callSuper = true)
public class UserEvent extends RemoteApplicationEvent {

    public UserEvent(Object source, String originService, String destination) {
        super(source, originService, destination);
    }
}
  • originService:对于事件发布者来说 originService 就是自己
  • destinationService:将事件发布到哪些微服务实例,配置的格式为 {serviceld):{appContextId),在配置时 serviceld 和 appContextld 可以使用通配符,比如 userservice:** 会将事件发布给 userservice 微服务

发布消息代码如下

@Slf4j
@RestController
public class TestCon {

    @Autowired
    private ApplicationContextHolder holder;

    @GetMapping("/test/userEvent")
    public void userAdd() {
        User user = new User();
        user.setId("2");
        user.setName("tom");
        user.setAge(50);
        ApplicationContext ctx = ApplicationContextHolder.getApplicationContext();
        UserEvent event = new UserEvent(user, ctx.getId(), "*:**");
        ctx.publishEvent(event);
    }
}

在配置文件中添加如下配置:

spring:
  cloud:
    stream:
      default-binder: kafka
      kafka:
        binder:
          brokers: localhost:9092

在启动类添加 @RemoteApplicationEventScan 注解

@SpringBootApplication
@RemoteApplicationEventScan
public class Server01Application {

    public static void main(String[] args) {
        SpringApplication.run(Server01Application.class, args);
    }
}

创建事件接收者项目,引入和事件发布者同样的依赖,将 UserEvent 类复制到该模块下,实现事件监听类UserEventListener

@Slf4j
@Component
public class UserEventListener implements ApplicationListener<UserEvent> {

    @Override
    public void onApplicationEvent(UserEvent event) {
        log.info("收到用户事件: {}", event);
    }
}

加上事件发布者同样的配置和启动类注解

启动两个项目,请求事件发布者的 /test/userEvent 接口,即可发布和接收事件

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Bus是一个用于在分布式系统中传播状态变化的消息总线。它基于Spring Cloud Stream和Spring Cloud Config构建,可以将消息广播到整个系统中的所有服务实例。通过使用Spring Cloud Bus,可以实现配置的动态刷新、事件的传播和集群中的状态同步。 下面是使用Spring Cloud Bus自定义消息总线的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Cloud Bus的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ``` 2. 配置消息代理:在应用的配置文件中配置消息代理,例如使用RabbitMQ作为消息代理: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 发送自定义消息:在需要发送自定义消息的地方,使用Spring Cloud Bus提供的API发送消息。例如,可以使用`/actuator/bus-refresh`端点发送刷新配置的消息: ```shell curl -X POST http://localhost:8080/actuator/bus-refresh ``` 4. 接收自定义消息:在需要接收自定义消息的地方,使用Spring Cloud Bus提供的注解和监听器来接收消息。例如,可以使用`@RefreshScope`注解来刷新配置: ```java @RefreshScope @RestController public class ConfigController { // ... } ``` 通过以上步骤,您可以使用Spring Cloud Bus自定义消息总线来实现配置的动态刷新、事件的传播和集群中的状态同步。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值