Home > php教程 > php手册 > php amqp 消息队列 RabbitMQ 基本概念(二)

php amqp 消息队列 RabbitMQ 基本概念(二)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:43:57
Original
1196 people have browsed it

连接: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 显示为空

php amqp 消息队列 RabbitMQ  基本概念(二)


运行下面代码再查看链接

connect.php

<?php $connect = new AMQPConnection();
$connect->connect();

while (true) {
    
}
Copy after login

php amqp 消息队列 RabbitMQ  基本概念(二)


rabbitmqctl.bat -q list_connections 现在服务器的链接

php amqp 消息队列 RabbitMQ  基本概念(二)



信道:AMQPChannel

rabbitmqctl.bat -q list_channels 显示为空

php amqp 消息队列 RabbitMQ  基本概念(二)


运行代码channel.php

<?php $connect = new AMQPConnection();
$connect->connect();

$channel = new AMQPChannel($connect);

while (true) {
    
}
Copy after login

rabbitmqctl.bat -q list_channels 显示如下

php amqp 消息队列 RabbitMQ  基本概念(二)


交换机:Exchange

rabbitmqctl.bat -q list_exchanges 显示如下 下面是系统默认交换机

php amqp 消息队列 RabbitMQ  基本概念(二)


运行exchange.php

<?php $connect = new AMQPConnection();
$connect->connect();

$channel = new AMQPChannel($connect);

$exchange = new AMQPExchange($channel);
$exchange->setName('exchange_name');
$exchange->setType(AMQP_EX_TYPE_DIRECT);
$exchange->declare();

while (true) {
    
}
Copy after login

rabbitmqctl.bat -q list_exchanges 显示如下

php amqp 消息队列 RabbitMQ  基本概念(二)

队列服务不重启那么这个exchange_name交换机就会一直存在


队列:AMQPQueue

rabbitmqctl.bat -q list_queues 显示如下

php amqp 消息队列 RabbitMQ  基本概念(二)


我们继续cmd运行 queue.php

<?php $connect = new AMQPConnection();
$connect->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) {
    
}
Copy after login

再运行 rabbitmqctl.bat -q list_queues

php amqp 消息队列 RabbitMQ  基本概念(二)



绑定和路由键:bind & routing_key

rabbitmqctl.bat -q list_bindings 显示如下

php amqp 消息队列 RabbitMQ  基本概念(二)


我们运行如下php代码 bind.php

<?php $connect = new AMQPConnection();
$connect->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) {
    
}
Copy after login

再次运行rabbitmqctl.bat -q list_bindings 显示如下

php amqp 消息队列 RabbitMQ  基本概念(二)


信息:Envelope

下面我们把上面的bind.php改一下变成一个接收端(处理信息端)

<?php $connect = new AMQPConnection();
$connect->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());
}
Copy after login
在dos中运行 如下

php amqp 消息队列 RabbitMQ  基本概念(二)


我们再写个发送端envelope.php

<?php $connect = new AMQPConnection();
$connect->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();
Copy after login

运行envelope.php后可以看见接收端收到了信息

php amqp 消息队列 RabbitMQ  基本概念(二)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template