composer 安装
composer require php-amqplib/php-amqplib
代码如下
// +----------------------------------------------------------------------
namespace app\system\admin;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use think\App;
use think\Controller;
class Rabbit extends Controller
{
const exchange = 'router';
const queue = 'msg';
const consumerTag = 'consumer';
static $connection = null;
public function __construct(App $app = null)
{
parent::__construct($app);
//创建连接
self::$connection = new AMQPStreamConnection('192.168.0.8','5672','admin','123456');
}
/** 生产端
* @return string
* @throws \Exception
*/
public function product(){
$data = [
'id'=>1,
'data'=>'我爱你',
];
//从连接中获取通道
$channel = self::$connection->channel();
//创建队列声明
$channel->queue_declare(self::queue,false,true,false,false);
//创建交换机声明
$channel->exchange_declare(self::exchange,'direct',false,true,false);
$channel->queue_bind(self::queue,self::exchange);
$msg_body = json_encode($data);
$message = new AMQPMessage($msg_body,['content_type'=>'text/plain','delivery_mode'=>AMQPMessage::DELIVERY_MODE_NON_PERSISTENT]);
$channel->basic_publish($message,self::exchange,'',false);
$channel->close();
self::$connection->close();
return 'ok';
}
/**消费端
* @throws \ErrorException
*/
public function consumer(){
//创建频道
$channel = self::$connection->channel();
$channel->queue_declare(self::queue,false,true,false,false);
$channel->exchange_declare(self::queue,'direct',false,true,false);
$channel->queue_bind(self::queue,self::exchange);
$channel->basic_consume(self::queue,self::consumerTag,false,false,false,false,[$this
,'process_message']);
register_shutdown_function([$this,'closeMQ'],$channel,self::$connection);
dump($channel->callbacks);
while (count($channel->callbacks)){
$channel->wait();
//sleep(2);
trace(\think\facade\Log::getLog());
}
\think\facade\Log::write('starting:'.'3',2);
}
function process_message($message)
{
$this->write_log('body:'.$message->body,2);
if ($message->body !== 'quit') {
$obj = json_decode($message->body);
if (!isset($obj->id)) {
echo 'error data\n';
$this->write_log("【错误异常1】 data:" . $message->body, 2);
} else {
try {
$this->write_log("【正常数据】 data:" . json_encode($message),2);
} catch (\Think\Exception $e) {
$this->write_log($e->getMessage(), 2);
$this->write_log('【错误异常2】 data:'.json_encode($message), 2);
} catch (\PDOException $pe) {
$this->write_log($pe->getMessage(), 2);
$this->write_log('【错误异常2】 data:'.json_encode($message), 2);
}
}
}
$message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
// Send a message with the string "quit" to cancel the consumer.
if ($message->body === 'quit') {
$message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']);
}
}
function closeMQ($channel, $connection)
{
$channel->close();
$connection->close();
$this->write_log("closed",3);
}
public function write_log($data='str:',$status=1){
switch ($status){
case 1 :
$type = 'waning';
break;
case 2 :
$type = 'error';
break;
case 3 :
$type = 'note';
break;
}
\think\facade\Log::write('End '.$data.'&status='.$status.''.'&=',$type);
}
}
执行效果 给生产者发送数据
消费者接受数据