因此,我正在探索 0MQ 作为我的集群应用程序的消息传递协议。它提供了我需要的那种异步通信。
到目前为止,我在制作原型方面取得了一些初步成功。我能够使用几种不同类型的模式发送和接收消息。
我目前正在尝试通过“握手”获得一种PUB/SUB
与初始同步一起工作的模式。REP/REQ
此方法在 ZeroMQ 指南中的“节点协调”下进行了描述。而且这个方法效果很好。唯一的问题是 0MQ 似乎正在吃掉我大约 50% 的消息,或者至少是其中的数据。
我正在使用 Qt,因此消息由QString
. 我正在QByteArray
使用 a 将字符串字节打包到 a 中QDataStream
。然后,我通过QByteArray
“电线”并在另一侧打开它。我经常使用这种方法通过其他协议(如 tcp 套接字)进行通信,并且效果很好。
我有 4 个“工人”连接到 viaPUB/SUB
和REQ/REP
一个“经理”。在大多数运行中,至少 1 个和最多 3 个“工人”同步正常。但是,当他们不这样做时,他们会收到一个空字符串。
这是 4 个客户端的日志:
alicia
:
[09:18:15.337] [Info] Logging initialized
[09:18:15.337] [Debug] Initializing client
alicia
[09:18:15.337] [Debug] Attempting to receive START signal from server
[09:18:15.340] [Info] Received message:
START
[ 09:18:15.341] [Debug] Recieved a SYNC message
SYNC
[09:18:15.341] [Info] Received START signal from manager
[09:18:15.341] [Info] Received message:
2
brenda
:
[09:18:15.337] [Info] Logging initialized
[09:18:15.337] [Debug] Initializing client
brenda
[09:18:15.337] [Debug] Attempting to receive START signal from server
[09:18:15.340] [Info] Received message:
START
[ 09:18:15.340] [Debug] Recieved a SYNC message
SYNC
[09:18:15.340] [Info] Received START signal from manager
[09:18:15.340] [Info] Received message:
START
[09:18:15.340] [Debug] Sending
0
th message
carlie
:
[09:18:15.336] [Info] Logging initialized
[09:18:15.337] [Debug] Initializing client
carlie
[09:18:15.337] [Debug] Attempting to receive START signal from server
[09:18:15.340] [Info] Received message:
START
[ 09:18:15.340] [Debug] Recieved a SYNC message
[09:18:15.340] [Fatal] carlie
Caught unhandled WASError:
Assertion "SOMEPATH/zmqworker.cpp" failed at SOMEPATH/zmqworker.cpp:52:virtual void was::ZMQWorker::run(): Invalid sync message
darcie
:
[09:18:15.336] [Info] Logging initialized
[09:18:15.337] [Debug] Initializing client
darcie
[09:18:15.337] [Debug] Attempting to receive START signal from server
[09:18:15.340] [Info] Received message:
START
[ 09:18:15.341] [Debug] Recieved a SYNC message
[09:18:15.341] [Fatal] darcie
Caught unhandled WASError:
Assertion "SOMEPATH/zmqworker.cpp" failed at SOMEPATH/zmqworker.cpp:52:virtual void was::ZMQWorker::run(): Invalid sync message
这是大部分代码:
从zmqtools.cpp
void sendQString( zmq::socket_t& socket, QString& str )
{
QByteArray package;
QDataStream packer( &package, QIODevice::WriteOnly );
packer << str;
zmq::message_t msg( package.data(), package.size(), NULL );
WAS_ASSERT_MSG( socket.send( msg ), "Failed to send QString" );
}
void recvQString( zmq::socket_t& socket, QString& str )
{
zmq::message_t msg;
WAS_ASSERT_MSG( socket.recv( &msg ), "Failed to receive QString" );
QByteArray package( ( char* )msg.data(), msg.size() );
QDataStream unpacker( &package, QIODevice::ReadOnly );
unpacker >> str;
}
从zmqworker.cpp
ZMQWorker::ZMQWorker( const QString& clientName, QObject *parent ) :
QThread( parent ),
clientName( clientName ),
context( 1 ),
inMessageSocket( context, ZMQ_PULL ),
outMessageSocket( context, ZMQ_PUSH ),
inControlSocket( context, ZMQ_SUB ),
synchronizeSocket( context, ZMQ_REQ )
{
qxtLog->debug() << "Initializing client " << clientName;
inMessageSocket.connect( "tcp://localhost:9900" );
outMessageSocket.connect( "tcp://localhost:9901" );
inControlSocket.connect( "tcp://localhost:9902" );
inControlSocket.setsockopt( ZMQ_SUBSCRIBE, "", 0 );
synchronizeSocket.connect( "tcp://localhost:9903" );
messagePoll.fd = 0;
messagePoll.events = ZMQ_POLLIN;
messagePoll.revents = 0;
messagePoll.socket = inMessageSocket;
controlPoll.fd = 0;
controlPoll.events = ZMQ_POLLIN;
controlPoll.revents = 0;
controlPoll.socket = inControlSocket;
}
void ZMQWorker::run()
{
QString message;
// Wait for start signal from server
bool started = true;
do
{
qxtLog->debug() << "Attempting to receive START signal from server";
recvQString( inControlSocket, message );
qxtLog->info() << "Received message: " << message;
started = message == "START";
} while( !started );
message = "SYNC";
sendQString( synchronizeSocket, message );
recvQString( synchronizeSocket, message );
qxtLog->debug() << "Recieved a SYNC message" << message;
WAS_ASSERT_MSG( message == "SYNC", "Invalid sync message" );
qxtLog->info() << "Received START signal from manager";
int messagesSent = 0;
forever
{
zmq::poll( &messagePoll, 1, 0 );
if( messagePoll.revents & ZMQ_POLLIN )
{
recvQString( inMessageSocket, message );
qxtLog->info() << "Received message: " << message;
}
zmq::poll( &controlPoll, 1, 0 );
if( controlPoll.revents & ZMQ_POLLIN )
{
recvQString( inControlSocket, message );
qxtLog->info() << "Received message: " << message;
if( message == "STOP" )
break;
}
if( messagesSent < 1000 )
{
qxtLog->debug() << "Sending " << messagesSent << "th message";
QString message = QString::number( messagesSent );
sendQString( outMessageSocket, message );
messagesSent++;
}
}
}
从zmqmanager.cpp
ZMQManager::ZMQManager( const QString& serverName, unsigned clientCount, QObject* parent ) :
QThread( parent ),
serverName( serverName ),
clientCount( clientCount ),
context( 1 ),
inMessageSocket( context, ZMQ_PULL ),
outMessageSocket( context, ZMQ_PUSH ),
outControlSocket( context, ZMQ_PUB ),
synchronizeSocket( context, ZMQ_REP )
{
qxtLog->debug() << "Initializing server " << serverName;
outMessageSocket.bind( "tcp://*:9900" );
inMessageSocket.bind( "tcp://*:9901" );
outControlSocket.bind( "tcp://*:9902" );
synchronizeSocket.bind( "tcp://*:9903" );
messagePoll.fd = 0;
messagePoll.events = ZMQ_POLLIN;
messagePoll.revents = 0;
messagePoll.socket = inMessageSocket;
synchronizePoll.fd = 0;
synchronizePoll.events = ZMQ_POLLIN;
synchronizePoll.revents = 0;
synchronizePoll.socket = synchronizeSocket;
}
void ZMQManager::run()
{
QString message;
unsigned clientsConnected = 0;
do
{
qxtLog->debug() << "Publishing START signal";
message = "START";
sendQString( outControlSocket, message );
zmq::poll( &synchronizePoll, 1, 5000 );
if( synchronizePoll.revents & ZMQ_POLLIN )
{
qxtLog->debug() << "Checking for response to START signal";
recvQString( synchronizeSocket, message );
qxtLog->debug() << "Recieved a SYNC message" << message;
WAS_ASSERT_MSG( message == "SYNC", "Invalid sync message" );
sendQString( synchronizeSocket, message );
clientsConnected++;
}
} while( clientsConnected < clientCount );
qxtLog->info() << "Started and syncrhonized with clients";
unsigned messagesSent = 0;
unsigned messagesReceived = 0;
do
{
zmq::poll( &messagePoll, 1, 0 );
if( messagePoll.revents & ZMQ_POLLIN )
{
recvQString( inMessageSocket, message );
qxtLog->info() << "Received message: " << message;
messagesReceived++;
}
if( messagesSent < clientCount * 1000 )
{
qxtLog->debug() << "Sending a message";
message = QString::number( messagesSent );
sendQString( outMessageSocket, message );
messagesSent++;
}
} while( messagesSent < clientCount * 1000 && messagesReceived < clientCount * 1000 );
message = "STOP";
sendQString( outControlSocket, message );
}
所以,最后我的问题是:
- 我是否忽略了这段代码中的某些内容?
- 我是否错误地打包/解包 ZeroMQ 消息?
- 我
REQ/REP
是否错误地管理同步?
我真的很感谢那些对 ZMQ 有一定经验的人的一些见解,特别是在混合和PUB/SUB
模式方面。PUSH/PULL
REQ/REP