消息驱动 —— SpringCloud Stream

Stream 简介

Spring Cloud Stream 是用于构建消息驱动的微服务应用程序的框架,提供了多种中间件的合理配置

Spring Cloud Stream 包含以下核心概念:

  • Destination Binders:目标绑定器,目标指的是 Kafka 或者 RabbitMQ,绑定器就是封装了目标中间件的包,如果操作的是 Kafka,就使用 Kafka Binder,如果操作的是 RabbitMQ,就使用 RabbitMO Binder
  • Bindings:外部消息传递系统和应用程序之间的桥梁,提供消息的"生产者"和"消费者"(由目标绑定器创建)
  • Message:一种规范化的数据结构,生产者和消费者基于这个数据结构通过外部消息系统与目标绑定器和其他应用程序通信

应用程序通过 inputs 或者 outpus 与 Spring Cloud Stream 的 Binder 交互,Binder 层负责和中间件的通信,通过配置来 binding。通过定义 Binder 作为中间层,实现了应用程序与消息中间件细节之间的隔离,应用程序不需要再考虑各种不同的消息中间件实现。当需要升级消息中间件或是更换其他消息中间件产品时,只需要更换对应的 Binder 绑定器

Stream 整合 kafka

以 Kafka 为例,确保安装 Kafka 并启动

分别创建生产者和消费者项目,分别添加依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>
1. 创建生产者

开发 MqSource 接口

java 复制代码
public interface MqSource {

    @Output("test-topic")
    MessageChannel testTopic();

    @Output("test-topic-2")
    MessageChannel testTopic2();
}

通过 @Output@Input 注解定义消息输入和输出通道的名称定义,输出通道需要返回 MessageChannel 接口对象,它定义了向消息通道发送消息的方法。默认情况下,通道的名称就是注解的方法的名称,也能自己定义通道名称,只需要给 @Input@Output 注解传入 String 类型参数通道名称即可,这里指定两个通道分别为 test-topictest-topic-2

开发 MsgProducer 类

java 复制代码
@Slf4j
@EnableBinding(MqSource.class)
public class MsgProducer {

    @Autowired
    private MqSource mqSource;

    public void sendTestTopicMessage(String msg) {
        try {
            mqSource.testTopic().send(MessageBuilder.withPayload(msg).build());
        } catch (Exception e) {
            log.error("sendTestTopicMessage error", e);
        }
    }

    public void sendTestTopic2Message(String msg) {
        try {
            mqSource.testTopic2().send(MessageBuilder.withPayload(msg).build());
        } catch (Exception e) {
            log.error("sendTestTopic2Message error", e);
        }
    }
}

使用 @EnableBinding 创建和绑定通道,绑定通道是指将通道和 Binder 进行绑定,比如 Kafka、RabbiMQ 等。如果类路径下只有一种 Binder,那么 Spring Cloud Stream 会找到并绑定它,不需要进行配置。如果有多个就需要明确配置

调用 MqSource 接口方法获取输出通道对象,接着调用 send 方法发送数据。send 方法接收一个 Message 对象,这个对象不能直接新建,需要使用 MessageBuilder 获取

2. 创建消费者
java 复制代码
public interface MqSink {

    @Input("test-topic")
    MessageChannel testTopic();

    @Input("test-topic-2")
    MessageChannel testTopic2();
}

与生产者的 MqSource 同理

开发 MsgReceiver 类,@StreamLisiener 接收的参数是要处理的通道名,所注解的方法就是处理从通道获取数据的方法,方法的参数就是获取到的数据

java 复制代码
@Slf4j
@EnableBinding(MqSink.class)
public class MsgReceiver {

    @StreamListener("test-topic")
    public void testTopicMessageListen(String msg) {
        log.info("testTopicMessageListen: {}", msg);
    }

    @StreamListener("test-topic-2")
    public void testTopic2MessageListen(String msg) {
        log.info("testTopic2MessageListen: {}", msg);
    }
}
相关推荐
追逐时光者5 小时前
推荐 12 款开源美观、简单易用的 WPF UI 控件库,让 WPF 应用界面焕然一新!
后端·.net
Jagger_5 小时前
敏捷开发流程-精简版
前端·后端
苏打水com6 小时前
数据库进阶实战:从性能优化到分布式架构的核心突破
数据库·后端
间彧7 小时前
Spring Cloud Gateway与Kong或Nginx等API网关相比有哪些优劣势?
后端
间彧7 小时前
如何基于Spring Cloud Gateway实现灰度发布的具体配置示例?
后端
间彧7 小时前
在实际项目中如何设计一个高可用的Spring Cloud Gateway集群?
后端
间彧7 小时前
如何为Spring Cloud Gateway配置具体的负载均衡策略?
后端
间彧7 小时前
Spring Cloud Gateway详解与应用实战
后端
EnCi Zheng9 小时前
SpringBoot 配置文件完全指南-从入门到精通
java·spring boot·后端
烙印6019 小时前
Spring容器的心脏:深度解析refresh()方法(上)
java·后端·spring