RabbitMQ三种常用交换机类型原理和PHP实现

fanout交换器

会把所有发送到该交换器的消息路由到所有与该交换器绑定的队列中,此时生产者投递消息时不需要指定route。

也就是说用fanout交换器发送消息的生产者,会将消息发送到所有与该交换器绑定的队列上。(通过交换器名识别)

示例代码

publisher.php

<?php

date_default_timezone_set("Asia/Shanghai");

$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'why',
    'password' => 'why',
    'vhost' => 'why'
);

$ex_name = 'test_exchange';

$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}

$channel = new AMQPChannel($conn);

$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('fanout');
$exchange->declareExchange(); 

$message = 'test success';
echo date('Y-m-d H:i:s', time()) .':' . $exchange->publish($message) . "\n";

$conn->disconnect();   

consumer.php

<?php
date_default_timezone_set("Asia/Shanghai");
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'why',
    'password' => 'why',
    'vhost' => 'why'
);

$ex_name = 'test_exchange';
$queue_name = 'queue1';
$route = 'why_route';

$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}

$channel = new AMQPChannel($conn);

$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('fanout');
$exchange->declareExchange();

$queue = new AMQPQueue($channel);
$queue->setName($queue_name);
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$queue->bind($ex_name, $route);                

while(true)
{
    $queue->consume('consume_msg');
}

$conn->disconnect();

function consume_msg($envelope, $queue){
    $msg = $envelope->getBody();
    echo $msg."\n";
    $queue->ack($envelope->getDeliveryTag());
}                                   

consumer1.php

<?php
date_default_timezone_set("Asia/Shanghai");
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'why',
    'password' => 'why',
    'vhost' => 'why'
);

$ex_name = 'test_exchange';
$queue_name = 'queue2';
$route = 'why_route';

$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}

$channel = new AMQPChannel($conn);

$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('fanout');
$exchange->declareExchange();

$queue = new AMQPQueue($channel);
$queue->setName($queue_name);
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$queue->bind($ex_name, $route);                

while(true)
{
    $queue->consume('consume_msg');
}

$conn->disconnect();

function consume_msg($envelope, $queue){
    $msg = $envelope->getBody();
    echo $msg."\n";
    $queue->ack($envelope->getDeliveryTag());
}                                   

下面开启两个消费者,并通过一个消费者发送消息,可以看到两个队列queue1和queue2同时收到消息并消费

[root@www html]# php publisher.php
2019-09-26 17:33:01:1
[root@www html]# 


[root@www html]# php consumer.php
test success



[root@www html]# php consumer1.php
test success

 

direct交换器

会把消息路由到那些绑定路由键完全匹配的队列中

也就是说生产者使用direct类型的交换器生产消息,并且该交换器绑定了路由键key,那么消费者必须将队列通过声明的交换器绑定到相同的路由键key上才可以收到消息。

示例代码

publisher.php

<?php

date_default_timezone_set("Asia/Shanghai");

$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'why',
    'password' => 'why',
    'vhost' => 'why'
);

$ex_name = 'test_exchange';
$route = 'route1';

$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}

$channel = new AMQPChannel($conn);

$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('direct');
$exchange->declareExchange();


$message = 'test success';
echo date('Y-m-d H:i:s', time()) .':' . $exchange->publish($message, $route) . "\n";

$conn->disconnect();  

consumer.php

<?php
date_default_timezone_set("Asia/Shanghai");
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'why',
    'password' => 'why',
    'vhost' => 'why'
);

$ex_name = 'test_exchange';
$queue_name = 'queue1';
$route = 'route1';

$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}

$channel = new AMQPChannel($conn);

$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('direct');
$exchange->declare();

$queue = new AMQPQueue($channel);
$queue->setName($queue_name);
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$queue->bind($ex_name, $route);   

while(true)
{
    $queue->consume('consume_msg');
}

$conn->disconnect();

function consume_msg($envelope, $queue){
    $msg = $envelope->getBody();
    echo $msg."\n";
    $queue->ack($envelope->getDeliveryTag());
}        

consumer1.php

<?php
date_default_timezone_set("Asia/Shanghai");
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

$conn_args = array(
    'host' => '127.0.0.1',
    'port' => '5672',
    'login' => 'why',
    'password' => 'why',
    'vhost' => 'why'
);

$ex_name = 'test_exchange';
$queue_name = 'queue2';
$route = 'route2';

$conn = new AMQPConnection($conn_args);
if(!$conn->connect())
{
    die("connect error");
}

$channel = new AMQPChannel($conn);

$exchange = new AMQPExchange($channel);
$exchange->setName($ex_name);
$exchange->setType('direct');
$exchange->declare();

$queue = new AMQPQueue($channel);
$queue->setName($queue_name);
$queue->setFlags(AMQP_DURABLE);
$queue->declare();
$queue->bind($ex_name, $route);   

while(true)
{
    $queue->consume('consume_msg');
}

$conn->disconnect();

function consume_msg($envelope, $queue){
    $msg = $envelope->getBody();
    echo $msg."\n";
    $queue->ack($envelope->getDeliveryTag());
}        

运行结果:绑定route1的queue1可接收消息并消费,而绑定route2的queue2并没收到消息

[root@www html]# php publisher.php
2019-09-26 17:51:59:1
[root@www html]# 

[root@www html]# php consumer.php
test success

[root@www html]# php consumer1.php
          

 

topic交换器

前提条件和direct交换器相同,只不过对key的匹配放宽了条件,可以通过*匹配单个字段或通过#匹配多个字段,具体可以参考另一篇文章:https://blog.csdn.net/why444216978/article/details/101405499

 

参考文档:http://docs.php.net/manual/da/book.amqp.php

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AirGo.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值