RabbitMQ php 使用

RabbitMQ是一个开源的基于AMQP(Advanced Message Queuing Protocol)标准,并且可靠性高的企业级消息系统,目前很多网站在用,包括reddit,Poppen.de等。

1. 安装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:)

 

 

 

<?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";
    }
}
 
?>

consumer:
?View Code PHP

<?php
/**
 * consumer demo
 *
 * @author wei
 * @version $Id$
 **/
$params = array('host' =>'localhost',
                'port' => 5672,
                'login' => 'guest',
                'password' => 'guest',
                'vhost' => '/');
$cnn = new AMQPConnect($params);
 
// create the Queue
$queue = new AMQPQueue($cnn, 'queue1');
$queueMessages = $queue->consume(100);
 
foreach($queueMessages as $item) {
    echo "$i.$item\n";
}
?>

 

 

 

参考:
http://www.rabbitmq.com/install.html
http://blog.ftofficer.com/2010/03/translation-rabbitmq-python-rabbits-and-warrens/
http://code.google.com/p/php-rabbit/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值