赞
踩
rabbitmq的安装:https://blog.csdn.net/u010533511/article/details/90752530
php安装amqp扩展
1.安装rabbitmq-c
下载地址:https://github.com/alanxz/rabbitmq-c/releases
- wget -c https://github.com/alanxz/rabbitmq-c/releases/download/v0.8.0/rabbitmq-c-0.8.0.tar.gz
-
- tar zxf rabbitmq-c-0.8.0.tar.gz
-
- cd rabbitmq-c-0.8.0
-
- ./configure --prefix=/usr/local/rabbitmq-c-0.8.0
-
- make && make install
2.安装amqp
官网下载地址:http://pecl.php.net/package/amqp
- wget -c http://pecl.php.net/get/amqp-1.9.3.tgz
-
- tar zxf amqp-1.9.3.tgz
-
- cd amqp-1.9.3
-
- /usr/local/php5.5/bin/phpize
-
- ./configure --with-php-config=/usr/local/php5.5/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c-0.8.0
-
- make && make install
3.修改php.ini
用phpinfo查看是否加载成功
php测试代码:
消息生产者(rabbit_publisher.php)
- <?php
- //配置信息
- $conn_args = array(
- 'host' => '127.0.0.1',
- 'port' => '5672',
- 'login' => 'zcw',
- 'password' => '123456',
- 'vhost'=>'/'
- );
- $e_name = 'exchange1'; //交换机名
- //$q_name = 'queue1'; //无需队列名
- $k_route = 'route1'; //路由key
-
- //创建连接和channel
- $conn = new AMQPConnection($conn_args);
- if (!$conn->connect()) {
- die("Cannot connect to the broker!\n");
- }
- $channel = new AMQPChannel($conn);
-
-
-
- //创建交换机对象
- $ex = new AMQPExchange($channel);
- $ex->setName($e_name);
- date_default_timezone_set("Asia/Shanghai");
- //发送消息
- //$channel->startTransaction(); //开始事务
- for($i=0; $i<5; ++$i){
- sleep(1);//休眠1秒
- //消息内容
- $message = ($i+1)."Send Mssage Success!".date("h:i:sa");
- echo ($i+1)."Send Message:".$ex->publish($message, $k_route)."\n";
- }
-
- //$channel->commitTransaction(); //提交事务
-
- $conn->disconnect();
- ?>
消费者(rabbit_consumer.php)
- <?php
- //配置信息
- $conn_args = array(
- 'host' => '127.0.0.1',
- 'port' => '5672',
- 'login' => 'zcw',
- 'password' => '123456',
- 'vhost'=>'/'
- );
- $e_name = 'exchange1'; //交换机名
- $q_name = 'queue1'; //队列名
- $k_route = 'route1'; //路由key
-
- //创建连接和channel
- $conn = new AMQPConnection($conn_args);
- if (!$conn->connect()) {
- die("Cannot connect to the broker!\n");
- }
- $channel = new AMQPChannel($conn);
-
- //创建交换机
- $ex = new AMQPExchange($channel);
- $ex->setName($e_name);
- $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型(常用的有fanout、direct、topic、headers)
- $ex->setFlags(AMQP_DURABLE); //持久化
- echo "Exchange Status:".$ex->declare()."\n";
-
- //创建队列
- $q = new AMQPQueue($channel);
- $q->setName($q_name);
- $q->setFlags(AMQP_DURABLE); //持久化
- echo "Message Total:".$q->declare()."\n";
-
- //绑定交换机与队列,并指定路由键
- echo 'Queue Bind: '.$q->bind($e_name, $k_route)."\n";
-
- //阻塞模式接收消息
- echo "Message:\n";
- while(True){
- $q->consume('processMessage');
- //$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答
- }
- $conn->disconnect();
-
- /**
- * 消费回调函数
- * 处理消息
- */
- function processMessage($envelope, $queue) {
- $msg = $envelope->getBody();
- echo $msg."\n"; //处理消息
- $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答
- }
- ?>
php rabbit_consumer.php
php rabbit_publisher.php
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。