Maven添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
<version>${spring-cloud-starter-stream-rocketmq.version}</version>
</dependency>
yml配置
消费者和生产者的使用方式为:名称-out-数字的方式用于生产者,名称-in-数字的方式用于消费者。
spring:
cloud:
function:
definition: receive1;receive2;receive3 #重点配置 与 binding 名与消费者对应,多个可以使用;
stream:
rocketmq:
binder:
name-server: 127.0.0.1:9876 #rocketmq 地址
vipChannelEnabled: false
bindings:
receive1-in-0:
consumer:
tags: s1||s2 #消费tag
broadcasting: false #是否使用广播消费,默认为 false 使用集群消费
receive1-out-0:
producer:
sync: true #开启同步发送
receive2-in-0:
consumer:
tags: #消费tag
broadcasting: true # 是否使用广播消费,默认为 false 使用集群消费
receive2-out-0:
producer:
sync: false #默认异步
receive3-in-0:
consumer:
tags: #消费tag
broadcasting: false # 是否使用广播消费,默认为 false 使用集群消费
receive3-out-0:
producer:
sync: true
bindings:
#消费者
receive1-in-0:
destination: receive1-topic #topic
content-type: application/json
group: receive1-in-group #group
#生产者
receive1-out-0:
destination: receive1-topic #topic
group: receive1-out-group
receive2-in-0:
destination: receive2-topic #topic
content-type: application/json
group: receive2-in-group #group
receive2-out-0:
destination: receive2-topic #topic
group: receive2-out-group
receive3-in-0:
destination: receive3-topic #topic
content-type: application/json
group: receive3-in-group #group
receive3-out-0:
destination: receive3-topic #topic
group: receive3-out-group #group
生产者发生消息
@RestController
@RequestMapping("/stream")
@RequiredArgsConstructor
@Slf4j
public class StreamController {
private final StreamBridge streamBridge;
@GetMapping("/test1")
public void test1() {
//消息广播消费
boolean result = streamBridge.send("receive2-out-0", "普通消息发送接收");
log.info("普通消息发送接收结果:{}", result);
}
@GetMapping("/test2")
public void test2() throws Exception {
//tag 发送
String payload = "消息发送到指定tag";
Map<String, Object> headers = new HashMap<>();
headers.put(MessageConst.PROPERTY_TAGS, "s1");
MessageHeaders messageHeaders = new MessageHeaders(headers);
Message<String> message = MessageBuilder.withPayload(payload).copyHeadersIfAbsent(messageHeaders).build();
boolean result = streamBridge.send("receive1-out-0", message);
log.info("消息发送到指定tag:{}", result);
}
@GetMapping("/test3")
public void test3() throws Exception {
String payload = "延迟消息 10s";
Map<String, Object> headers = new HashMap<>();
//延迟消费 延迟10秒
headers.put(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "3");
MessageHeaders messageHeaders = new MessageHeaders(headers);
Message<String> message = MessageBuilder.withPayload(payload).copyHeadersIfAbsent(messageHeaders).build();
boolean s3 = streamBridge.send("receive3-out-0", message);
log.info("延迟消息 10s:{}", s3);
}
}
延迟消息级别
String messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h"
消费者消费
@Slf4j
@Service
public class StreamConsumer {
@Bean
public Consumer<Message<String>> receive1() {
return message -> {
log.info("receive1获取消息tag:{}", message.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.TAGS));
log.info("receive1接收数据:{}", message.getPayload());
};
}
@Bean
public Function<Flux<Message<String>>, Mono<Void>> receive2() {
return flux -> flux.map(message -> {
log.info("receive2接收数据:{}", message.getPayload());
return message;
}).then();
}
@Bean
public Consumer<Flux<Message<String>>> receive3() {
return flux -> flux.map(message -> {
log.info("receive3接收数据:{}", message.getPayload());
return message;
}).subscribe();
}
}