vip域名做网站好不好,云南云桥建设股份有限公司官方网站,注册网站商标多少钱,求职招聘网站建设投标书基于php-amqplib/php-amqplib组件适配laravel框架的amqp封装库
支持便捷可配置的队列工作模式 官网详情
在此基础上可支持延迟消息、死信队列等机制。
环境要求#xff1a;
PHP版本: ^7.3|^8.0
需要开启的扩展: socket
其他:
如果需要实现延迟任务需要安装对应版本的ra…基于php-amqplib/php-amqplib组件适配laravel框架的amqp封装库
支持便捷可配置的队列工作模式 官网详情
在此基础上可支持延迟消息、死信队列等机制。
环境要求
PHP版本: ^7.3|^8.0
需要开启的扩展: socket
其他:
如果需要实现延迟任务需要安装对应版本的rabbitmq延迟插件以rabbitmq3.9.0版本为例:
wget https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/3.9.0/rabbitmq_delayed_message_exchange-3.9.0.ez
cp rabbitmq_delayed_message_exchange-3.9.0.ez /opt/rabbitmq/plugins/
rabbitmq-plugins enable rabbitmq_delayed_message_exchange用法
第一步 安装组件
composer require sai97/laravel-amqp第二步 发布服务以及配置
php artisan vendor:publish --providerSai97\LaravelAmqp\AmqpQueueProviders执行完后会分别在app/config目录下生成amqp.php(amqp连接配置等)、app/QueueJob/DefaultQueueJob.php(默认队列任务)
amqp.php
?phpuse App\QueueJob\DefaultQueueJob;return [connection [default [host env(AMQP_HOST, 127.0.0.1),port env(AMQP_PORT, 5672),user env(AMQP_USER, root),password env(AMQP_PASSWORD, root)]],event [default DefaultQueueJob::class,]
];
connection为amqp连接配置可根据自身业务去调整完全对应php-amqplib/php-amqplib相关配置项 event是队列实例标识最好和connection用相同的key以便管理。
目前可支持相关接口项 //获取连接名称public function getConnectName(): string;//获取交换机名称public function getExchangeName(): string;//获取交换机类型public function getExchangeType(): string;//获取队列名称public function getQueueName(): string;//获取路由KEYpublic function getRoutingKey(): string;//获取ContentTypepublic function getContentType(): string;//是否开启死信模式public function isDeadLetter(): bool;//获取死信交换机名称public function getDeadLetterExchangeName(): string;//获取死信路由KEYpublic function getDeadLetterRoutingKey(): string;//获取死信队列名称public function getDeadLetterQueueName(): string;//是否开启延迟任务public function isDelay(): bool;//获取延迟任务过期时长public function getDelayTTL(): int;//获取队列附加参数public function getQueueArgs(): array;//获取回调函数public function getCallback(): callable;//是否自动提交ACKpublic function isAutoAck(): bool;
当然你也可以自定义队列实例只要继承Sai97\LaravelAmqp\Queue基类即可具体功能配置参数参考Sai97\LaravelAmqp\QueueInterface。
代码示例:
生产者:
$message This is message...;
$amqpQueueServices new AmqpQueueServices(QueueFactory::getInstance(DefaultQueueJob::class));
$amqpQueueServices-producer($message);
消费者:
利用laravel自带的Command去定义一个RabbitMQWorker自定义命令行仅需要定义一次后续只需要更改amqp.php配置文件添加不同的队列实例绑定关系即可以下是RabbitMQWorker演示代码
?phpnamespace App\Console\Commands;use Illuminate\Console\Command;
use Sai97\LaravelAmqp\AmqpQueueServices;
use Sai97\LaravelAmqp\QueueFactory;class RabbitMQWorker extends Command
{/*** The name and signature of the console command.** var string*/protected $signature rabbitmq:worker {event};/*** The console command description.** var string*/protected $description rabbitmq worker 消费进程;/*** Create a new command instance.** return void*/public function __construct(){parent::__construct();}/*** Execute the console command.** return mixed*/public function handle(){try {$event $this-argument(event);$eventConfig config(amqp.event);if (!isset($eventConfig[$event]) || empty($entity $eventConfig[$event])) {return $this-error(未知的事件: {$event});}$this-info(rabbitmq worker of event[{$event}] process start ...);$amqpQueueServices new AmqpQueueServices(QueueFactory::getInstance($entity));$amqpQueueServices-consumer();} catch (\Throwable $throwable) {$event $event ?? ;$this-error($throwable-getFile() . [{$throwable-getLine()}]);return $this-error(rabbitmq worker of event[{$event}] process error:{$throwable-getMessage()});}$this-info(rabbitmq worker of event[{$event}] process stop ...);}
}
完成RabbitMQWorker消费者命令后我们只需执行php artisan rabbitmq:worker default 完成监听其中default是可变的请根据的amqp.php配置中的队列实例绑定标识去输入。
因为队列的消费者都需要是守护进程所以我们可以依托supervisord进程管理器去定义RabbitMQWorker消费者命令这样可以保证进程可后台允许以及重启启动等以下是supervisord.conf配置文件示例
[program:rabbitmq-worker-default]
#process_name%(program_name)s_%(process_num)d
process_nameworker_%(process_num)d
numprocs3
command/usr/local/bin/php /app/www/laravel8/artisan rabbitmq:worker default
autostarttrue
autorestarttrue
startretries3
priority3
stdout_logfile/var/log/rabbitmq-worker-default.log
redirect_stderrtrue搭配supervisord来进行管理消费者进程有许多便捷的方面
如果需要新增一个队列实例只需要按照上述格式复制一个program可以在不影响其他进程的情况下进程更新supervisord配置
supervisorctl update2. 通过配置numprocs参数来设定需要开启多少个相同配置项的消费者worker这在任务分发、并行处理等场景十分适用大大提高消费者执行效率。
这里不详细叙述supervisord相关操作具体可查看supervisord官方文档。 参考链接https://github.com/Z-Sai/laravel-amqp 文章转载自: http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.rykgh.cn.gov.cn.rykgh.cn http://www.morning.pqnpd.cn.gov.cn.pqnpd.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.pzrrq.cn.gov.cn.pzrrq.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.hmlpn.cn.gov.cn.hmlpn.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.hwtb.cn.gov.cn.hwtb.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.sgbjh.cn.gov.cn.sgbjh.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.dhyzr.cn.gov.cn.dhyzr.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.kxrhj.cn.gov.cn.kxrhj.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.wxfgg.cn.gov.cn.wxfgg.cn http://www.morning.gbfck.cn.gov.cn.gbfck.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.zcsch.cn.gov.cn.zcsch.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.xnkh.cn.gov.cn.xnkh.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.srrrz.cn.gov.cn.srrrz.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.nbnpb.cn.gov.cn.nbnpb.cn http://www.morning.mmzfl.cn.gov.cn.mmzfl.cn http://www.morning.gcqkb.cn.gov.cn.gcqkb.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.qmnhw.cn.gov.cn.qmnhw.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.xhgcr.cn.gov.cn.xhgcr.cn