说明
本示例只是简单仅是入门级的示例,详细使用后续会持续更新
测试版本:
- springcloud :Hoxton.SR8
- springcloud alibaba: 2.2.3.RELEASE
- springboot : 2.3.2.RELEASE
- rockectMQ: V4.4.0
Springcloud Stream交互示意图
搭建 Demo示例
生产者
maven依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-stream-rocketmq</artifactId> </dependency>
配置文件
spring: cloud: # Spring Cloud Stream 配置项,对应 BindingServiceProperties 类 stream: # Binding 配置项,对应 BindingProperties Map bindings: demo01-output: destination: DEMO-TOPIC-01 # 目的地。这里使用 RocketMQ Topic content-type: application/json # 内容格式。这里使用 JSON # Spring Cloud Stream RocketMQ 配置项 rocketmq: # RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类 binder: name-server: localhost:9876 # RocketMQ Namesrv 地址 # RocketMQ 自定义 Binding 配置项,对应 RocketMQBindingProperties Map bindings: demo01-output: # RocketMQ Producer 配置项,对应 RocketMQProducerProperties 类 producer: group: icspgroup # 生产者分组
代码
创建source
import org.springframework.cloud.stream.annotation.Output; import org.springframework.messaging.MessageChannel; /** * @author Rain * ceate time 2022/5/18 16:31 **/ public interface MySource { @Output("demo01-output") MessageChannel demo01Output(); }
说明:
- @Output内的名字要和我们配置文件中的 spring.cloud.stream.bindings 配置项对应上
- 返回参数为MessageChannel(