今天开发了emqx集群,使用nginx 实现负载均衡。测试连接了一个客户端,但是出现每10分钟自动断线重连一下。重连机制是我自己写的。但是为什么会自动断线呢?
1、使用的用户和密码也是超级用户和密码。
2、keepAlive设置的0; reconnect为true
3、以下代码为nginx配置文件实现的负载均衡
stream {
upstream stream_backend {
server 192.168.2.116:1883 weight=1;
server 192.168.2.5:1883 weight=1;
}
server {
listen 1893;
proxy_pass stream_backend;
proxy_buffer_size 4k;
}
}
4、这是在emqx track的日志:
- 1 emqx@192.168.2.5节点的日志:
2024-01-09T15:32:07.992780+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: mqtt_packet_received, packet: CONNECT(Q0, R0, D0, ClientId=MQTT-TRANSFER-HUB, ProtoName=MQTT, ProtoVsn=4, CleanStart=true, KeepAlive=0, Username=admin, Password=******)
2024-01-09T15:32:07.992780+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: http_response, provider: emqx_authn_http, request: [base_url: http://192.168.2.5:8082/login, body: {"username":"admin","password":"[password]"}, headers: [{<<"accept">>,<<"application/json">>},{<<"cache-control">>,<<"no-cache">>},{<<"connection">>,<<"keep-alive">>},{<<"content-type">>,<<"application/json">>},{<<"keep-alive">>,<<"timeout=30, max=1000">>}], method: post, path_query: /login], resource: emqx_authn_http:2, response: [error: {resource_error,#{msg => "resource not connected",reason => not_connected}}]
2024-01-09T15:32:07.992780+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: authenticator_result, authenticator: password_based:http, result: ignore
2024-01-09T15:32:07.992780+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: authenticator_result, authenticator: password_based:built_in_database, result: {ok,#{is_superuser => true}}
2024-01-09T15:32:07.992780+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: authentication_result, reason: chain_result, result: {stop,{ok,#{is_superuser => true}}}
2024-01-09T15:32:07.992780+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_Data_Push_WH_D'}
2024-01-09T15:32:07.992780+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_Data_Push_WH_D'}
2024-01-09T15:32:07.992780+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: mqtt_packet_sent, packet: CONNACK(Q0, R0, D0, AckFlags=0, ReasonCode=0)
2024-01-09T15:42:07.992194+08:00 [SOCKET] MQTT-TRANSFER-HUB@192.168.2.5:49364 msg: emqx_connection_terminated, reason: {shutdown,tcp_closed}
2024-01-09T15:52:09.054607+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: mqtt_packet_received, packet: CONNECT(Q0, R0, D0, ClientId=MQTT-TRANSFER-HUB, ProtoName=MQTT, ProtoVsn=4, CleanStart=true, KeepAlive=0, Username=admin, Password=******)
2024-01-09T15:52:09.054607+08:00 [QUERY] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: http_connector_received, connector: emqx_authn_http:2, note: the request body is redacted due to security reasons, request: {"/login",[{<<"accept">>,<<"application/json">>},{<<"cache-control">>,<<"no-cache">>},{<<"connection">>,<<"keep-alive">>},{<<"content-type">>,<<"application/json">>},{<<"keep-alive">>,<<"timeout=30, max=1000">>}],<<"******">>}, state: [base_path: /, connect_timeout: 15000, host: {192,168,2,5}, pool_name: emqx_authn_http:2, pool_type: random, port: 8082, request: undefined]
2024-01-09T15:52:09.069607+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: http_response, provider: emqx_authn_http, request: [base_url: http://192.168.2.5:8082/login, body: {"username":"admin","password":"[password]"}, headers: [{<<"accept">>,<<"application/json">>},{<<"cache-control">>,<<"no-cache">>},{<<"connection">>,<<"keep-alive">>},{<<"content-type">>,<<"application/json">>},{<<"keep-alive">>,<<"timeout=30, max=1000">>}], method: post, path_query: /login], resource: emqx_authn_http:2, response: [body: {"msg":"用户不存在/密码错误","code":500}, headers: [{<<"vary">>,<<"Origin">>},{<<"vary">>,<<"Access-Control-Request-Method">>},{<<"vary">>,<<"Access-Control-Request-Headers">>},{<<"x-content-type-options">>,<<"nosniff">>},{<<"x-xss-protection">>,<<"1; mode=block">>},{<<"content-type">>,<<"application/json;charset=UTF-8">>},{<<"transfer-encoding">>,<<"chunked">>},{<<"date">>,<<"Tue, 09 Jan 2024 07:52:09 GMT">>},{<<"keep-alive">>,<<"timeout=60">>},{<<"connection">>,<<"keep-alive">>}], status: 200]
2024-01-09T15:52:09.069607+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: authenticator_result, authenticator: password_based:http, result: ignore
2024-01-09T15:52:09.069607+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: authenticator_result, authenticator: password_based:built_in_database, result: {ok,#{is_superuser => true}}
2024-01-09T15:52:09.069607+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: authentication_result, reason: chain_result, result: {stop,{ok,#{is_superuser => true}}}
2024-01-09T15:52:09.069607+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:50048 msg: mqtt_packet_sent, packet: CONNACK(Q0, R0, D0, AckFlags=0, ReasonCode=0)
- 2 emqx@192.168.2.116节点的日志:
2024-01-09T15:30:04.181642+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: mqtt_packet_received, packet: CONNECT(Q0, R0, D0, ClientId=MQTT-TRANSFER-HUB, ProtoName=MQTT, ProtoVsn=4, CleanStart=true, KeepAlive=0, Username=fant, Password=******)
2024-01-09T15:30:04.181642+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: http_response, provider: emqx_authn_http, request: [base_url: http://192.168.2.5:8082/login, body: {"username":"fant","password":"[password]"}, headers: [{<<"accept">>,<<"application/json">>},{<<"cache-control">>,<<"no-cache">>},{<<"connection">>,<<"keep-alive">>},{<<"content-type">>,<<"application/json">>},{<<"keep-alive">>,<<"timeout=30, max=1000">>}], method: post, path_query: /login], resource: emqx_authn_http:2, response: [error: {resource_error,#{msg => "resource not connected",reason => not_connected}}]
2024-01-09T15:30:04.181642+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: authenticator_result, authenticator: password_based:http, result: ignore
2024-01-09T15:30:04.181642+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: authenticator_result, authenticator: password_based:built_in_database, result: {ok,#{is_superuser => false}}
2024-01-09T15:30:04.181642+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: authentication_result, reason: chain_result, result: {stop,{ok,#{is_superuser => false}}}
2024-01-09T15:30:04.196642+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_Data_Push_WH_D'}
2024-01-09T15:30:04.196642+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_OnoffLine_Push_WH_D'}
2024-01-09T15:30:04.196642+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_Data_Push_WH_D'}
2024-01-09T15:30:04.196642+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: mqtt_packet_sent, packet: CONNACK(Q0, R0, D0, AckFlags=0, ReasonCode=0)
2024-01-09T15:31:38.103459+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_Data_Push_WH_D'}
2024-01-09T15:31:38.103459+08:00 [BRIDGE] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: bridge_action, bridge_id: {bridge_v2,http,'Device_OnoffLine_Push_WH_D'}
2024-01-09T15:31:38.118458+08:00 [SOCKET] MQTT-TRANSFER-HUB@192.168.2.5:49160 msg: emqx_connection_terminated, reason: {shutdown,tcp_closed}
2024-01-09T15:42:09.336226+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: mqtt_packet_received, packet: CONNECT(Q0, R0, D0, ClientId=MQTT-TRANSFER-HUB, ProtoName=MQTT, ProtoVsn=4, CleanStart=true, KeepAlive=0, Username=admin, Password=******)
2024-01-09T15:42:09.336226+08:00 [QUERY] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: http_connector_received, connector: emqx_authn_http:2, note: the request body is redacted due to security reasons, request: {"/login",[{<<"accept">>,<<"application/json">>},{<<"cache-control">>,<<"no-cache">>},{<<"connection">>,<<"keep-alive">>},{<<"content-type">>,<<"application/json">>},{<<"keep-alive">>,<<"timeout=30, max=1000">>}],<<"******">>}, state: [base_path: /, connect_timeout: 15000, host: {192,168,2,5}, pool_name: emqx_authn_http:2, pool_type: random, port: 8082, request: undefined]
2024-01-09T15:42:09.742225+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: http_response, provider: emqx_authn_http, request: [base_url: http://192.168.2.5:8082/login, body: {"username":"admin","password":"[password]"}, headers: [{<<"accept">>,<<"application/json">>},{<<"cache-control">>,<<"no-cache">>},{<<"connection">>,<<"keep-alive">>},{<<"content-type">>,<<"application/json">>},{<<"keep-alive">>,<<"timeout=30, max=1000">>}], method: post, path_query: /login], resource: emqx_authn_http:2, response: [body: {"msg":"用户不存在/密码错误","code":500}, headers: [{<<"vary">>,<<"Origin">>},{<<"vary">>,<<"Access-Control-Request-Method">>},{<<"vary">>,<<"Access-Control-Request-Headers">>},{<<"x-content-type-options">>,<<"nosniff">>},{<<"x-xss-protection">>,<<"1; mode=block">>},{<<"content-type">>,<<"application/json;charset=UTF-8">>},{<<"transfer-encoding">>,<<"chunked">>},{<<"date">>,<<"Tue, 09 Jan 2024 07:42:08 GMT">>},{<<"keep-alive">>,<<"timeout=60">>},{<<"connection">>,<<"keep-alive">>}], status: 200]
2024-01-09T15:42:09.742225+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: authenticator_result, authenticator: password_based:http, result: ignore
2024-01-09T15:42:09.758225+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: authenticator_result, authenticator: password_based:built_in_database, result: {ok,#{is_superuser => true}}
2024-01-09T15:42:09.758225+08:00 [AUTHN] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: authentication_result, reason: chain_result, result: {stop,{ok,#{is_superuser => true}}}
2024-01-09T15:42:09.773225+08:00 [MQTT] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: mqtt_packet_sent, packet: CONNACK(Q0, R0, D0, AckFlags=0, ReasonCode=0)
2024-01-09T15:52:09.771751+08:00 [SOCKET] MQTT-TRANSFER-HUB@192.168.2.5:49719 msg: emqx_connection_terminated, reason: {shutdown,tcp_closed}