英文:
How to handle ZeroMQ C++ listener IP address change
问题
你可以尝试使用以下代码来检测和处理ZeroMQ C++连接的日志服务的IP地址变化:
while (true) {
try {
// 执行你的ZeroMQ发送操作
m_socket->send(msg, ZMQ_DONTWAIT);
// 如果成功,则跳出循环
break;
}
catch (const zmq::error_t& e) {
// 处理发送错误,例如记录或采取必要的行动
printf("zeromq发送错误,将尝试重新连接");
// 在尝试重新连接之前等待一段时间
Sleep(1000);
// 通过关闭套接字并建立新连接来尝试重新连接
m_socket->close();
m_socket->connect(GetLogListenerUri());
}
}
这段代码将在发生发送错误时捕获异常,然后尝试重新连接。然而,要注意在MQ中间件中,不是直接的TCP连接,所以一些特定的操作可能不会像你预期的那样起作用。如果这样的情况发生,你可能需要进一步调查或者尝试其他方法来处理IP地址的变化。
另外,你提供了一些关于套接字选项的定义,但它们并没有直接与IP地址变化相关。如果你需要特定的套接字选项来处理这个问题,你可能需要查阅ZeroMQ的文档或者社区来获取更多信息。
英文:
How can I detect/handle IP address change of a ZeroMQ C++ connected logger service from the logger client?
Like when the Docker container restarts in the Swarm mode?
I use this string on the server tcp://*:20000
while using tcp://logger_svc:20000
on the client(s).
It seems it doesn't resolve the logger_svc
hostname on each call/caches the IP, so I guess my only way is to detect the change on the client.
I tried wrapping the socket.send call in a try catch block, but it never catches a thing, not event when I bring down the logger service:
while (true) {
try {
// Perform your ZeroMQ send operation here
m_socket->send(msg, ZMQ_DONTWAIT);
// If successful, break out of the loop
break;
}
catch (const zmq::error_t& e) {
// Handle the send error, e.g., log or take necessary actions
printf("zeromq send error, will try reconnecting");
// Sleep for a delay before attempting reconnection
Sleep(1000);
// Attempt to reconnect by closing the socket and establishing a new connection
m_socket->close();
m_socket->connect(GetLogListenerUri());
}
}
I guess that's normal, since a MQ is in between, not a direct TCP connection, but I'm lost at what to try next.
I only have these socket options available:
#define ZMQ_AFFINITY 4
#define ZMQ_IDENTITY 5
#define ZMQ_SUBSCRIBE 6
#define ZMQ_UNSUBSCRIBE 7
#define ZMQ_RATE 8
#define ZMQ_RECOVERY_IVL 9
#define ZMQ_SNDBUF 11
#define ZMQ_RCVBUF 12
#define ZMQ_RCVMORE 13
#define ZMQ_FD 14
#define ZMQ_EVENTS 15
#define ZMQ_TYPE 16
#define ZMQ_LINGER 17
#define ZMQ_RECONNECT_IVL 18
#define ZMQ_BACKLOG 19
#define ZMQ_RECONNECT_IVL_MAX 21
#define ZMQ_MAXMSGSIZE 22
#define ZMQ_SNDHWM 23
#define ZMQ_RCVHWM 24
#define ZMQ_MULTICAST_HOPS 25
#define ZMQ_RCVTIMEO 27
#define ZMQ_SNDTIMEO 28
#define ZMQ_LAST_ENDPOINT 32
#define ZMQ_ROUTER_MANDATORY 33
#define ZMQ_TCP_KEEPALIVE 34
#define ZMQ_TCP_KEEPALIVE_CNT 35
#define ZMQ_TCP_KEEPALIVE_IDLE 36
#define ZMQ_TCP_KEEPALIVE_INTVL 37
#define ZMQ_TCP_ACCEPT_FILTER 38
#define ZMQ_IMMEDIATE 39
#define ZMQ_XPUB_VERBOSE 40
#define ZMQ_ROUTER_RAW 41
#define ZMQ_IPV6 42
#define ZMQ_MECHANISM 43
#define ZMQ_PLAIN_SERVER 44
#define ZMQ_PLAIN_USERNAME 45
#define ZMQ_PLAIN_PASSWORD 46
#define ZMQ_CURVE_SERVER 47
#define ZMQ_CURVE_PUBLICKEY 48
#define ZMQ_CURVE_SECRETKEY 49
#define ZMQ_CURVE_SERVERKEY 50
#define ZMQ_PROBE_ROUTER 51
#define ZMQ_REQ_CORRELATE 52
#define ZMQ_REQ_RELAXED 53
#define ZMQ_CONFLATE 54
#define ZMQ_ZAP_DOMAIN 55
答案1
得分: 1
由于 @bazza 的评论,对于我的情况,解决方法是跳过DNS缓存,使用以下两个命令(更新ZeroMQ cpp库也可能有帮助):
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name MaxCacheTtl -Value 0 -Type DWord
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name MaxNegativeCacheTtl -Value 0 -Type DWord
基于来自Github的讨论:
https://github.com/moby/moby/issues/27499
英文:
Thanks to @bazza's comments, the solution in my case was to skip DNS caching using these two commands (updating the ZeroMQ cpp lib could have also helped):
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name MaxCacheTtl -Value 0 -Type DWord
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name MaxNegativeCacheTtl -Value 0 -Type DWord
Based on a discussion from Github:
https://github.com/moby/moby/issues/27499
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论