消息队列-RockMQ-单生产者多消费者

单生产者多消费者

在这里插入图片描述

工程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个消费者组成一个消费者组进行消费。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值