RabbitMQ消费者分组与消息分区

rabbitmq多实例消费同⼀个queue数据

Rabbitmq多实例消费同⼀个queue数据

如何解决rabbitmq同⼀个topic的数据被多个实例消费, 多实例消费有2种情况

1.同⼀个topic的数据下发到不同的queue中,被不同的queue消费者消费

2.同⼀个queue中的数据多实例消费,这⾥要说的是这⼀种,先上⼀个配置代码

同一个交换机 exchange,同一group组的消息,消费者在同一个queue下,一个消息只会被一个消费者消费;
同一个交换机exchange,不同group下的消费,同一个消息会被不同group组下的消费这多次消费

spring:
  cloud:
    stream:
      bindings:
        product_init_input:
          destination: product.init.exchange
          content-type: application/json
          group: product.group
          consumer:
            concurrency: 50

如何保证多实例消费呢,这⾥必须要⽤到的代码是配置就是group, 这个group指定在消费端,指定某⼀个特定的group,⽐如代码中的product.group这样的话启动多实例的时候,实例中注册的queue都是product.init.exchange.product.group。

如果不指定group,那么每启动⼀个实例,就会⾃动创建⼀个product.init.exchange.wefdjsfisofewfidsfi 随机后缀的queue,这样相当于第⼀种情况,

⼀个topic数据被多个queue消费,那么每个queue都会收到完全相同的数据,相当于有⼏个实例每条数据都被重复消费了⼏次.所以要注意⾃⼰的需求,是要定义不同的queue来分别消费topic的数据还是要⽤多实例共同消费同⼀个queue的数据

spring:
  rabbitmq:
    host: 192.168.1.92
    port: 5672
    username: stone
    password: 123456
    virtual-host: /test
  cloud:
    stream:
      bindings:
        sms-out-0: # 自定义生产者通道名称
          destination: sms-exchange # 自定义Exchange交换机名称,生产者和消费者需要配置同一个Exchange
        sms-in-0: # 自定义消费者通道名称
          destination: sms-exchange # 自定义Exchange交换机名称,生产者和消费者需要配置同一个Exchange
          group: group01

当生产者向指定Exchange发送了一条消息时,处于同一个group的消费者只会消费一次消息,不会出现重复消费,当然如果消费者处于两个不同的group,消费消息是不受影响的。

启动应用我们会得到一个持久化的队列
在这里插入图片描述

消费者分组与消息分区

消息分区配置示例

RabbitMQ本身不支持消息分区,有时候,将数据发送到特定的分区是非常有必要的。我们可以将一些特定的消息发送到同一个分区。

spring:
  application:
    name: sms
  rabbitmq:
    host: 192.168.1.2
    port: 5672
    username: guest
    password: guest
    virtual-host: /test
  cloud:
    stream:
      bindings:
        sms-out-0: # 自定义生产者通道名称
          destination: sms-exchange # 自定义Exchange交换机名称,生产者和消费者需要配置同一个Exchange
          producer:
            partitionKeyExpression: payload #payload的含义是让算法根据我们消息的内容进行分区计算,我们可以自定义分区算法
            partition-count: 2
        sms-in-0: # 自定义消费者通道名称
          destination: sms-exchange # 自定义Exchange交换机名称,生产者和消费者需要配置同一个Exchange
          group: group01
          consumer:
            partitioned: true # 消费开启分区
      instance-count: 2 # 应用程序部署的实例数
      instance-index: 0 # 应用程序实例的索引数,如果instance-count=2的话,那instance-index分别就是0,1

这时候我们在启动两个消费者实例,就会发现接收到的消息是进过分区处理的

配置文件配置

消息分组

生产者消息配置

spring.cloud.stream.bindings.output.destination=greetings
server.port=8081
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

配置了spring.cloud.stream.bindings.output.destination=greetings 后会在RabbitMQ 中创建一个名为 greetings 交换器(exchange)。spring.cloud.stream.bindings.output.destination=greetings 的意思是把 spring cloud stream 的消息输出通道绑定到 RabbitMQ 的 greetings 交换器。

stream-hello 节点(消息消费端)消费者消息配置
spring.cloud.stream.bindings.input.group=Service-A
spring.cloud.stream.bindings.input.destination=greetings
server.port=8080
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

配置了 spring.cloud.stream.bindings.input.destination=greetings 后会在RabbitMQ 中创建一个名为 greetings 交换器(exchange)。spring.cloud.stream.bindings.input.destination=greetings 的意思是把 spring cloud stream 的输入通道绑定到 RabbitMQ 的 greetings 交换器。这样节点 stream-hello 的输入通道对应节点 stream-hello2 的输出通道,stream-hello 节点就配置成了 stream-hello2 节点的消费端。spring.cloud.stream.bindings.input.group=Service-A 配置 stream-hello 为消息组 Service-A 中的一个消费端。这两个配置项联合起来解释,就是把节点 stream-hello 的输入通道绑定到 RabbitMQ 的 greetings 交换器,并设置为greetings 交换器中 Service-A 消息消费组中的消费端节点。

stream-hello3 节点(消息消费端)
spring.cloud.stream.bindings.input.group=Service-A
spring.cloud.stream.bindings.input.destination=greetings
server.port=8082
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

配置了 spring.cloud.stream.bindings.input.destination=greetings 后会在RabbitMQ 中创建一个名为 greetings 交换器(exchange)。spring.cloud.stream.bindings.input.destination=greetings 的意思是把 spring cloud stream 的输入通道绑定到 RabbitMQ 的 greetings 交换器。这样节点 stream-hello 的输入通道对应节点 stream-hello2 的输出通道,stream-hello 节点就配置成了 stream-hello2 节点的消费端。spring.cloud.stream.bindings.input.group=Service-A 配置 stream-hello 为消息组 Service-A 中的一个消费端。这两个配置项联合起来解释,就是把节点 stream-hello 的输入通道绑定到 RabbitMQ 的 greetings 交换器,并设置为greetings 交换器中 Service-A 消息消费组中的消费端节点。

四、测试

  1. 启动RabbitMQ

  2. 启动节点 stream-hello 和 stream-hello3

  3. 启动节点 stream-hello2

  4. 我们看到 RabbitMQ 中已经创建了 greetings 交换器

在这里插入图片描述
5. 我们看到 RabbitMQ 中已经创建了greetings.Service-A 消息队列

在这里插入图片描述

  1. 消费节点接收到的消息

5.1 stream-hello 节点接收的消息
在这里插入图片描述
5.2 stream-hello3 接收到的消息

在这里插入图片描述
通过两个消费端输出的消息我们看到,每条消息只会被其中一个消费节点接收。

消息分区

五、配置消息

  1. 消息生产端 src/main/resources/application.properties
spring.cloud.stream.bindings.output.destination=greetings
spring.cloud.stream.bindings.output.producer.partition-count=2
spring.cloud.stream.bindings.output.producer.partition-key-expression=1
server.port=8081
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.stream.bindings.output.producer.partition-count=2 指定参与消息分区的消费端节点数量为2个。

spring.cloud.stream.bindings.output.producer.partition-key-expression=1 表示只有分区ID为1的消费端能接收到信息。

spring.cloud.stream.bindings.output.producer.partition-key-expression=0 表示只有分区ID为0的消费端能接收到信息。

2.消费端 stream-hello src/main/resources/application.properties

spring.cloud.stream.bindings.input.group=Service-A
spring.cloud.stream.bindings.input.destination=greetings
spring.cloud.stream.bindings.input.consumer.partitioned=true
spring.cloud.stream.instance-count=2
spring.cloud.stream.instance-index=0
server.port=8080
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.stream.bindings.input.consumer.partitioned=true 表示启用消息分区功能

spring.cloud.stream.instance-count=2 表示消息分区的消费端节点数量为2个

spring.cloud.stream.instance-index=0 该参数设置消费端实例的索引号,索引号从0开始。这里设置该节点的索引号为0

3.消费端 stream-hello3 src/main/resources/application.properties

spring.cloud.stream.bindings.input.consumer.partitioned=true
spring.cloud.stream.instance-count=2
spring.cloud.stream.instance-index=1
  1. 测试

4.1 重启 stream-hello 、sream-hello2、stream-hello3

4.2 当消息生产端 设置 spring.cloud.stream.bindings.output.producer.partition-key-expression=1 时,只有实例索引号为1的 stream-hello3 节点可以接收到信息。从截图我们看到 stream-hello3 接收到的消息的数字都是连续的。

在这里插入图片描述

4.3 当消息生产端 设置 spring.cloud.stream.bindings.output.producer.partition-key-expression=0 时,只有实例索引号为0的 stream-hello 节点可以接收到信息。

在这里插入图片描述

查看 RabbitMQ Management 可以看到,其实在 RabbitMQ 中,针对两个消费端分别创建了消息队列。

在这里插入图片描述

————————————————
版权声明:本文为CSDN博主「lixiaxin200319」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lixiaxin200319/article/details/82930026

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值