11第三章:05_订阅关系的一致性

一、订阅关系的一致性

订阅关系的一致性指的是,同一个消费者组(Group ID 相同)下所有 Consumer 实例所订阅的 Topic 与 Tag 及对消息的处理逻辑必须完全一致。否则,消息消费的逻辑就会混乱,甚至导致消息丢失。

二、正确订阅关系

多个消费者组订阅了多个 Topic,并且每个消费者组里的多个消费者实例的订阅关系保持了一致。

三、错误订阅关系

一个消费者组订阅了多个 Topic,但是该消费者组里的多个 Consumer 实例的订阅关系并没有保持一致。

1、订阅了不同 Topic

该例中的错误在于,同一个消费者组中的两个 Consumer 实例订阅了不同的 Topic。

Consumer 实例 1-1:(订阅了 topic 为 jodie_test_A,tag 为所有的消息)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_1");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","*",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

Consumer 实例 1-2:(订阅了 topic 为 jodie_test_B,tag 为所有的消息)

    Integer pageSize = 10;
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_1");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_B","*",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

2、订阅了不同 Tag

该例中的错误在于,同一个消费者组中的两个 Consumer 订阅了相同 Topic 的不同 Tag。

Consumer 实例 2-1:(订阅了 topic 为 jodie_test_A,tag 为 TagA 的消息)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_2");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","TagA",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

Consumer 实例 2-2:(订阅了 topic 为 jodie_test_A,tag 为所有的消息)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_2");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","*",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

3、订阅了不同数量的 Topic

该例中的错误在于,同一个消费者组中的两个 Consumer 订阅了不同数量的 Topic。

Consumer 实例 3-1:(该 Consumer 订阅了两个 Topic)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_3");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","TagA",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    }); consumer.subscribe("jodie_test_B","TagB",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });

Consumer 实例 3-2:(该 Consumer 订阅了一个 Topic)

    Properties properties = new Properties(); properties.put(PropertyKeyConst.GROUP_ID,"GID_jodie_test_3");
    Consumer consumer = ONSFactory.createConsumer(properties); consumer.subscribe("jodie_test_A","TagB",new

    MessageListener() {
        public Action consume (Message message, ConsumeContext context){
            System.out.println(message.getMsgID());
            return Action.CommitMessage;
        }
    });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值