Linux——ZMQ-zmq_socket

来源:互联网 发布:mes系统用java开发吗 编辑:程序博客网 时间:2024/05/18 22:09

请求模式(Request-reply pattern)

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服务器,并且接收每个发送请求的后续回复。

1、ZMQ_REQ(client)

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.

2、ZMQ_REP(server)
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.

3、ZMQ_DEALER

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(主体部分).

4、ZMQ_ROUTER
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.

====================================

发布-订阅模式(Publish-subscribe pattern)

The publish-subscribe pattern is used for one-to-many distribution of data(一对多的数据分发) from a single publisher(发布者) to multiple subscribers(多个用户) in afan out(扩展) fashion.

1、ZMQ_PUB(服务器:发布方)

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.

NOTE:1)服务器根据需要调用函数zmq_send(3),但是不能用PUB套接字调用函数zmq_recv(3)。

2、ZMQ_SUB(客服端:订阅方)
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.

NOTE:

1)必须使用 zmq_setsockopt [ZMQ_SUBSCRIBE] 进行订阅(重要)。 

2)PUB-SUB 套接字对是异步的。

3)一个订阅方实际上可以链接到不止一个发布方,每次使用一条连接命令。数据会交叉到达,因此没有哪个单一的发布方能够淹没其它发布方。

4)如果一个发布方没有与它连接的订阅方,它会简单的扔掉所有消息。

5)如果你使用TCP,并且订阅方很慢,消息将会在发布方排队。

====================================

管道模式(Pipeline pattern)

ZMQ_PUSH

ZMQ_PULL

=====================================

补充说明:

其一、模式一 —— 典型的请求-应答模式:套接字对REQ-REP 是步伐一致的。客户端调用ZMQ发送函数,然后调用函数zmq_send(3),然后调用函数zmq_recv(3),这是一个循环(或者如果需要的话只调用一次)。使用别的任何序列(如在一行发送两个消息)都会出错。类似的,服务器按照先调用函数zmq_recv(3),然后调用函数zmq_send(3)的顺序,调用次数根据需要而定。

其二、模式二 —— 数据分发模式,一台服务器把数据发送到一系列的客户端上。

其三、模式三—— 管道。它以扇入/扇出模型连接节点,会有很多步骤和循环。这是一个并行任务收集和分发方式。

其四、除了字节的大小外,0MQ 对你发送的数据一无所知。这就意味着你有责任对它进行安全的格式化,以便应用程序能够读回它。

其五、多线程服务器:请求-应答模型链式(REQ-XREP-queue-XREQ-REP )

其四、除了字节的大小外,0MQ 对你发送的数据一无所知。这就意味着你有责任对它进行安全的格式化,以便应用程序能够读回它。
其五、inproc:// 进程间通信方式,非组播,一对一。
其六、一定要注意默认消息体的大小(C 语言:32)。
0 0
原创粉丝点击