The request-reply pattern is used for sending requests from a ZMQ_REQ client to one or more ZMQ_REP services, and receiving subsequent replies to each request sent.
请求应答(request-reply)模式用于从MQ_REQ 客户端发送向一个或者多个 ZMQ_REP服务器，并且接收每个发送请求的后续回复。
A socket of type ZMQ_REQ is used by a client to send requests to and receive replies from a service. This socket type allows only an alternating(交替、轮换) sequence of zmq_send(request) and subsequent zmq_recv(reply) calls. Each request sent is round-robined among all services, and each reply received is matched with the last issued request.[请求一次消息，接着必须收到应答]
When a ZMQ_REQ socket enters the mute state(非活跃状态) due to having reached the high water mark for all services, or if there are no services at all, then any zmq_send(3) operations on the socket shall block(阻塞) until the mute state ends or at least one service becomes available for sending; messages are not discarded.
A socket of type ZMQ_REP is used by a service to receive requests from and send replies to a client. This socket type allows only an alternating(交替、轮换) sequence ofzmq_recv(request) and subsequentzmq_send(reply) calls. Each request received is fair-queued(公平排队) from among all clients, and each reply sent is routed to the client that issued the last request. If the original requester doesn’t exist any more the reply is silently discarded(默默丢弃).
When a ZMQ_REP socket enters the mute state due to having reached the high water mark for a client, then any replies sent to the client in question shall be dropped until the mute state ends.
A socket of type ZMQ_DEALER is an advanced pattern(先进的模式) used for extending request/reply sockets. Each messagesentis round-robined among all connected peers(连接节点), and each messagereceivedis fair-queued from all connected peers.
When a ZMQ_DEALER socket enters the mute state due to having reached the high water mark for all peers, or if there are no peers at all, then any zmq_send(3) operations on the socket shall block until the mute state ends or at least one peer becomes available for sending; messages are not discarded.
When a ZMQ_DEALER socket is connected to a ZMQ_REP socket each message sent must consist of an empty message part, the delimiter(分隔符), followed by one or more body parts(主体部分).
A socket of type ZMQ_ROUTER is an advanced socket type used for extending request/reply sockets. When receiving messages aZMQ_ROUTERsocket shall prepend(追加在...前) a message part containing the identity of the originating peer(含源节点的身份) to the message before passing it to the application. Messages received are fair-queued from among all connected peers. When sending messages aZMQ_ROUTER socket shall remove the first part of the message and use it to determine the identity of the peer the message shall be routed to. If the peer does not exist anymore the message shall be silently discarded by default, unlessZMQ_ROUTER_BEHAVIOR socket option is set to 1.
When a ZMQ_ROUTER socket enters the mute state due to having reached the high water mark for all peers, then any messages sent to the socket shall be dropped until the mute state ends. Likewise, any messages routed to a peer for which the individual high water mark has been reached shall also be dropped.
When a ZMQ_REQ socket is connected to a ZMQ_ROUTER socket, in addition to the identity of the originating peer each message received shall contain an empty delimiter message part. Hence, the entire structure of each received message as seen by the application becomes: one or more identity parts, delimiter part, one or more body parts. When sending replies to a ZMQ_REQ socket the application must include the delimiter part.
The publish-subscribe pattern is used for one-to-many distribution of data(一对多的数据分发) from a single publisher(发布者) to multiple subscribers(多个用户) in afan out(扩展) fashion.
A socket of type ZMQ_PUBis used by a publisher to distribute data. Messages sent are distributed in a fan out fashion to all connected peers. Thezmq_recv(3) function is not implemented(不再生效) for this socket type.
When a ZMQ_PUB socket enters the mute state due to having reached the high water mark for a subscriber, then any messages that would be sent to the subscriber in question shall instead be dropped until the mute state ends. The zmq_send() function shall never block(从不阻塞) for this socket type.
A socket of type ZMQ_SUB is used by a subscriber to subscribe to data distributed by a publisher. Initially a ZMQ_SUB socket is not subscribed to any messages, use the ZMQ_SUBSCRIBE option of zmq_setsockopt(3) to specify which messages to subscribe to. The zmq_send() function is not implemented for this socket type.
1）必须使用 zmq_setsockopt [ZMQ_SUBSCRIBE] 进行订阅（重要）。
其一、模式一 —— 典型的请求-应答模式：套接字对REQ-REP 是步伐一致的。客户端调用ZMQ发送函数，然后调用函数zmq_send(3)，然后调用函数zmq_recv(3),这是一个循环（或者如果需要的话只调用一次）。使用别的任何序列（如在一行发送两个消息）都会出错。类似的，服务器按照先调用函数zmq_recv(3)，然后调用函数zmq_send(3)的顺序，调用次数根据需要而定。
其二、模式二 —— 数据分发模式，一台服务器把数据发送到一系列的客户端上。