您当前的位置:学无止境 > rabbitmq的fanout模式消息队列的简单运用网站首页学无止境
rabbitmq的fanout模式消息队列的简单运用
发布时间:2024-07-16 14:14:13编辑:三青查看次数:1900
注意:默认根目录已经安装composer下载安装rabbitmq(php-amqplib/php-amqplib)扩展
fanout模式消息队列生产者
publish.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibMessageAMQPMessage;
$dbName = 'sanqing';
$dbPwd = '111111';
$tableName = 'order';
$connection = new AMQPStreamConnection('localhost', 5672, $dbName, $dbPwd, $tableName);
$channel = $connection->channel();
$exc_name = 'exch';
$channel->exchange_declare($exc_name,'fanout',false,false,false);
$data = 'this is fanout message';
$msg = new AMQPMessage($data,['delivery_mode'=>AMQPMessage::DELIVERY_MODE_PERSISTENT]);
$channel->basic_publish($msg,$exc_name);
$channel->close();
$connection->close();fanout模式消息队列消费者
sub.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
$dbName = 'sanqing';
$dbPwd = '111111';
$tableName = 'order';
$connection = new AMQPStreamConnection('localhost', 5672, $dbName, $dbPwd, $tableName);
$channel = $connection->channel();
$exc_name = 'exch';
$channel->exchange_declare($exc_name,'fanout',false,false,false);
list($queue_name,,) = $channel->queue_declare('',false,false,true,false);
$channel->queue_bind($quene_name,$exc_name);
$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();可以在复试一份sub.php改名sub2.php
运行sub.php和sub2.php
php sub.php
php sub.php
运行publish.php
php publish.php
sub.php和sub2.php都能收到信息,这就是fanout模式
关键字词:rabbitmq,fanout模式,fanout,php,广播,Exchange,消息队列,队列
评论:
