php amqp 消息队列 RabbitMQ 基本概念(二)
程序员文章站
2022-03-11 20:05:18
...
连接:AMQPConnection 先看服务器现有的链接 rabbitmqctl.bat -q list_connections 显示为空 运行下面代码再查看链接 connect.php ?php$connect = new AMQPConnection();$connect-connect();while (true) { } rabbitmqctl.bat -q list_connections 现在服务
连接:AMQPConnection
先看服务器现有的链接
rabbitmqctl.bat -q list_connections 显示为空
运行下面代码再查看链接
connect.php
connect(); while (true) { }
rabbitmqctl.bat -q list_connections 现在服务器的链接
信道:AMQPChannel
rabbitmqctl.bat -q list_channels 显示为空
运行代码channel.php
connect(); $channel = new AMQPChannel($connect); while (true) { }
rabbitmqctl.bat -q list_channels 显示如下
交换机:Exchange
rabbitmqctl.bat -q list_exchanges 显示如下 下面是系统默认交换机
运行exchange.php
connect(); $channel = new AMQPChannel($connect); $exchange = new AMQPExchange($channel); $exchange->setName('exchange_name'); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declare(); while (true) { }
rabbitmqctl.bat -q list_exchanges 显示如下
队列服务不重启那么这个exchange_name交换机就会一直存在
队列:AMQPQueue
rabbitmqctl.bat -q list_queues 显示如下
我们继续cmd运行 queue.php
connect(); $channel = new AMQPChannel($connect); $exchange = new AMQPExchange($channel); $exchange->setName('exchange_name'); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declare(); $queue = new AMQPQueue($channel); $queue->setName('queue_name'); $queue->declare(); while (true) { }
再运行 rabbitmqctl.bat -q list_queues
绑定和路由键:bind & routing_key
rabbitmqctl.bat -q list_bindings 显示如下
我们运行如下php代码 bind.php
connect(); $channel = new AMQPChannel($connect); $exchange = new AMQPExchange($channel); $exchange->setName('exchange_name'); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declare(); $queue = new AMQPQueue($channel); $queue->setName('queue_name'); $queue->declare(); $queue->bind('exchange_name', 'routing_key'); while (true) { }
再次运行rabbitmqctl.bat -q list_bindings 显示如下
信息:Envelope
下面我们把上面的bind.php改一下变成一个接收端(处理信息端)
connect(); $channel = new AMQPChannel($connect); $exchange = new AMQPExchange($channel); $exchange->setName('exchange_name'); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declare(); $queue = new AMQPQueue($channel); $queue->setName('queue_name'); $queue->declare(); $queue->bind('exchange_name', 'routing_key'); while (true) { $queue->consume('functionName'); } function functionName($envelope,$queue) { var_dump($envelope->getBody()); }在dos中运行 如下
我们再写个发送端envelope.php
connect(); $channel = new AMQPChannel($connect); $exchange = new AMQPExchange($channel); $exchange->setName('exchange_name'); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declare(); $exchange->publish('hello world','routing_key'); $connect->disconnect();
运行envelope.php后可以看见接收端收到了信息
推荐阅读
-
PHP+RabbitMQ实现消息队列的完整代码
-
PHP+RabbitMQ实现消息队列的完整代码
-
深入剖析 RabbitMQ —— Spring 框架下实现 AMQP 高级消息队列协议
-
使用PHP访问RabbitMQ消息队列的方法示例
-
RabbitMQ消息队列之基础 (二)
-
消息队列RabbitMQ之Spring-AMQP
-
spring amqp rabbitmq 学习(二) 接收消息
-
C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(二)
-
深入剖析 RabbitMQ —— Spring 框架下实现 AMQP 高级消息队列协议
-
message - 如何用php获取rabbitmq指定队列中的未处理消息数量