赞
踩
怎么安装rabbitmq请查看之前课程,如果已经安装,请略过此步。
mkdir vendor
cd vendor
composer require php-amqplib/php-amqplib
- cd ../
- mkdir worker
- cd worker
- touch pro.php
- <?php
-
- require_once '../vendor/autoload.php';
-
-
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- use PhpAmqpLib\Message\AMQPMessage;
-
- $v_host = 'order';
-
- $queue_name = 'task_queue';
-
- $connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);
-
- $channel = $connection->channel();
-
-
- $channel->queue_declare($queue_name,false,true,false,false);
-
-
- for($i=1;$i<=10;$i++){
-
- $data = 'this is '.$i.' message';
-
-
- $msg = new AMQPMessage($data,['delivery_mode'=>AMQPMessage::DELIVERY_MODE_PERSISTENT]);
-
-
- $channel->basic_publish($msg,'',$queue_name);
-
- }
-
- $channel->close();
-
- $connection->close();
touch worker.php
- <?php
-
- require_once '../vendor/autoload.php';
-
-
- use PhpAmqpLib\Connection\AMQPStreamConnection;
-
- $v_host ='order';
- $queue_name = 'task_queue';
-
- $connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);
-
- $channel = $connection->channel();
-
- $channel->queue_declare($queue_name,false,true,false,false);
-
-
- $callback = function($msg){
- echo 'received ',$msg->body,"\n";
- $msg->ack();
-
- };
-
- $channel->basic_qos(null,1,null);
-
- $channel->basic_consume($queue_name,'',false,false,false,false,$callback);
-
- while($channel->is_open()){
- $channel->wait();
- }
-
-
-
-
- $channel->close();
-
- $connection->close()
php pro.php
php worker.php
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。