订阅关系一致

MQ 里的一个 Consumer ID 代表一个 Consumer 实例群组。对于大多数分布式应用来说,一个 Consumer ID 下通常会挂载多个 Consumer 实例。订阅关系一致指的是同一个 Consumer ID 下所有 Consumer 实例的处理逻辑必须完全一致。一旦订阅关系不一致,消息消费的逻辑就会混乱,甚至导致消息丢失。

由于 MQ 的订阅关系主要由 Topic+Tag 共同组成,因此,保持订阅关系一致意味着同一个 Consumer ID 下所有的实例需在以下两方面均保持一致:

  1. 订阅的 Topic 必须一致;

  2. 订阅的 Topic 中的 Tag 必须一致。

下面举几个反面例子:

【例一】以下例子中,同一个 Consumer ID 下的两个实例订阅的 Topic 不一致。

Consumer 实例 1-1:

 
 
  1. Properties properties = new Properties();
  2. properties.put(PropertyKeyConst.ConsumerId, "CID_jodie_test_1");
  3. Consumer consumer = ONSFactory.createConsumer(properties);
  4. consumer.subscribe("jodie_test_A", "*", new MessageListener() {
  5. public Action consume(Message message, ConsumeContext context) {
  6. System.out.println(message.getMsgID());
  7. return Action.CommitMessage;
  8. }
  9. });

Consumer 实例1-2:

 
 
  1. Properties properties = new Properties();
  2. properties.put(PropertyKeyConst.ConsumerId, " CID_jodie_test_1");
  3. Consumer consumer = ONSFactory.createConsumer(properties);
  4. consumer.subscribe("jodie_test_B ", "*", new MessageListener() {
  5. public Action consume(Message message, ConsumeContext context) {
  6. System.out.println(message.getMsgID());
  7. return Action.CommitMessage;
  8. }
  9. });

【例二】以下例子中,同一个 Consumer ID 下订阅 Topic 的 Tag 不一致。Consumer 实例2-1 订阅了 TagA,而 Consumer 实例2-2 未指定 Tag。

Consumer 实例2-1:

 
 
  1. Properties properties = new Properties();
  2. properties.put(PropertyKeyConst.ConsumerId, "CID_jodie_test_2");
  3. Consumer consumer = ONSFactory.createConsumer(properties);
  4. consumer.subscribe("jodie_test_A", "TagA", new MessageListener() {
  5. public Action consume(Message message, ConsumeContext context) {
  6. System.out.println(message.getMsgID());
  7. return Action.CommitMessage;
  8. }
  9. });

Consumer 实例2-2:

 
 
  1. Properties properties = new Properties();
  2. properties.put(PropertyKeyConst.ConsumerId, " CID_jodie_test_2");
  3. Consumer consumer = ONSFactory.createConsumer(properties);
  4. consumer.subscribe("jodie_test_A ", "*", new MessageListener() {
  5. public Action consume(Message message, ConsumeContext context) {
  6. System.out.println(message.getMsgID());
  7. return Action.CommitMessage;
  8. }
  9. });

【例三】此例中,错误的原因有俩个:

  1. 同一个 Consumer ID 下订阅 Topic 个数不一致。
  2. 同一个 Consumer ID 下订阅 Topic 的 Tag 不一致。

Consumer 实例3-1:

 
 
  1. Properties properties = new Properties();
  2. properties.put(PropertyKeyConst.ConsumerId, "CID_jodie_test_3");
  3. Consumer consumer = ONSFactory.createConsumer(properties);
  4. consumer.subscribe("jodie_test_A", "TagA", new MessageListener() {
  5. public Action consume(Message message, ConsumeContext context) {
  6. System.out.println(message.getMsgID());
  7. return Action.CommitMessage;
  8. }
  9. });
  10. consumer.subscribe("jodie_test_B", "TagB", new MessageListener() {
  11. public Action consume(Message message, ConsumeContext context) {
  12. System.out.println(message.getMsgID());
  13. return Action.CommitMessage;
  14. }
  15. });

Consumer 实例3-2:

 
 
  1. Properties properties = new Properties();
  2. properties.put(PropertyKeyConst.ConsumerId, " CID_jodie_test_3");
  3. Consumer consumer = ONSFactory.createConsumer(properties);
  4. consumer.subscribe("jodie_test_A ", "TagB", new MessageListener() {
  5. public Action consume(Message message, ConsumeContext context) {
  6. System.out.println(message.getMsgID());
  7. return Action.CommitMessage;
  8. }
  9. });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值