开发者社区> 问答> 正文

怎么让订阅关系一致


MQ 里的一个 Consumer ID 代表一个 Consumer 实例群组。对于大多数分布式应用来说,一个 Consumer ID 下通常会挂载多个 Consumer 实例。订阅关系一致指的是同一个 Consumer ID 下所有 Consumer 实例的处理逻辑必须完全一致。一旦订阅关系不一致,消息消费的逻辑就会混乱,甚至导致消息丢失。
由于 MQ 的订阅关系主要由 Topic+Tag 共同组成,因此,保持订阅关系一致意味着同一个 Consumer ID 下所有的实例需在以下两方面均保持一致:


  1. 订阅的 Topic 必须一致;

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


如上图所示,一个 Consumer ID 也可以订阅多个 Topic,但是这个 Consumer ID 里的多个消费者实例的订阅关系一定要保持一致。
下文给出了订阅关系不一致的错误代码示例。
【例一】以下例子中,同一个 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. });

展开
收起
猫饭先生 2017-10-26 13:53:58 1773 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多