歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Ubuntu下PHP + RabbitMQ使用

Ubuntu下PHP + RabbitMQ使用

日期:2017/2/28 16:33:23   编辑:Linux教程

RabbitMQ是一個開源的基於AMQP(Advanced Message Queuing Protocol)標准,並且可靠性高的企業級消息系統,目前很多網站在用,包括reddit,Poppen.de等。

1. Ubuntu下安裝RabbitMQ
sudo apt-get install rabbitmq-server
sudo /etc/init.d/rabbitmq-server start

2. 安裝librabbitmq
sudo apt-get install mercurial
hg clone http://hg.rabbitmq.com/rabbitmq-c
cd rabbitmq-c
hg clone http://hg.rabbitmq.com/rabbitmq-codegen codegen
autoreconf -i && ./configure && make && sudo make install

3. 安裝php-rabbit擴展
wget http://php-rabbit.googlecode.com/files/php-rabbit.r91.tar.gz
tar -zxvf php-rabbit.r91.tar.gz
cd php-rabbit.r91
/path/to/php/bin/phpize
./configure –with-amqp –with-php-config=/path/to/php/bin/php-config
make && sudo make install
編輯 php.ini 添加:
extension=rabbit.so
輸出phpinfo看下是否擴展已經加載成功,have fun:)

4. Demo程序
producer:

<?php
/**
 * producer demo
 *
 * @author wei
 * @version $Id$
 **/
$params = array('host' =>'localhost',
                'port' => 5672,
                'login' => 'guest',
                'password' => 'guest',
                'vhost' => '/');
$cnn = new AMQPConnect($params);
 
// declare Exchange
$exchange = new AMQPExchange($cnn);
$exchange->declare('ex1', 'topic', AMQP_DURABLE );
 
// declare Queue
$queue = new AMQPQueue($cnn);  
$queue->declare('queue1', AMQP_DURABLE); 
 
// bind Queue
$queue->bind('ex1','wei.#');
 
// publishing
$msg = "msg";
 
for ($i=0; $i < 100; $i++) { 
	$res = $exchange->publish($i . 'msg', 'wei.' . $i);
	if ($res) {
		echo $i . 'msg' . " Yes\n";
	} else {
		echo $i . 'msg' . " No\n";
	}
}
 
?>
Copyright © Linux教程網 All Rights Reserved