zeromq及php扩展安装

技术文档 - PHP文档 作者: xcheng1986 发布于:2016-07-06 阅读(1002) (暂无评论)  

1.安装ZeroMQ
wget -c https://github.com/zeromq/zeromq3-x/releases/download/v3.2.5/zeromq-3.2.5.tar.gz
tar zxvf zeromq-3.2.5.tar.gz 
cd zeromq-3.2.5/
./configure --prefix=/usr/local/zeromq --with-pgm=libpgm-5.1.118~dfsg
make && make install
ldconfig


2.安装PHP的ZeroMQ扩展
git clone git://github.com/mkoppanen/php-zmq.git
cd php-zmq/
phpize
./configure --with-php-config=/usr/bin/php-config --with-zmq=/usr/local/zeromq
make && make install 
echo 'extension=zmq.so' >> /etc/php5/cli/php.ini
service php5-fpm restart
php -m|grep zmq


3.php测试样例
客户端:client.php:
<?php
/**
 * client
 */
 
$context = new ZMQContext ();
echo "Connecting to hello world server...\n";
$requester = new ZMQSocket ($context, ZMQ::SOCKET_REQ);
$requester->connect ("tcp://localhost:5555");
for($request_nbr = 0; $request_nbr != 10; $request_nbr++) {
	printf ("Sending request %d...\n", $request_nbr);
	$requester->send ("Hello");
	$reply = $requester->recv ();
	printf ("Received reply %d: [%s]\n", $request_nbr, $reply);
}
服务端:server.php:
<?php
/**
 * server
 */

$context = new ZMQContext (1);
$responder = new ZMQSocket ($context, ZMQ::SOCKET_REP);
$responder->bind ("tcp://localhost:5555");
while(true) {
	$request = $responder->recv ();
	printf ("Received request: [%s]\n", $request);
 
	sleep (1);
 
	$responder->send ("World");
}