channext/channext-rabbitmq
最新稳定版本:1.8.1
Composer 安装命令:
composer require channext/channext-rabbitmq
包简介
The RabbitMQ integration for Channext services
README 文档
README
A Laravel x RabbitMQ integration is a package to simplify the publishing and consuming of messages to and from a RabbitMQ server. This package expects you to use a topic exchange and bases its routing on the messages' routing keys.
Installation
- Run
composer require channext/channext-rabbitmqin the root of your project - Run
php artisan vendor:publishand choose the option "Provider: Channext\ChannextRabbitmq\Providers\RabbitMQServiceProvider" - If you want to use RabbitMQAuth facade and run
php artisan vendor:publishthen choose the option "Provider: Channext\ChannextRabbitmq\Providers\EventAuthServiceProvider" - Implement your logic inside EventAuthServiceProvider boot()
- If you want to use EventFail provider facade and run
php artisan vendor:publishthen choose the option "Provider: Channext\ChannextRabbitmq\Providers\EventFailServiceProvider" - Implement your logic inside EventFailServiceProvider boot()
- Add the correct RabbitMQ credentials to your .env file (see config/rabbitmq.php for the available variables. Also be sure to use different queue names for different services, otherwise only one service will act on the event other ones will miss it.)
- Run
php artisan config:cache - Run
php artisan cache:clear - Update the routes/topics.php file with the routes you want to listen to
- Run
php artisan rabbitmq:consumeto start consuming messages. Or you can usephp artisan rabbitmq:listento listen & consume every event in a separate app container. (This is more suitable for development purposes, but a bit slower.)
Examples
This package enables you to use a facade for executing simple publishing and consuming of RabbitMQ messages. To start using the facade simply import it in the class where you want to use it:
use Channext\ChannextRabbitmq\Facades\RabbitMQ;
To publish a message simply use the publish method in the RabbitMQ facade:
RabbitMQ::publish($body, $routingKey);
Where the body parameter is the data that you want to send and the routing key is the route that consumers can listen to. E.g. the moment a user is created the body should contain all the user data and the routing key could be "user.created".
To start consuming messages you should update the routes/topics.php file with all the routing keys you want your service to listen to. This is actually very similar to the way Laravel usually handles routing:
RabbitMQ::route('user.login', [Controller::class, 'test'], retry: false);
This route would only listen to messages with a routing key "user.login" and would execute the "test"-method in the controller.
The functions in the controllers called by the router should always accept the message as the first parameter:
public function test(AMQPMessage $message) { $messageBody = $message->all(); }
统计信息
- 总下载量: 8.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2023-09-29