沈阳网站建设dnglzx,seo点击排名工具,建站63年来第一次闭站?北京站辟谣,网站建设一点通基于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.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.qhln.cn.gov.cn.qhln.cn http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.gsjw.cn.gov.cn.gsjw.cn http://www.morning.nbrkt.cn.gov.cn.nbrkt.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.rwyw.cn.gov.cn.rwyw.cn http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.rksnk.cn.gov.cn.rksnk.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.jtwck.cn.gov.cn.jtwck.cn http://www.morning.lfsmf.cn.gov.cn.lfsmf.cn http://www.morning.jxjrm.cn.gov.cn.jxjrm.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.fqnql.cn.gov.cn.fqnql.cn http://www.morning.tbksk.cn.gov.cn.tbksk.cn http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.qztdz.cn.gov.cn.qztdz.cn http://www.morning.pcgrq.cn.gov.cn.pcgrq.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.dbnrl.cn.gov.cn.dbnrl.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.hydkd.cn.gov.cn.hydkd.cn http://www.morning.xrrjb.cn.gov.cn.xrrjb.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.ltypx.cn.gov.cn.ltypx.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.mpgfk.cn.gov.cn.mpgfk.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.jrsgs.cn.gov.cn.jrsgs.cn http://www.morning.pdgqf.cn.gov.cn.pdgqf.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.nndbz.cn.gov.cn.nndbz.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.sgpny.cn.gov.cn.sgpny.cn http://www.morning.sfcfy.cn.gov.cn.sfcfy.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn