消息驱动 —— 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);
    }
}
相关推荐
Csvn12 分钟前
Python 性能优化与 Profiling 工具
后端·python
不减20斤不改头像41 分钟前
手机一句话开发贪吃蛇!TRAE SOLO 移动端 AI 编程实测
前端·后端
明月_清风1 小时前
K8s 从入门到上手:核心概念+常用工具全解析
后端·kubernetes
随风,奔跑1 小时前
Nginx
服务器·后端·nginx·web
小村儿3 小时前
给 AI Agent 装上"长期记忆":Karpathy 的 LLM Wiki 思想,我做成了工具
前端·后端·ai编程
何陋轩4 小时前
Spring AI实战指南:在Java项目中集成大语言模型
人工智能·后端·机器学习
用户8356290780514 小时前
Python 操作 PowerPoint 表格的创建与格式化
后端·python
forestqq4 小时前
基于openeuler2403sp3的容器,打包django运行环境镜像
后端·python·django
站着5 小时前
TRAE SOLO 移动端正式上线:手机也是随身工位,随时随地进入「Vibe Working」
后端
盖世英雄酱581365 小时前
6000条数据执行时间9s??
数据库·后端