提问:SpringBoot 整合 RocketMQ,怎么配置是否启用 RocketMQ

SpringBoot 整合 RocketMQ,有个类使用了 @RocketMQMessageListener 监听器注解,项目启动会自动开启监听,如果 rocketMQ 未部署/未启动时,项目连不上MQ,则会自动停掉。查阅了 (rocketmq-spring)[https://github.com/apache/rocketmq-spring] 官方文档,没有看到能配置开启关闭MQ的配置。

SpringBoot 版本号:2.2.4
RocketMQ gradle依赖:implementation “org.apache.rocketmq:rocketmq-spring-boot-starter:2.1.0”

异常如下:

2020-03-10 12:10:48.405 ERROR 1835 --- [  restartedMain] o.a.r.s.a.ListenerContainerConfiguration : Started container failed. DefaultRocketMQListenerContainer{consumerGroup='PongSky_consumer_one', nameServer='localhost:9876', topic='PongSky_topic', consumeMode=ORDERLY, selectorType=TAG, selectorExpression='🐭 || 🐯 || 🐲 || 🐎 || 🐒 || 🐶', messageModel=CLUSTERING}

java.lang.IllegalStateException: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to [localhost:9876] failed
	at org.apache.rocketmq.client.impl.factory.MQClientInstance.updateTopicRouteInfoFromNameServer(MQClientInstance.java:681) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.factory.MQClientInstance.updateTopicRouteInfoFromNameServer(MQClientInstance.java:511) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.updateTopicSubscribeInfoWhenSubscriptionChanged(DefaultMQPushConsumerImpl.java:867) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.start(DefaultMQPushConsumerImpl.java:648) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.consumer.DefaultMQPushConsumer.start(DefaultMQPushConsumer.java:693) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer.start(DefaultRocketMQListenerContainer.java:279) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.registerContainer(ListenerContainerConfiguration.java:120) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	at java.base/java.util.HashMap.forEach(HashMap.java:1336) ~[na:na]
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.afterSingletonsInstantiated(ListenerContainerConfiguration.java:79) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:896) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at com.dindonyun.zhibaoiot.HttpApiApplication.main(HttpApiApplication.java:22) ~[main/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.4.RELEASE.jar:2.2.4.RELEASE]
Caused by: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to [localhost:9876] failed
	at org.apache.rocketmq.remoting.netty.NettyRemotingClient.getAndCreateNameserverChannel(NettyRemotingClient.java:441) ~[rocketmq-remoting-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.remoting.netty.NettyRemotingClient.getAndCreateChannel(NettyRemotingClient.java:396) ~[rocketmq-remoting-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.remoting.netty.NettyRemotingClient.invokeSync(NettyRemotingClient.java:365) ~[rocketmq-remoting-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.MQClientAPIImpl.getTopicRouteInfoFromNameServer(MQClientAPIImpl.java:1369) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.MQClientAPIImpl.getTopicRouteInfoFromNameServer(MQClientAPIImpl.java:1359) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.factory.MQClientInstance.updateTopicRouteInfoFromNameServer(MQClientInstance.java:624) ~[rocketmq-client-4.6.0.jar:4.6.0]
	... 23 common frames omitted

2020-03-10 12:10:48.407  INFO 1835 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-03-10 12:10:48.418  INFO 1835 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-10 12:10:48.423 ERROR 1835 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

java.lang.RuntimeException: java.lang.IllegalStateException: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to [localhost:9876] failed
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.registerContainer(ListenerContainerConfiguration.java:123) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	at java.base/java.util.HashMap.forEach(HashMap.java:1336) ~[na:na]
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.afterSingletonsInstantiated(ListenerContainerConfiguration.java:79) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:896) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
	at com.dindonyun.zhibaoiot.HttpApiApplication.main(HttpApiApplication.java:22) ~[main/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.4.RELEASE.jar:2.2.4.RELEASE]
Caused by: java.lang.IllegalStateException: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to [localhost:9876] failed
	at org.apache.rocketmq.client.impl.factory.MQClientInstance.updateTopicRouteInfoFromNameServer(MQClientInstance.java:681) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.factory.MQClientInstance.updateTopicRouteInfoFromNameServer(MQClientInstance.java:511) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.updateTopicSubscribeInfoWhenSubscriptionChanged(DefaultMQPushConsumerImpl.java:867) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.consumer.DefaultMQPushConsumerImpl.start(DefaultMQPushConsumerImpl.java:648) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.consumer.DefaultMQPushConsumer.start(DefaultMQPushConsumer.java:693) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer.start(DefaultRocketMQListenerContainer.java:279) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.registerContainer(ListenerContainerConfiguration.java:120) ~[rocketmq-spring-boot-2.1.0.jar:2.1.0]
	... 17 common frames omitted
Caused by: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to [localhost:9876] failed
	at org.apache.rocketmq.remoting.netty.NettyRemotingClient.getAndCreateNameserverChannel(NettyRemotingClient.java:441) ~[rocketmq-remoting-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.remoting.netty.NettyRemotingClient.getAndCreateChannel(NettyRemotingClient.java:396) ~[rocketmq-remoting-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.remoting.netty.NettyRemotingClient.invokeSync(NettyRemotingClient.java:365) ~[rocketmq-remoting-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.MQClientAPIImpl.getTopicRouteInfoFromNameServer(MQClientAPIImpl.java:1369) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.MQClientAPIImpl.getTopicRouteInfoFromNameServer(MQClientAPIImpl.java:1359) ~[rocketmq-client-4.6.0.jar:4.6.0]
	at org.apache.rocketmq.client.impl.factory.MQClientInstance.updateTopicRouteInfoFromNameServer(MQClientInstance.java:624) ~[rocketmq-client-4.6.0.jar:4.6.0]
	... 23 common frames omitted

2020-03-10 12:10:48.428  INFO 1835 --- [  restartedMain] a.r.s.s.DefaultRocketMQListenerContainer : container destroyed, DefaultRocketMQListenerContainer{consumerGroup='PongSky_consumer_one', nameServer='localhost:9876', topic='PongSky_topic', consumeMode=ORDERLY, selectorType=TAG, selectorExpression='🐭 || 🐯 || 🐲 || 🐎 || 🐒 || 🐶', messageModel=CLUSTERING}
2020-03-10 12:10:48.440  INFO 1835 --- [  restartedMain] org.mongodb.driver.connection            : Closed connection [connectionId{localValue:2, serverValue:49}] to localhost:27017 because the pool has been closed.
Disconnected from the target VM, address: '127.0.0.1:53222', transport: 'socket'

Process finished with exit code 1

麻烦大家了,谢谢~

你的意思是,你整合了MQ。你现在是消费者,启用要要去连接MQ服务。但是因为目前还没MQ服务,导启动后致监听器异常了。你需要一个配置项,类似于这样。

rocketmq.enabled=false

对的,不然项目会自动停掉

据我所知,没有这种配置。

但是你可以通过在监听器上标识@Profile注解, 指定运行环境。来解决这个问题。
在测试,本地开发环境,就不要加载这个监听器Bean。即可。

我觉得这是更优雅的解决方式。

1赞

好的,我试试,谢谢大佬 :slight_smile:

你要是解决了,觉得没问题。就点一下我回复下面的那个【解决方案】。
下次有人遇到相同问题,可以第一时间找到解决方案 (满足一下我虚荣心:see_no_evil:

好的,第一次使用社区 [狗头]