前言

上一篇文章:基于SpringBoot整合RocketMQ异步发送短信功能在项目启动的过程中报了 RocketMQ 连接错误。针对这个问题,本文给予记录。

一、RocketMQ 连接报错处理

1.1 报错信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-12-24 21:12:22.374 ERROR 26188 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

java.lang.RuntimeException: java.lang.IllegalStateException: org.apache.rocketmq.remoting.exception.RemotingConnectException: connect to [192.168.57.129:9876] failed
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.registerContainer(ListenerContainerConfiguration.java:123) ~[rocketmq-spring-boot-2.2.2.jar:2.2.2]
	at java.util.HashMap.forEach(HashMap.java:1289) ~[na:1.8.0_181]
	at org.apache.rocketmq.spring.autoconfigure.ListenerContainerConfiguration.afterSingletonsInstantiated(ListenerContainerConfiguration.java:79) ~[rocketmq-spring-boot-2.2.2.jar:2.2.2]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:963) ~[spring-beans-5.3.6.jar:5.3.6]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.6.jar:5.3.6]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.6.jar:5.3.6]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.5.jar:2.4.5]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) [spring-boot-2.4.5.jar:2.4.5]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) [spring-boot-2.4.5.jar:2.4.5]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) [spring-boot-2.4.5.jar:2.4.5]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) [spring-boot-2.4.5.jar:2.4.5]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) [spring-boot-2.4.5.jar:2.4.5]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) [spring-boot-2.4.5.jar:2.4.5]
	at com.huang.quickstart.QuickStartApplication.main(QuickStartApplication.java:12) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.4.5.jar:2.4.5]

简单的理解就是 RocketMQ 连接不上虚拟机地址 192.168.57.129:9876 信息。

1.2 修改 broker.conf 文件

进入 broker.conf 目录下

cd /mydata/rocketmq/rocketmq-4.9.1/conf
vim broker.conf

加入两行(建议直接复制,然后再修改为自己的IP),一定是大写IP。

namesrvAddr=192.168.57.129:9876
brokerIP1=192.168.57.129

然后重启 nameserver 和 broker (一定要先启动 namesrv,因为消息服务器是注册到命名服务器上的)。

nohup sh mqnamesrv

再启动消息服务器,同时指定刚刚修改过的 conf 文件,不然还是会读取原本默认的阿里外网IP,还是会报错。

nohup sh mqbroker -n 192.168.57.129:9876 -c ../conf/broker.conf

备注:
-n 指定的是注册到哪个消息服务器上
-c 指定的是刚刚修改的conf配置文件

查看日志信息 namesrv 和broker 启动成功

[root@localhost bin]# cat nohup.out 
Java HotSpot(TM) 64-Bit Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) 64-Bit Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
The Name Server boot success. serializeType=JSON
The broker[broker-a, 192.168.57.129:10911] boot success. serializeType=JSON and name server is 192.168.57.129:9876

重新启动 SpringBoot 项目,发现还是报错 !!!

1.3 Linux 开放端口

排查了一下原因:发现可能是 Linux 中 RocketMQ 的 9876 端口没有开放的问题!!!

1、开启防火墙

systemctl start firewalld

2、开放指定端口

firewall-cmd --zone=public --add-port=9876/tcp --permanent

备注:
–zone 作用域
–add-port=9876/tcp 添加端口,格式为:端口/通讯协议
–permanent 永久生效,没有此参数重启后失效

3、重启防火墙

firewall-cmd --reload

4、查看端口号
netstat -ntlp #查看当前所有tcp端口

netstat -ntulp |grep 9876 #查看所有9876端口使用情况

1.4 项目启动成功

1、开放端口以及重启防火墙

[root@localhost bin]# firewall-cmd --zone=public --add-port=9876/tcp --permanent
success
[root@localhost bin]# firewall-cmd --reload
success

2、重启启动 SpringBoot 项目

2023-12-24 21:38:39.978  INFO 24612 --- [  restartedMain] a.r.s.s.DefaultRocketMQListenerContainer : running container: DefaultRocketMQListenerContainer{consumerGroup='screening-record-consumer_status-change', namespace='', nameServer='192.168.57.129:9876', topic='screening-record-topic', consumeMode=CONCURRENTLY, selectorType=TAG, selectorExpression='status-change', messageModel=CLUSTERING', tlsEnable=false}
2023-12-24 21:38:39.978  INFO 24612 --- [  restartedMain] o.a.r.s.a.ListenerContainerConfiguration : Register the listener to container, listenerBeanName:screeningRecordStatusChangeConsumer, containerBeanName:org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer_2
2023-12-24 21:38:40.039  INFO 24612 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2023-12-24 21:38:40.053  INFO 24612 --- [  restartedMain] c.h.quickstart.QuickStartApplication     : Started QuickStartApplication in 8.829 seconds (JVM running for 10.857)

3、项目启动成功,RocketMQ连接报错问题解决!!!

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐