多个消费者组订阅了多个Topic,并且每个消费者组里的多个消费者实例的订阅关系应该保持一致。
例如下消费关系, 不同的消费组,消费不同的队列,相同的消费组订阅相同的队列,tag
错误示例:
一个消费者组订阅了多个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为所有的消息)
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,或者两个和上面不同的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;
}
});
注意,上面的几种错误示例,在运行时并不会报错,并且在少量的消息消费中,可能也看不出什么问题,但是当消费量很大,broker集群比较复杂时,将有可能出现不可预料的问题, 同时官网也不建议我们这样做.