php使用zeromq

Zeromq是個啥玩意?

Connect your code in any language, on any platform.
Carries messages across inproc, IPC, TCP, TPIC, multicast.
Smart patterns like pub-sub, push-pull, and router-dealer.
High-speed asynchronous I/O engines, in a tiny library.
Backed by a large and active open source community.
Supports every modern language and platform.
Build any architecture: centralized, distributed, small, or large.
Free software with full commercial support


以上是官方頁面的介紹,大意是說一種跨平臺,可以使用任何語言使用的消息中間件,可以通過inproc,IPC,TCP,TPIC,多播的消息傳遞消息,包括多種模式,pub-sub(分發-訂閱),push-pull(推送模式),router-dealer(路由模式)等等等等。極高的處理速度是其重要的特性之一。

官方詳細介紹請訪問:http://zeromq.org/


安裝:

#wget http://download.zeromq.org/zeromq-4.0.4.tar.gz

#tar zxvf zeromq-4.0.4.tar.gz

#cd zeromq-4.0.4

#./configure=/usr/local/zeromq404

#make

#make install


安裝PHP擴展:

#git clone git://github.com/mkoppanen/php-zmq.git

#cd php-zmq

#/usr/local/php/bin/phpize         //自己PHP的安裝目錄,根據需要更改

#./configure --with-php-config=/usr/local/php/bin/php-config --with-zmq=/usr/local/zeromq404    //php-config需要根據自己的情況進行更改

#make

#make install


安裝好之後會在/usr/local/php/lib/php/extensions/no-debug-zts-20090626/目錄下面生成zmq.so


修改配置文件:

#vim /etc/php.ini

修改extension_dir="/usr/local/php/lib/php/extensions/no-debug-zts-20090626/"

增加extension=zmq.so


重啓apache

訪問phpinfo,如果看到zmq的相關信息表明已經OK了


測試:

系統分兩部分:client和server端

server端一般由phpcli來執行,常駐後臺,監聽一個端口,此例中使用5555,代碼如下:


zmqserver.php

<?php

/*

 * * Hello World server

 * * Binds REP socket to tcp://*:5555

 * * Expects "Hello" from client, replies with "World"

 * * @author Ian Barber <ian(dot)barber(at)gmail(dot)com>

 * */


$context = new ZMQContext(1);


// Socket to talk to clients

$responder = new ZMQSocket($context, ZMQ::SOCKET_REP);

$responder->bind("tcp://*:5555");


while (true) {

    $request = $responder->recv();

    printf ("Received request: [%s]\n", $request);


    logtxt($request);


    usleep (100);

    $responder->send("World");

}


function logtxt($msg){

    $handler = fopen("/tmp/log/zmq.log","a+");

    fwrite($handler,date('Y-m-d H:i:s').'    '.$msg."\r\n");

    fclose($handler);

}



當我執行

#php zmqserver.php

的時候,報錯,找不到"ZMQContext"的類


使用

#php -m

查看加載的類,發現木有,只有一些默認的


繼續使用

#php --ini

發現加載INI的路徑並不是/etc/php.ini


於是cp /etc/php.ini /usr/local/php/lib/php.ini

再次執行

#php zmqserver.php

ok


查看一下5555端口是否被監聽:

#lsof -i:5555

在我的機器上出現了

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

php     35145 root    9u  IPv4 272314      0t0  TCP *:personal-agent (LISTEN)


表明監聽成功.



下面編寫client端


zmqclient.php

<?php

/*

 * * Hello World client

 * * Connects REQ socket to tcp://localhost:5555

 * * Sends "Hello" to server, expects "World" back

 * * @author Ian Barber <ian(dot)barber(at)gmail(dot)com>

 * */


$context = new ZMQContext();


// Socket to talk to server

echo "Connecting to hello world server…\n";

$requester = new ZMQSocket($context, ZMQ::SOCKET_REQ);

$requester->connect("tcp://localhost:5555");

$date = mktime();



if($requester->send($date) !== false){

    echo "send success\n";

}

$reply = $requester->recv();

printf ("Received:[%s]\n",$reply);



從瀏覽器訪問zmqclient.php

可以正確接收到World的數據,同時在/tmp/log/zmq.log下面有新生成的日誌文件

表明一切正常。


PS:

此例中使用了簡單的:rep/req的請求應答模式,其實zmq支持的模式非常多,使用的場景也不盡相同,可以根據自己的實際情況靈活的選擇合適的模式。


一些有用的資源:

官網:http://zeromq.org/

http://blog.fity.cn/post/382/

http://iyuan.iteye.com/category/148998


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章