RabbitMQ-消息队列之routing使用

1、安装rabbitmq

怎么安装rabbitmq请查看之前课程,如果已经安装,请略过此步。

2、创建vendor文件夹或是直接采用PHP框架

mkdir vendor

3、进入文件

cd vendor

4、安装php扩展

composer require php-amqplib/php-amqplib

5、进入上级创建routing文件夹

cd ../
mkdir routing

6、进入routing文件并创建生产者php

cd routing
touch publish.php

7、输入routing生产者内容

<?php

require_once  '../vendor/autoload.php';


use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

$v_host = 'order';

$exc_name ='direct_log';

$routing_key = 'warn';

$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);

$channel = $connection->channel();


$channel->exchange_declare($exc_name,'direct',false,false,false);

$data = 'this is '.$routing_key.' message';

$msg = new AMQPMessage($data,['delivery_mode'=>AMQPMEssage::DELIVERY_MODE_PERSISTENT]);


$channel->basic_publish($msg,$exc_name,$routing_key);

$channel->close();

$connection->close();

8、创建消费者php

touch error.php
touch info.php
touch warn.php

9、输入routing消费者内容

vi error.php
<?php

require_once  '../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

$v_host ='order';

$exc_name = 'direct_log';

$routing_key = 'error';

$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);

$channel = $connection->channel();

$channel->exchange_declare($exc_name,'direct',false,false,false);


list($queue_name,,) = $channel->queue_declare('',false,false,true,false);

$channel->queue_bind($queue_name,$exc_name,$routing_key);

$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();

vi info.php
<?php

require_once  '../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

$v_host ='order';

$exc_name = 'direct_log';

$routing_key = 'info';

$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);

$channel = $connection->channel();

$channel->exchange_declare($exc_name,'direct',false,false,false);


list($queue_name,,) = $channel->queue_declare('',false,false,true,false);

$channel->queue_bind($queue_name,$exc_name,$routing_key);

$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();

vi warn.php
<?php

require_once  '../vendor/autoload.php';

use PhpAmqpLib\Connection\AMQPStreamConnection;

$v_host ='order';

$exc_name = 'direct_log';

$routing_key = 'warn';

$connection = new AMQPStreamConnection('localhost',5672,'wangyulong','wangyulong',$v_host);

$channel = $connection->channel();

$channel->exchange_declare($exc_name,'direct',false,false,false);


list($queue_name,,) = $channel->queue_declare('',false,false,true,false);

$channel->queue_bind($queue_name,$exc_name,$routing_key);

$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();

10、执行消费者

php error.php
php info.php
php warn.php

11、执行生产者

php publish.php
此时你会发现只有warn.php对应的消费者才有数据,如果你把publish.php生产者里面的$routing_key对应的值改成error,你会发现只有error.php对应的消费者才有数据。这就是rabbitmq routing工作模式硬实力
相关推荐
ELI_He9993 分钟前
PHP中替换某个包或某个类
开发语言·php
字节程序员6 分钟前
Jmeter分布式压力测试
分布式·jmeter·压力测试
ProtonBase22 分钟前
如何从 0 到 1 ,打造全新一代分布式数据架构
java·网络·数据库·数据仓库·分布式·云原生·架构
时时刻刻看着自己的心26 分钟前
clickhouse分布式表插入数据不用带ON CLUSTER
分布式·clickhouse
2401_857439697 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
Data跳动9 小时前
Spark内存都消耗在哪里了?
大数据·分布式·spark
Java程序之猿11 小时前
微服务分布式(一、项目初始化)
分布式·微服务·架构
来一杯龙舌兰11 小时前
【RabbitMQ】RabbitMQ保证消息不丢失的N种策略的思想总结
分布式·rabbitmq·ruby·持久化·ack·消息确认
节点。csn13 小时前
Hadoop yarn安装
大数据·hadoop·分布式
Smile灬凉城66614 小时前
反序列化为啥可以利用加号绕过php正则匹配
开发语言·php