单生产者多消费者
工程POM
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
yaml配置
server:
port: 9520
spring:
application:
name: rocket-consumer
cloud:
stream:
bindings:
input:
content-type: application/json
destination: test-topic
group: test-group
rocketmq:
binder:
name-server: ip:9876
group: rocket-demo
启动类,多个消费者的搭建方式可以通过-Dserver.port来启动多个应用
@SpringBootApplication
// 但消费者那么绑定Sink就可以了
@EnableBinding({Sink.class})
public class RocketConsumerApplication {
@Value("${server.port}")
private int port;
public static void main(String[] args) {
SpringApplication.run(RocketConsumerApplication.class, args);
}
@StreamListener("input")
public void receive(String msg) {
System.out.println("receive msg " + port + " " + msg);
}
}
我们可以看到RocketMq默认是一个主题绑定了四个队列,
如果我们启动三个消费者,那么肯定是有一个消费者是要多消费数据的。当然实际生产我们可以和这个队列保持一致4个消费者组成一个消费者组进行消费。