参考:
RocketMQ 与 Spring Cloud Stream整合(一、快速入门) - 简书 (jianshu.com)
引用
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
<version>2021.1</version>
</dependency>
配置:
spring:
cloud:
# Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
stream:
## 消息多个需要在此定义,以分号(;)分隔
function:
definition: transformer;sms
# Binding 配置项,对应 BindingProperties Map
bindings:
# in为消费者 out为生产者
sms-out-0:
destination: sms-topic # 目的地。这里使用 RocketMQ Topic
content-type: application/json # 内容格式。这里使用 JSON
sms-in-0:
destination: sms-topic # 目的地。这里使用 RocketMQ Topic
content-type: text/plain # 内容格式。这里使用 JSON
group: sms-group
rocketmq:
binder:
name-server: 127.0.0.1:9876 # RocketMQ Namesrv 地址
# 以下配置可以忽略
bindings:
# RocketMQ Producer 配置项,对应 RocketMQProducerProperties 类
sms-out-0:
producer:
group: sms-binder-group # 生产者分组
sync: true # 是否同步发送消息,默认为 false 异步。
sms-in-0:
consumer:
orderly: true
生产者:
@Service
@AllArgsConstructor
public class SmsServiceImpl implements ISmsService {
private final StreamBridge streamBridge;
/**
* 采用StreamBridge的发送方式
*
* @param message 短消息
*/
@Override
public void sendSms(String message) {
streamBridge.send("sms-out-0", message);
}
}
消费者:
@Slf4j
@Service
public class SmsConsumerService {
/**
* 函数式编辑接收消息 sms名称与 spring.cloud.stream.founction.definition 中的一致。
*
* @return
*/
@Bean
public Consumer<String> sms() {
return message -> {
log.info("接收消息为:{}", message);
};
}
}
原 @Input @OutPut @EnableBinding 已过时,使用函数式编程即可。