xerxes/laravel-rabbitmq-communication
Composer 安装命令:
composer require xerxes/laravel-rabbitmq-communication
包简介
A Laravel package for RabbitMQ messaging with outbox pattern, dead letter queues, and automatic retry mechanisms
README 文档
README
A Laravel package for RabbitMQ messaging with support for the outbox pattern, event consumers, dead letter queues, and automatic retry mechanisms.
Features
- Direct Publish with Outbox Fallback - Messages are published directly; if RabbitMQ is unavailable, they're stored for later retry
- Event Consumers - Consume messages from multiple queues with configurable handlers
- Dead Letter Queue (DLQ) - Automatic retry with exponential backoff, failed messages go to DLQ
- Type-Safe Enums - PHP 8.1+ enums for status and mode handling
- Fully Configurable - Environment-based configuration for all settings
Requirements
- PHP 8.1+
- Laravel 9, 10, 11, or 12
- RabbitMQ Server
- ext-json
Installation
composer require xerxes/laravel-rabbitmq-communication
Publish the configuration:
php artisan vendor:publish --tag=laravel-rabbitmq-communication-config
Run migrations:
php artisan migrate
Configuration
Environment Variables
# Connection Settings RABBITMQ_HOST=127.0.0.1 RABBITMQ_PORT=5672 RABBITMQ_USER=guest RABBITMQ_PASS=guest RABBITMQ_VHOST=/ # Outbox Pattern (always enabled as fallback) RABBITMQ_OUTBOX_CONNECTION=outbox # Dead Letter Queue RABBITMQ_DLQ_ENABLED=true RABBITMQ_DLQ_EXCHANGE=dlx.failed RABBITMQ_DLQ_MAX_RETRIES=3 # Consumer Settings RABBITMQ_CONSUMER_MODE=sync RABBITMQ_CONSUMER_PREFETCH=10 RABBITMQ_LOG_CHANNEL=stack
Publishing Messages
Basic Publishing
use Xerxes\RabbitMQ\RabbitMQ; app(RabbitMQ::class) ->message() ->viaExchange('my.exchange') ->route('my.routing.key') ->withPayload(['event' => 'user.created', 'data' => [...]]) ->persistent() ->publish();
Publishing with Headers
app(RabbitMQ::class) ->message() ->viaExchange('my.exchange') ->route('my.routing.key') ->withPayload($payload) ->withHeaders(['x-custom-header' => 'value']) ->persistent() ->publish();
Outbox Pattern (Guaranteed Delivery)
The outbox pattern is always enabled as an automatic fallback and cannot be disabled. Messages follow this flow:
- Try direct publish - Message is sent immediately to RabbitMQ
- On failure - Message is automatically stored in the database outbox table
- Outbox worker - Processes pending messages and retries publishing
app(RabbitMQ::class) ->message() ->viaExchange('my.exchange') ->route('my.routing.key') ->withPayload($payload) ->publish();
Process outbox messages:
php artisan outbox:work
Using Laravel Events
Implement the ShouldPublish interface to automatically publish events:
use Xerxes\RabbitMQ\Support\ShouldPublish; class UserCreated implements ShouldPublish { public string $exchange = 'user.events'; public function __construct( public User $user ) {} public function routingKey(): string { return 'user.created'; } } // Dispatch normally - automatically published to RabbitMQ event(new UserCreated($user));
Consuming Messages
Configuration
Define event consumers in config/rabbitmq.php:
'event-consumers' => [ [ 'exchange' => 'user.events', 'exchange_type' => 'topic', 'routing_key' => 'user.#', 'handler' => [\App\Handlers\UserHandler::class, 'handle'], 'queue' => 'my-app.users', ], ],
Handler Class
Your handler should implement the MessageHandler interface:
use Xerxes\RabbitMQ\Contracts\MessageHandler; class UserHandler implements MessageHandler { public function handle(array $payload, ?string $routingKey = null): void { // Process the message Log::info('Processing user event', [ 'routing_key' => $routingKey, 'payload' => $payload, ]); } }
Running the Consumer
php artisan rabbitmq:consume-events
Consumer Modes
Configure how consumed events are dispatched:
- sync: Events fired synchronously. Errors stop the consumer (unless DLQ is enabled).
- kind-sync: Events fired synchronously. Errors are logged but don't stop the consumer.
- job: Events dispatched via Laravel queue jobs.
Message Acknowledgment
Message acknowledgment is always required and cannot be disabled:
- On success: Message is acknowledged and removed from the queue
- On failure: Message is rejected and requeued for retry
- With DLQ enabled: After max retries, message is moved to the dead letter queue
Dead Letter Queue (DLQ)
How It Works
- Message fails processing
- Republished to retry queue with delay
- After TTL, routed back to original queue
- After max retries (default: 3), moved to dead letter queue
- Failed messages can be manually reprocessed
Retry Flow
Message fails → Retry Queue (5s delay) → Original Queue
↓ fails again
Retry Queue (30s delay) → Original Queue
↓ fails again
Retry Queue (2min delay) → Original Queue
↓ fails again (max retries exceeded)
Dead Letter Queue (.dlq)
Reprocessing Failed Messages
# Reprocess up to 100 messages php artisan rabbitmq:reprocess-dlq my-app.orders # Limit the number php artisan rabbitmq:reprocess-dlq my-app.orders --limit=50 # Dry run php artisan rabbitmq:reprocess-dlq my-app.orders --dry-run
Available Commands
| Command | Description |
|---|---|
rabbitmq:consume-events |
Start consuming messages from configured queues |
outbox:work |
Run outbox worker (continuous) |
outbox:process |
Process outbox messages once |
rabbitmq:declare-exchanges |
Declare configured exchanges |
rabbitmq:test-connection |
Test RabbitMQ connection |
rabbitmq:reprocess-dlq {queue} |
Reprocess messages from dead letter queue |
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
xerxes/laravel-rabbitmq-communication 适用场景与选型建议
xerxes/laravel-rabbitmq-communication 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rabbitmq」 「messaging」 「laravel」 「event-driven」 「outbox-pattern」 「dead-letter-queue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xerxes/laravel-rabbitmq-communication 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xerxes/laravel-rabbitmq-communication 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xerxes/laravel-rabbitmq-communication 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Work with RabbitMQ in Laravel.
Microservice RPC through message queues.
PHP AMQP Binding Library
PHP SDK for Mobile Message SMS API
Azure service bus Transport
Official PHP client for NATS messaging system
统计信息
- 总下载量: 97
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-15