php使用zeromq

來源:互聯網
上載者:User

標籤:php   zmq   

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


php使用zeromq

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.