PHP使用 enqueue/amqp-lib拓展实现rabbitmq任务处理

一:拓展安装

复制代码
composer require enqueue/amqp-lib

文档地址:https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_lib.md

二:方法介绍

1:连接rabbitmq

复制代码
$factory = new AmqpConnectionFactory([
    'host' => '192.168.6.88',//host
    'port' => '5672',//端口
    'vhost' => '/',//虚拟主机
    'user' => 'admin',//账号
    'pass' => 'admin',//密码
]);
$context = $factory->createContext();

2:声明主题

复制代码
//声明并创建主题
$exchangeName = 'exchange';
$fooTopic = $context->createTopic($exchangeName);
$fooTopic->setType(AmqpTopic::TYPE_FANOUT);
$context->declareTopic($fooTopic);

//删除主题
$context->deleteTopic($fooTopic);

3:声明队列

复制代码
//声明并创建队列
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue($queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
$context->declareQueue($fooQueue);

//删除队列
$context->deleteQueue($fooQueue);

4:将队列绑定到主题

复制代码
$context->bind(new AmqpBind($fooTopic, $fooQueue));

5:发送消息

复制代码
//向队列发送消息
$message = $context->createMessage('Hello world!');
$context->createProducer()->send($fooQueue, $message);

//向队列发送优先消息
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue(queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
//设置队列的最大优先级
$fooQueue->setArguments(['x-max-priority' => 10]);
$context->declareQueue($fooQueue);

$message = $context->createMessage('Hello world!');

$context->createProducer()
    ->setPriority(5) //设置优先级,优先级越高,消息越快到达消费者
    ->send($fooQueue, $message);

//向队列发送延时消息
$message = $context->createMessage('Hello world!');

$context->createProducer()
    ->setDelayStrategy(new RabbitMqDlxDelayStrategy())
    ->setDeliveryDelay(5000) //消息延时5秒
    ->send($fooQueue, $message);

6:消费消息【接收消息】

复制代码
//消费消息
$consumer = $context->createConsumer($fooQueue);

$message = $consumer->receive();

// process a message
//业务代码

$consumer->acknowledge($message);//ack应答,通知rabbitmq成功,删除对应任务
// $consumer->reject($message);ack应答,通知rabbitmq失败,不删除对应任务


//订阅消费者
$fooConsumer = $context->createConsumer($fooQueue);

$subscriptionConsumer = $context->createSubscriptionConsumer();
$subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
    // process message
    //业务代码
    $consumer->acknowledge($message);//ack应答,通知rabbitmq成功,删除对应任务
    // $consumer->reject($message);ack应答,通知rabbitmq失败,不删除对应任务

    return true;
});
$subscriptionConsumer->consume();

//清除队列消息
$queueName = 'rabbitmq';
$queue = $context->createQueue($queueName);
$context->purgeQueue($queue);

三:简单实现

1:发送消息

复制代码
//连接rabbitmq
$factory = new AmqpConnectionFactory([
    'host' => '192.168.6.88',
    'port' => '5672',
    'vhost' => '/',
    'user' => 'admin',
    'pass' => 'admin',
    'persisted' => false,
]);

$context = $factory->createContext();
//声明主题
$exchangeName = 'exchange';
$fooTopic = $context->createTopic($exchangeName);
$fooTopic->setType(AmqpTopic::TYPE_FANOUT);
$context->declareTopic($fooTopic);

//声明队列
$queueName = 'rabbitmq';
$fooQueue = $context->createQueue($queueName);
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
$context->declareQueue($fooQueue);

//将队列绑定到主题
$context->bind(new AmqpBind($fooTopic, $fooQueue));

//发送消息到队列
$message = $context->createMessage('Hello world!');

$context->createProducer()->send($fooQueue, $message);

2:消费消息

复制代码
$factory = new AmqpConnectionFactory([
    'host' => '192.168.6.88',
    'port' => '5672',
    'vhost' => '/',
    'user' => 'admin',
    'pass' => 'admin',
    'persisted' => false,
]);
$context = $factory->createContext();


$queueName = 'rabbitmq';
$fooQueue = $context->createQueue($queueName);



$fooConsumer = $context->createConsumer($fooQueue);

$subscriptionConsumer = $context->createSubscriptionConsumer();
$subscriptionConsumer->subscribe($fooConsumer, function(Message $message, Consumer $consumer) {
    // process message
    //业务代码
    $consumer->acknowledge($message);//ack应答,通知rabbitmq成功,删除对应任务
    // $consumer->reject($message);ack应答,通知rabbitmq失败,不删除对应任务

    return true;
});
$subscriptionConsumer->consume();
相关推荐
LawrenceLan21 小时前
Flutter 零基础入门(十一):空安全(Null Safety)基础
开发语言·flutter·dart
txinyu的博客21 小时前
解析业务层的key冲突问题
开发语言·c++·分布式
码不停蹄Zzz21 小时前
C语言第1章
c语言·开发语言
行者961 天前
Flutter跨平台开发在OpenHarmony上的评分组件实现与优化
开发语言·flutter·harmonyos·鸿蒙
阿蒙Amon1 天前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
SmartRadio1 天前
ESP32添加修改蓝牙名称和获取蓝牙连接状态的AT命令-完整UART BLE服务功能后的完整`main.c`代码
c语言·开发语言·c++·esp32·ble
且去填词1 天前
Go 语言的“反叛”——为什么少即是多?
开发语言·后端·面试·go
知乎的哥廷根数学学派1 天前
基于生成对抗U-Net混合架构的隧道衬砌缺陷地质雷达数据智能反演与成像方法(以模拟信号为例,Pytorch)
开发语言·人工智能·pytorch·python·深度学习·机器学习
yeziyfx1 天前
kotlin中 ?:的用法
android·开发语言·kotlin
charlie1145141911 天前
嵌入式的现代C++教程——constexpr与设计技巧
开发语言·c++·笔记·单片机·学习·算法·嵌入式