guandeng / laravel-rabbitmq
Laravel RabbitMQ
v1.0.2
2022-07-08 02:19 UTC
Requires
- php: ^7.1
- ext-json: *
- illuminate/queue: 5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|9.*
- illuminate/support: 5.6.*|5.7.*|5.8.*|6.*|7.*|8.*|9.*
- php-amqplib/php-amqplib: ^2.9
This package is auto-updated.
Last update: 2025-03-08 08:11:10 UTC
README
功能列表
- 生产消息
- 消费消息
- 支持队列持久
- 支持消息持久化
- 支持交换机持久化
- 支持生产消息确认机制
- 支持消费消息确认机制
- 支持消息公平调度
- 支持多个工作队列
- 支持死信队列
- 支持延迟队列
- 支持优先级队列
- 支持集群配置
- 停止指定消费
安装
composer require guandeng/laravel-rabbitmq
.env配置
RABBITMQ_HOST=127.0.0.1
RABBITMQ_PORT=5672
RABBITMQ_USER=guest
RABBITMQ_PASS=guest
RABBITMQ_VHOST=/
rabbitmq.php配置
可参考rabbitmq_example.php
交换机配置
exchanges' => [
'DirectExchange' => [
'name' => 'my.first.directexchange',
'attributes' => [
'exchange_type' => 'direct',
'binds' => [
[
'routing_key' => 'direct_route_key',
],
],
'arguments' => [
'x-dead-letter-exchange' => 'my.first.dead.directexchange',
'x-message-ttl' => 5000, //消息存活时间,单位毫秒
'x-dead-letter-routing-key' => 'dead_direct_route_key',
],
'message' => [
'delivery_mode' => 2,
'priority' => 10,
],
],
]
]
队列配置
'queues' => [
'DirectQueue' => [
'name' => 'my.first.directqueue',
'attributes' => [
'binds' => [
[
'exchange' => 'DirectExchange',
'routing_key' => 'direct_route_key',
],
],
],
],
]
生产者配置
'publishers' => [
'DirectPublisher' => 'DirectExchange',
],
消费者配置
'DirectConsumer' => [
'queue' => 'DirectQueue',
'prefetch_count' => 1,
'handlers' => ["\\App\\QueueHandlers\\MyHandler"],
],
生产消息
命令行,指定配置生产消息
php artisan publisher:rabbitmq DirectPublisher test
伪代码
public function publish()
{
// 向所有生产者发送消息
$publishers = config('rabbitmq.publishers');
foreach ($publishers as $publisher => $exchange) {
$messages = [
'key' => 'hello world'
];
app('rabbitmq')->exchange($exchange)->publish($messages);
}
}
消费消息
指定rabbitmq.php消费者配置
php artisan consumer:rabbitmq DirectConsumer
处理消息在消费者配置handlers指定回调类中 具体使用参考Handlers下的DefaultHandler.php