Zeromq的资源:
Zeromq模式:
http://blog.codingnow.com/2011/02/zeromq_message_patterns.html
zeromq主页:
Zeromq Guild:
http://zguide.zeromq.org/page:all#Fixing-the-World
Zeromq 中文简介:
http://blog.csdn.net/program_think/article/details/6687076
Zero wiki:
http://en.wikipedia.org/wiki/%C3%98MQ
zeromq系列:
http://iyuan.iteye.com/blog/972949
Zeromq资源阅读:
ØMQ(Zeromq) 是一个更为高效的传输层
优势是:
1 程序接口库是一个并发框架
2 在集群和超级计算机上表现得比TCP更快
3 通过inproc, IPC, TCP, 和 multicast进行传播消息
4 通过发散,订阅,流水线,请求的方式连接
5 对于不定规模的多核消息传输应用使用异步IO
6 有非常大并且活跃的开源社区
7 支持30+的语言
8 支持多种系统
Zeromq定义为“史上最快的消息队列”
从网络通信的角度看,它处于会话层之上,应用层之下。
ØMQ (ZeroMQ, 0MQ, zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry whole messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N
with patterns like fanout, pub-sub, task distribution, and request-reply. It's fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has
a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPL open source.
Zeromq中传递的数据格式是由用户自己负责,就是说如果server发送的string是有带"\0"的,那么client就必须要知道有这个
Pub_Sub模式。
the subscriber will always miss the first messages that the publisher sends. This is because as the subscriber connects to the publisher (something that takes a small but non-zero time), the publisher may already be sending messages out.
在这种模式下很可能发布者刚启动时发布的数据出现丢失,原因是用zmq发送速度太快,在订阅者尚未与发布者建立联系时,已经开始了数据发布(内部局域网没这么夸张的)。官网给了两个解决方案;1,发布者sleep一会再发送数据(这个被标注成愚蠢的);2,使用proxy。
Zeromq示例:
1 获取例子
git clone --depth=1 git://github.com/imatix/zguide.git
2 服务器端:
(当服务器收到消息的时候,服务器回复“World”)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php /* * * * * */ $context = new ZMQContext(1); // $responder = new ZMQSocket( $context , while (true) // $request = $responder ->recv(); printf "Received , $request ); // sleep // $responder ->send( "World" ); } |
3 客户端:
(客户端发送消息)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php /* * * * * */ $context = new ZMQContext(); // echo "Connecting ; $requester = new ZMQSocket( $context , for ( $request_nbr = $request_nbr != $request_nbr ++) printf "Sending , $request_nbr ); $requester ->send( "Hello" ); $reply = $requester ->recv(); printf "Received , $request_nbr , $reply ); } |
1
|
|
天气气候订阅系统:(pub-sub)
1 server端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php /* * * * * */ // $context = new ZMQContext(); $publisher = $context ->getSocket(ZMQ::SOCKET_PUB); while (true) // $zipcode = $temperature = $relhumidity = // $update = "%05d , $zipcode , $temperature , $relhumidity ); $publisher ->send( $update ); } |
2 client端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php /* * * * * */ $context = new ZMQContext(); // echo "Collecting , $subscriber = new ZMQSocket( $context , // $filter = $_SERVER [ 'argc' ] $_SERVER [ 'argv' ][1] "10001" ; $subscriber ->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, $filter ); // $total_temp = for ( $update_nbr = $update_nbr < $update_nbr ++) $string = $subscriber ->recv(); sscanf $string , "%d , $zipcode , $temperature , $relhumidity ); $total_temp += $temperature ; } printf "Average , $filter , $total_temp / $update_nbr )); |
1
|
------------------------ |
1
|
pub-sub的proxy模式: |
1
|
图示是: |
Proxy节点的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php /* * * */ $context = new ZMQContext(); // $frontend = new ZMQSocket( $context , // $backend = new ZMQSocket( $context , // $frontend ->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "" ); // while (true) while (true) // $message = $frontend ->recv(); $more = $frontend ->getSockOpt(ZMQ::SOCKOPT_RCVMORE); $backend ->send( $message , $more ? if (! $more ) break ; // } } } |
其实就是proxy同时是作为pub又作为sub的
----------------------
作者:yjf512(轩脉刃)
出处:http://www.cnblogs.com/yjf512/
本文版权归yjf512和cnBlog共有,欢迎转载,但未经作者同意必须保留此段声明
http://www.cnblogs.com/yjf512/archive/2012/03/03/2378024.html