spring boot整合rocketmq报错:The consumer group[] has been created before,

完整报错信息:

Caused by: org.apache.rocketmq.client.exception.MQClientException: The consumer group[zhaf] has been created before, specify another name please.
See http://rocketmq.apache.org/docs/faq/ for further details.
	at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.start(DefaultMQPushConsumerImpl.java:628)
	at org.apache.rocketmq.client.consumer.DefaultMQPushConsumer.start(DefaultMQPushConsumer.java:693)
	at org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer.start(DefaultRocketMQListenerContainer.java:275)
	... 11 common frames omitted

原因:

多个RocketMQMessageListener使用了相同的consumerGroup

解决方案:

1. 每个RocketMQMessageListener用不同的consumerGroup(有点欠揍)

2. 在打了@RocketMQMessageListener注解的类上实现RocketMQPushConsumerLifecycleListener接口,给每个Listener指定不同的listenerName

Listener1

@Service
@RocketMQMessageListener(consumerGroup = "a",
       topic = "topic1",
)
public class Listener1 implements RocketMQListener, RocketMQPushConsumerLifecycleListener {

    @Override
    public void onMessage(String message) {

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        // 注意看这个地方!! 给当前的listener指定一下InstanceName,不同的listner要使用不同的InstanceName
        consumer.setInstanceName("instance1");
    }
}

 Listener2

@Service
@RocketMQMessageListener(consumerGroup = "a",
       topic = "topic2",
)
public class Listener1 implements RocketMQListener, RocketMQPushConsumerLifecycleListener {

    @Override
    public void onMessage(String message) {

    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        // 注意看这个地方!! 给当前的listener指定一下InstanceName,不同的listner要使用不同的InstanceName
        consumer.setInstanceName("instance2");
    }
}

参考:two @RocketMQMessageListener can't use the same consumerGroup · Issue #237 · apache/rocketmq-spring · GitHub

你可能感兴趣的:(spring,boot,java-rocketmq,rocketmq)