riesenia / cakephp-rabbitmq
CakePHP 的 RabbitMQ 插件
Requires
- cakephp/cakephp: ~3.0
- php-amqplib/php-amqplib: ~2.0
Requires (Dev)
- phpunit/phpunit: ~6.0
- rshop/php-cs-fixer-config: ~1.0
This package is auto-updated.
Last update: 2024-09-09 13:49:58 UTC
README
本插件适用于 CakePHP 3.x,简化了在 CakePHP 应用程序中使用 RabbitMQ。
安装
使用 composer
composer require riesenia/cakephp-rabbitmq
在 config/bootstrap.php 中加载插件
Plugin::load('RabbitMQ');
使用方法
RabbitMQ 带有一个内置的 shell,它监听定义的队列并将消息转发到配置中指定的回调。
要启动服务器,请运行
bin/cake rabbitmq
要仅监听指定的队列,请传递它们的别名作为参数
bin/cake rabbitmq server email sms
发送
要向队列发送消息,只需使用 send
方法
use RabbitMQ\CakephpRabbitMQ as MQ; MQ::send('email', 'this is a message');
监听
如果您想在您的自己的 shell 中运行服务器,请使用 listen
方法
use RabbitMQ\CakephpRabbitMQ as MQ; // this will listen to all queues defined in the configuration file MQ::listen(); // this will listen only to passed queues MQ::listen(['email']);
配置
示例配置(例如,在您的 config/app.php 中)
'Riesenia.CakephpRabbitMQ' => [ 'server' => [ 'host' => '127.0.0.1', 'port' => 5672, 'user' => 'guest', 'password' => 'guest' ], 'email' => [ 'cake_command' => 'email send', 'retry_time' => 15 * 60 * 1000, 'retry_max' => 3 ] ]
配置中的每个键都是特定队列的别名。键 server
保留用于定义 RabbitMQ 连接。
基本配置键
以下只是基本配置键。有关完整配置,请参阅下面的部分。
retry
(bool) - 如果操作失败则重试retry_time
(int) - 重试周期(以毫秒为单位)retry_max
(int) - 最大重试次数
回调
有三种可用的回调类型:callback
、command
和 cake_command
。**请指定一种类型的回调!** 如果启用了重试,则 **回调必须返回一个状态码** 来指示进程是否成功。**成功时返回 0**,任何其他数字表示失败。**对于 cake shell 方法,返回 true 表示成功,否则返回 false**。
command
(string)
这将执行一个定义的命令。例如,以下配置
'command' => 'rm'
将执行 rm <message>
命令。
cake_command
(string)
这是 bin/cake 命令的缩写。例如,以下配置
'cake_command' => 'email send'
将执行 bin/cake email send <message>
命令。
callback
(callable)
这将调用回调函数。例如,以下配置
'callback' => [new MyMailer() ,'sendEmail']
将在 MyMailer
对象上调用 sendEmail($message)
。请注意,**回调函数将接收原始 AMQPMessage**。您可以通过 $message->body
访问发送的消息。有关 PHP 可调用类型的更多信息,请参阅 PHP 文档。
完整配置键
以下是所有配置键的默认值。有关每个配置键的详细信息,请参阅 RabbitMQ 文档。
server
'server' => [ 'host' => 'localhost', 'port' => 5672, 'user' => 'guest', 'password' => 'guest', 'vhost' => '/', 'insist' => false, 'login_method' => 'AMQPLAIN', 'login_response' => null, 'locale' => 'en_US', 'connection_timeout' => 3.0, 'read_write_timeout' => 3.0, 'context' => null, 'keepalive' => false, 'heartbeat' => 0 ];
queue
'<alias>' => [ // Main queue 'exchange' => [ 'name' => '<alias>_exchange', 'type' => 'direct', 'passive' => false, 'durable' => false, 'auto-delete' => false, 'internal' => false, 'no-wait' => false, 'arguments' => [] ], 'queue' => [ 'name' => '<alias>_queue', 'passive' => false, 'durable' => true, 'exclusive' => false, 'auto-delete' => false, 'no-wait' => false, 'arguments' => [] ], 'routing_key' => '<alias>_routing_key', // Retry setting 'retry' => true, 'retry_time' => 5 * 60 * 1000, // 5 mins 'retry_max' => 5, // Retry queue 'retry_exchange' => [ 'name' => '<alias>_retry_exchange', 'type' => 'direct', 'passive' => false, 'durable' => false, 'auto-delete' => false, 'internal' => false, 'no-wait' => false, 'arguments' => [] ], 'retry_queue' => [ 'name' => '<alias>_retry_queue', 'passive' => false, 'durable' => true, 'exclusive' => false, 'auto-delete' => false, 'no-wait' => false, 'arguments' => [] ], 'retry_routing_key' => '<alias>_retry_routing_key', // Basic qos 'basic_qos' => [ 'prefetch-size' => null, 'prefetch-count' => 1, 'global' => null ], // Basic consume 'basic_consume' => [ 'consumer-tag' => '', 'no-local' => false, 'no-ack' => false, 'exclusive' => false, 'no-wait' => false ] ]
注意:除 retry_max
之外,配置在第一次运行后不能更改,除非重置队列。
运行以下命令以重置队列
rabbitmqctl stop_app && rabbitmqctl reset && rabbitmqctl start_app
警告:这将删除 rabbitmq 中的所有消息