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

fanout交换器

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

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

示例代码

publisher.php

 '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

 '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

 '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

 '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

 '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

 '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

你可能感兴趣的:(RabbitMQ)