mqtt无法连接emqx

错误报告

环境

  • EMQX 版本:4.4.4
  • 操作系统版本:Ubuntu 18.04 64位

重现此问题的步骤

  1. 通过docker安装emqx
  2. 运行mqtt程序可以正常连接并订阅消息
  3. 运行一段时间后无法连接到emqx
  4. 重启emqx和mqtt程序后可以恢复正常

预期行为

应该能够持续连接并订阅到消息

实际行为

2022-11-03 20:58:54.240 [MQTT Ping: mqttReceiveId_Customer_172.20.0.2] ERROR o.e.p.c.m.i.ClientState - [logToJsr47,210] - mqttReceiveId_Customer_172.20.0.2: Timed out as no activity, keepAlive=60,000,000,000 lastOutboundActivity=13,585,515,514,421,284 lastInboundActivity=13,585,455,513,791,432 time=13,585,575,514,421,878 lastPing=13,585,515,514,425,230
2022-11-03 20:58:54.240 [scheduling-1] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - [error,261] - Error connecting or subscribing to [$share/group/$SYS/brokers/+/clients/+/connected, $share/group/$SYS/brokers/+/clients/+/disconnected, $share/group/config/products/+/devices/+/command, $share/group/event/products/+/devices/+/command, $share/group/reagent/products/+/devices/+/result, $share/group/sample/products/+/devices/+/command, $share/group/residue/products/+/devices/+/reagent, $share/group/residue/products/+/devices/+/common, $share/group/qc/products/+/devices/+/setting, $share/group/qc/products/+/devices/+/info, $share/group/qc/products/+/devices/+/test, $share/group/qc/products/+/devices/+/chart, $share/group/calibration/products/+/devices/+/setting, $share/group/calibration/products/+/devices/+/info, $share/group/calibration/products/+/devices/+/test, $share/group/calibration/products/+/devices/+/curve, $share/group/reagent/products/+/devices/+/register, $share/group/metric/products/+/devices/+/reboot]
org.eclipse.paho.client.mqttv3.MqttException: Timed out waiting for a response from the server
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
at org.eclipse.paho.client.mqttv3.internal.ClientState.checkForActivity(ClientState.java:747)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.checkForActivity(ClientComms.java:818)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.checkForActivity(ClientComms.java:804)
at org.eclipse.paho.client.mqttv3.TimerPingSender$PingTask.run(TimerPingSender.java:79)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
2022-11-03 20:59:24.241 [MQTT Ping: mqttReceiveId_Customer_172.20.0.2] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - [error,250] - Lost connection: Timed out waiting for a response from the server; retrying…
2022-11-03 20:59:24.241 [scheduling-1] ERROR o.s.i.m.i.MqttPahoMessageDrivenChannelAdapter - [error,149] - Exception while connecting and subscribing
org.eclipse.paho.client.mqttv3.MqttException: Timed out waiting for a response from the server
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
at org.eclipse.paho.client.mqttv3.internal.ClientState.checkForActivity(ClientState.java:747)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.checkForActivity(ClientComms.java:818)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.checkForActivity(ClientComms.java:804)
at org.eclipse.paho.client.mqttv3.TimerPingSender$PingTask.run(TimerPingSender.java:79)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)


你好,建议在出现这个问题的时候用 MQTT X 工具试一下,能不能连接到 EMQX 并正常进行订阅发布。

感觉比较大的概率是客户端实现的问题,不过看你提供的客户端日志也无法分辨是连接失败还是订阅失败,推荐你同时追踪一下这个客户端,以获取 EMQX 这边的详细日志。如何追踪客户端:日志与追踪 | EMQX 4.4 文档

谢谢回答,打开日志后发现客户端再频繁的连接和断开连接,麻烦帮忙看看什么原因
eqx_log.zip (3.6 KB)

看起来是客户端库的实现有点问题,我猜测是客户端发送了多余的连接报文导致自己反复踢自己下线。

建议追踪一下客户端 ID 而不是你订阅的主题,以获取更详细的日志。

使用的下面指令,应该是追踪的客户端的日志

emqx_ctl trace start client my_client log/my_client.l

我把完整的日志文件发给你,还有客户端的实现代码。
my_client.zip (2.5 MB)

应该就是我刚说的那个问题,你的客户端在自己互踢。

就是说,实际上并没有断开连接的时候,你的客户端又发了一个连接报文过来,EMQX 接受了这个新的连接然后关闭了旧连接,结果你的客户端处理时把这个关闭操作应用到了新的连接上,误以为连接断开,然后再次连接,最后就死循环了。

谢谢,平常的时候是好的,长时间连接才会出现这个问题。
麻烦看看我刚才提供的连接代码能看出来什么问题吗。

Java 代码我不是特别了解,可能很难帮到你。你可以看一下这个 SDK 会在什么时候发送 CONNECT 报文,然后 Debug 看看。

好的,非常感谢