1.安装RabbitMQ
安装RabbitMQ之前首先要安装Erlang语言开发包,下载地址http://erlang.org/download/otp_win64_21.1.exe(根据自己windows版本选择) 默认安装即可
配置环境变量 ERLANG_HOME C:Program Files (x86)erl5.9 (安装erlang的目录)
添加到PATH %ERLANG_HOME%in;
下载安装RabbitMQ,下载地址:https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.9
配置环境变量 RABBITMQ_SERVER C:Program Files (x86)RabbitMQ Server abbitmq_server-2.8.0(安装地址)
添加到PATH %RABBITMQ_SERVER%sbin;
2.以管理员身份进入rabbitmq 安装目录看到如图所示文件
(1)启动rabbitmq-server服务
./rabbitmq-server.bat
(2)查看状态
./rabbitmqctl.bat status
3.执行rabbitmq-plugins.bat enable rabbitmq_management
然后,浏览器中输入:127.0.0.1:15672,用户名密码是guest ,如果能登陆就说明安装成功。
4.安装php的amqp的扩展
先用phpinfo()查看php版本信息,及
最后根据上面的信息去下载相应的amqp版本:http://pecl.php.net/package/amqp
据上面信息我们的是32位非线程安全版本
解压后:
将php_amqp.dll复制到php/ext,同时在php.ini中添加如下代码:
[amqp]
extension=php_amqp.dll
然后将rabbitmq.1.dll复制到php根目录C:/xampp/php/,同时修改apache配置文件httpd.conf,添加如下代码:
# rabbitmq
LoadFile "C:/xampp/php/rabbitmq.1.dll"
最后重启看看是否已经加载了amqp模块:
5.实例
consumer.php(消费者)
<?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest', 'vhost'=>'/' ); $e_name = 'e_linvo'; //交换机名 $q_name = 'q_linvo'; //队列名 $k_route = 'key_1'; //路由key //创建连接和channel $conn = new AMQPConnection($conn_args); if (!$conn->connect()) { die("Cannot connect to the broker! "); } $channel = new AMQPChannel($conn); //创建交换机 $ex = new AMQPExchange($channel); $ex->setName($e_name); $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型 $ex->setFlags(AMQP_DURABLE); //持久化 echo "Exchange Status:".$ex->declareExchange()." "; //创建队列 $q = new AMQPQueue($channel); $q->setName($q_name); $q->setFlags(AMQP_DURABLE); //持久化 echo "Message Total:".$q->declareQueue()." "; //绑定交换机与队列,并指定路由键 echo 'Queue Bind: '.$q->bind($e_name, $k_route)." "; //阻塞模式接收消息 echo "Message: "; while(True){ $q->consume('processMessage'); //$q->consume('processMessage', AMQP_AUTOACK); //自动ACK应答 } $conn->disconnect(); /** * 消费回调函数 * 处理消息 */ function processMessage($envelope, $queue) { $msg = $envelope->getBody(); echo $msg." "; //处理消息 $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答 } ?>
publisher.php(生产者)
<?php //配置信息 $conn_args = array( 'host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest', 'vhost'=>'/' ); $e_name = 'e_linvo'; //交换机名 //$q_name = 'q_linvo'; //无需队列名 $k_route = 'key_1'; //路由key //创建连接和channel $conn = new AMQPConnection($conn_args); if (!$conn->connect()) { die("Cannot connect to the broker! "); } $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 = "TEST MESSAGE!".date("h:i:sa"); echo "Send Message:".$ex->publish($message, $k_route)." "; } //$channel->commitTransaction(); //提交事务 $conn->disconnect(); ?>
开启dos窗口运行消费者
php xxx目录/consumer.php
$ php ./consumer.php
Exchange Status:1
Message Total:0
Queue Bind: 1
Message:
在开启窗口运行生产者
$ php ./publisher.php Send Message:1 Send Message:1 Send Message:1 Send Message:1 Send Message:1
消费者接收到消息