rabbitmq使用_php中rabbitmq的使用

一、安装:

1.安装rabbitmq运行环境

1)下载地址:https://www.erlang.org/downloads

2)下载完成之后,安装,直接下一步、下一步。

3)安装完成或,配置环境变量。

​ 变量名:ERLANG_HOME

​ 变量值:根据你安装的路径,如果都是下一步的话路径就是:C:Program Fileserl-23.0

365c0f502607765e889508b357a28ab7.png

配置path路径:

8453fcb8e93314bbe927a879d71986ff.png

2.安装rabbitmq。

​ 1.下载地址:https://www.rabbitmq.com/download.html。

​ 2)下载完成之后,安装,直接下一步、下一步。

​ 3)进入安装路径,如果是直接下一步的话,路径是:C:Program FilesRabbitMQ Serverrabbitmq_server-3.8.6sbin,执行rabbitmq-plugins enable rabbitmq_management开启管理界面

b89f8cfe77adb0bd8070f5ffdbdfabae.png

4)启动。

8453fcb8e93314bbe927a879d71986ff.png

点击右键,用管理员的身份运行。

5)然后再浏览器中输入:http://localhost:15672/#/exchanges,地址,就可以看到如下界面了

b89f8cfe77adb0bd8070f5ffdbdfabae.png

6)输入rabbitmq的初始密码:Username:guest,Password:guest,就可以看到如下界面了:

d4c579b6db92730f77c815f13bbf0c12.png

​ 7)rabbitmq登录进来,新建一个用户。

bfd6c3d8cb8b113bac0ca11f415946e7.png

比如我新建的 Username:tianbin_test ,password:tianbin_test

8)选择标签(选Admin)

62a8adf5231c4d02556f975507f7a1a4.png

9)用户添加成功。

2be1d9675bfa8c525e06c3c936c43b84.png

10)没有虚拟主机,新建一个虚拟主机

68c85112ffe9e653e0f7e541b6e24ea6.png

11)我这里填写的

dcc16507a78112cc1cb4db39ad235b92.png

12)添加成功的界面如下:

71d58e23c7d5b041950cdd11e70ace44.png

13)设置权限

8f137a524f21cfd6e2f9a7b19ae27fe0.png

97430405ab794ded2205805d9aabf578.png

fe79cda4913d4a0d0a8bc295d39fe275.png

14)查看权限

249b375680aa3ff7d3430614862cc50b.png

15)over。

二.rabbitmq的五种模式。

1.简单模式

0483ccc35bb591ea4ffdd46b08b0b91c.png

说明:

1.生产者将消息交给默认的交换器(AMQP default) 2 .交换器获取消息后交给绑定这个生产者的队列(关系是通过队列名称完成) 3.监听当前队列的消费者获取消息,执行消费逻辑

使用:

1)使用tianbin_test用户登录。

479c17b524220a4ba7c7c04573f04cf1.png

2)设置权限

7e714e022e9f66341c4c4e648fa06104.png

3).进入某个文件夹

​ 在控制台中执行:composer init

​ 然后再执行 :composer require php-amqplib/php-amqplib

​ 代码结果如下:

c526fcf4fa7260fd6a2ec7145011cbd8.png

新建simple文件夹和文件,最终结果如下:

46c7b8b2fc01f4ba430067851ae1acbf.png

simple文件夹中customer文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
$exchange = 'simple_query';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->queue_declare($exchange,false,false, false, false);
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "n";
};

$channel->basic_consume($exchange, '', false, false, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

simple文件夹中producter文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
use PhpAmqpLibMessageAMQPMessage;

$exchange = 'simple_query';
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'tianbin_test', 'tianbin_test');
$AMQPChannel = $connection->channel();
$AMQPChannel->queue_declare($exchange,false,false, false, false);
$message = '简单模式';
$AMQPMessage = new AMQPMessage($message,array('content_type' => 'text/plain'));
$AMQPChannel->basic_publish($AMQPMessage,'',$exchange);
$AMQPChannel->close();
$connection->close();

执行:php producter.php,结果如下:

d99292329dd8a3bd3f7a78bc00f0d24d.png

2805908668c4d26c25ff617f479add2e.png

​ 执行:php customer.php,结果如下:

7fe600e26dbe27b6715be2f1b1e6d4b7.png

备注:如果要手动进行消息确认(就是确定该消息有没有被消费成功)

请使用如下代码:

customer代码如下

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
$exchange = 'simple_query';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->queue_declare($exchange,false,true, false, false);
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "n";
  //这里抛异常,该消息不会被消费
    $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$channel->basic_consume($exchange, '', false, false, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

producter代码如下

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
use PhpAmqpLibMessageAMQPMessage;

$exchange = 'simple_query';
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'tianbin_test', 'tianbin_test');
$AMQPChannel = $connection->channel();
//$AMQPChannel->queue_declare($exchange,false,false, false, false);
$AMQPChannel->queue_declare($exchange,false,true, false, false);
$message = '简单模式';
$AMQPMessage = new AMQPMessage($message, array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
$AMQPChannel->basic_publish($AMQPMessage,'',$exchange);
$AMQPChannel->close();
$connection->close();

演示:

1)执行:php producter.php,结果如下:

665d775c50f3aba7285e2e7fe31d9be5.png

f442851eb87cb062f122442e75513bce.png

2)修改customer,结果如下:

66db520f3ded8c0daef113e19b29aab7.png

3)执行

php customer.php

4)查看

7d2452f7e5b6da03ca92f48576e07f5a.png

5)结果如下:

4801c3549a5dcbb79a971aa2c95a3c6b.png

7)如果我将异常去掉,执行

0b3dc8e0d74c260156c5e5179d9af52a.png

发现该消息已经被消费了。

2.工作模式

5e1c413aefd8a5e3cfea3a9cf8efda39.png

说明:

  1. 生产者将消息交个交换机
  2. 交换机交给绑定的队列
  3. 队列由多个消费者同时监听,只有其中一个能够获取这一条消息,形成了资源的争抢,谁的资源空闲大,争抢到的可能越大;

使用:

新建work目录和文件,最终目录结果如下

6b0d8a31e1a8abdb43d23f50058c2c5f.png

2)代码如下:

work文件夹producter的文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
use PhpAmqpLibMessageAMQPMessage;

$exchange = 'task_query';
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'tianbin_test', 'tianbin_test');
$AMQPChannel = $connection->channel();
$AMQPChannel->queue_declare($exchange,false,true, false, false);
$message = '';

for ($i =0;$i<50;$i++){
    $message= '工作模式'.(string)$i;
    $AMQPMessage = new AMQPMessage($message,array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT));
    $AMQPChannel->basic_publish($AMQPMessage,'',$exchange);
}
$AMQPChannel->close();
$connection->close();

work文件夹customerOne的文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

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

use PhpAmqpLibConnectionAMQPStreamConnection;

$exchange = 'task_query';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->queue_declare($exchange, false, true, false, false);
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "n";
    echo " [x] Donen";
    sleep(10);
    $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$channel->basic_consume($exchange, '', false, false, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

work文件夹customerTwo的文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
$exchange = 'task_query';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->queue_declare($exchange,false,true, false, false);
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "n";
    echo " [x] Donen";
    $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};

$channel->basic_consume($exchange, '', false, false, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

3)执行

php customerOne.php

php customerTwo.php

php producter.php

4)结果如下:

1991421832e4a06b9a265fb2c999893e.png

241134fa8bd79e41e8b6fa3f485ddc19.png

发现没有多劳多得。

6)修改代码

customerTwo的文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;
$exchange = 'task_query';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->queue_declare($exchange,false,true, false, false);
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "n";
    echo " [x] Donen";
    $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
$channel->basic_qos(null,1,null);//每个消费者只能处理一条信息
$channel->basic_consume($exchange, '', false, false, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

customerOne文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

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

use PhpAmqpLibConnectionAMQPStreamConnection;

$exchange = 'task_query';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->queue_declare($exchange, false, true, false, false);
$callback = function ($msg) {
    echo ' [x] Received ', $msg->body, "n";
    echo " [x] Donen";
    sleep(10);
    $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
};
$channel->basic_qos(null,1,null);//每个消费者只能处理一条信息
$channel->basic_consume($exchange, '', false, false, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}

7)执行

php customerOne.php

php customerTwo.php

php producter.php

8)结果如下:

d9ba90c681cacad200dba0af025bb6eb.png

2227098744c4c182dc60fca59b7fedfe.png

9)over。

3.发布订阅模型

85ffef7384e44059f956d7dcf418dae3.png

说明:

1 .生产者扔给交换机消息 2 .交换机根据自身的类型(fanout)将会把所有消息复制同步到所有与其绑定的队列 3 .每个队列可以有一个消费者,接收消息进行消费逻辑

使用:

1)新建fanout文件夹和文件,最终结构如下:

7aa07339d9a2a22c7906c097e08f8e94.png

2)代码如下:

customerOne文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'fanout.exchange.test';
$queue_name = 'fanout.query1';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->exchange_declare($exchange, AMQPExchangeType::FANOUT, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);

$channel->queue_bind($queue_name, $exchange);
$callback = function ($msg) {
    echo ' [x] ', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

customerTwo的文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'fanout.exchange.test';
$queue_name = 'fanout.query1';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->exchange_declare($exchange, AMQPExchangeType::FANOUT, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);

$channel->queue_bind($queue_name, $exchange);
$callback = function ($msg) {
    echo ' [x] ', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

producter文件内容如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'fanout.exchange.test';
$queue_name = 'fanout.query1';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->exchange_declare($exchange, AMQPExchangeType::FANOUT, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);

$channel->queue_bind($queue_name, $exchange);
$callback = function ($msg) {
    echo ' [x] ', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

3)执行

php customerOne.php

php customerTwo.php

php producter.php

4)结果如下:

​ producter结果:

5ab3346e99fd259ff0ccac2bec535b14.png

customerOne结果如下:

bdd55feee0a5c534db206fab27871d84.png

customerTwo结果如下:

aa23b411bcb2fb1749fb61dd4c2e158e.png

4.路由模式

9f48cb5d1bebcec5c6bd56fdd8851912.png

说明:

1.生产者还是将消息发送给交换机,消息携带具体的路由key(routingKey) 2.交换机类型direct,将接收到的消息中的routingKey,比对与之绑定的队列的routingKey 3.消费者监听一个队列,获取消息,执行消费逻辑

使用:

1)新建direct文文件夹和文件,最终结果如下:

c7311698d5fbdc8c92e35ae6fba09248.png

2)代码如下:

customerOne代码如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'fanout.exchange.test';
$queue_name = 'fanout.query1';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->exchange_declare($exchange, AMQPExchangeType::FANOUT, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);

$channel->queue_bind($queue_name, $exchange);
$callback = function ($msg) {
    echo ' [x] ', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

customerTwo代码如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'fanout.exchange.test';
$queue_name = 'fanout.query2';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$channel->exchange_declare($exchange, AMQPExchangeType::FANOUT, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);
$channel->queue_bind($queue_name, $exchange);
$callback = function ($msg) {
    echo ' [x] ', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

producter代码如下:

<?php
require_once __DIR__ . '/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
use PhpAmqpLibMessageAMQPMessage;

$exchange = 'fanout.exchange.test';
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'tianbin_test', 'tianbin_test');
$AMQPChannel = $connection->channel();
$AMQPChannel->exchange_declare($exchange, AMQPExchangeType::FANOUT,false , false, false);
$message = 'fanout,hello';
$AMQPMessage = new AMQPMessage($message);
$AMQPChannel->basic_publish($AMQPMessage, $exchange);
echo ' [x] Sent ', $message, "n";
$AMQPChannel->close();
$connection->close();

3)最终结果如下:

producter如下:

363c04f6fa5ba14c088d708eb113ebd9.png

customerTwo如下:

2be82e709b5124ea34bb7c1c2d151792.png

customerOne如下:

9f86ae2ab3c26511285ee5d3bab4e3be.png

4)over。

5.主题 模式

82d80d86340bee3ca9c6fe369a505b97.png

说明:

1.生产端发送消息,消息携带具体的路由key 2 .交换机的类型topic 3 .队列绑定交换机不在使用具体的路由key而是一个范围值

*表示一个字符串(不能携带特殊符号) #表示任意字符串

使用:

1)新建topic文件夹和文件,最终结果如下:

2e74c09544b1b619b9b8716227e5287d.png

2)代码如下:

customerOne代码如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'topic.exchange.test';
$queue_name = 'topic.query1';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$binding_keys = [];
$binding_keys[] = '#';
$binding_keys[] = "*";
$channel->exchange_declare($exchange, AMQPExchangeType::TOPIC, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);
foreach ($binding_keys as $binding_key) {
    $channel->queue_bind($queue_name, $exchange, $binding_key);
}
$callback = function ($msg) {
    echo ' [one] ', $msg->delivery_info['routing_key'], ':', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

customerTwo代码如下:

`customerOne`代码如下:<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'topic.exchange.test';
$queue_name = 'topic.query2';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$binding_keys = [];
$binding_keys[] = 'insert.*';
$channel->exchange_declare($exchange, AMQPExchangeType::TOPIC, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);
foreach ($binding_keys as $binding_key) {
    $channel->queue_bind($queue_name, $exchange, $binding_key);
}
$callback = function ($msg) {
    echo ' [one] ', $msg->delivery_info['routing_key'], ':', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

producter代码如下:

<?php
require_once __DIR__.'/../vendor/autoload.php';

use PhpAmqpLibConnectionAMQPStreamConnection;
use PhpAmqpLibExchangeAMQPExchangeType;
$exchange = 'topic.exchange.test';
$queue_name = 'topic.query2';
$connection = new AMQPStreamConnection('localhost', 5672, 'tianbin_test', 'tianbin_test');
$channel = $connection->channel();
$binding_keys = [];
$binding_keys[] = 'insert.*';
$channel->exchange_declare($exchange, AMQPExchangeType::TOPIC, false, false, false);
list($queue_name, ,) = $channel->queue_declare($queue_name, false, false, true, false);
foreach ($binding_keys as $binding_key) {
    $channel->queue_bind($queue_name, $exchange, $binding_key);
}
$callback = function ($msg) {
    echo ' [one] ', $msg->delivery_info['routing_key'], ':', $msg->body, "n";
};

$channel->basic_consume($queue_name, '', false, true, false, false, $callback);
while ($channel->is_consuming()) {
    $channel->wait();
}

3)最终结果如下:

producter结果如下:

6b2e3f52877bb5551774b0aec195ba9f.png

customerOne结果如下:

7c6eb57c9fb34ed93517fa879134e7f5.png

customerTwo结果如下:

18dd876124097c21ba55402da25e3d57.png

如果有错误的话,欢迎指出。

https://gitee.com/tbllmj/phprabbitmq.git​gitee.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值