sunspikes/carrot
Composer 安装命令:
composer require sunspikes/carrot
包简介
A simple abstraction for RabbitMQ
README 文档
README
A simple abstraction for RabbitMQ.
Carrot aims to make it easy to get started with RabbitMQ and PHP, at the same time maintaining the flexibility to implement all the supported messaging patterns.
Requirements
You must have a rabbitmq server running to use this package. For more on this refer RabbitMQ documentation.
Installation
With Composer
$ composer require sunspikes/carrot
Usage
To configure a RabbitMQ connection
Carrot::config([
'host' => '127.0.0.1',
'port' => 5672,
'username' => 'guest',
'password' => 'guest',
'vhost' => '/'
]);
To publish message to RabbitMQ,
// Create producer which sends messages to 'test-exchange'
$producer = Carrot::producer('test-exchange');
// Send a message to 'TestQueue'
$producer->send('TestQueue', [
'text' => 'hello',
]);
To Consume messages from RabbitMQ
// Create a consumer which reads from 'test-exchange'
$consumer = Carrot::consumer('test-exchange');
// Add a listener for for queue 'TestQueue', the callback will be executed
// everytime there is a new message on this queue, which will print 'hello'
$consumer->add('TestQueue', function($message) {
print $message->text;
});
// Listen for messages on 'test-exchange', this is a wait loop which will keep the
// consumer running till it's manually terminated or the connection is lost
$consumer->listen('test-exchange');
More on usage
You don't have to use static methods to build and use producers and consumers, also the library makes it easy to switch between different exchange types.
For example, you can also create a direct exchange & produce messages to it like,
$config = [
'host' => '127.0.0.1',
'port' => 5672,
'username' => 'guest',
'password' => 'guest',
'vhost' => '/'
];
$carrot = new Carrot('test-exchange', 'direct', $config);
$producer = $carrot->getProducer();
$producer->send('TestQueue', [
'text' => 'hello'
]);
Also the consumer can be created and listened to a direct exchange for messages like,
$carrot = new Carrot('test-exchange', 'direct', $config);
$consumer = $carrot->getConsumer();
$consumer->add('TestQueue', function($message) {
print $message->text;
});
$consumer->listen('test-exchange');
By default carrot producer will automatically serialize the message and consumer will deserialize the message acknowledge the incoming messages, you could disable this delegation by setting the configuration parameter delegate to false
// make some message
$message = ['text' => 'hello'];
// serialize it manually
$message = json_encode($message);
// send the message
$producer->send('TestQueue', $message);
// At consumer end, handle the message manually
$consumer->add('TestQueue', function (AMQPMessage $message) use ($consumer) {
$decoded = json_decode($message->body);
//... do something with $decoded->text
// if everything went fine acknowledge message
$consumer->acknowledgeMessage($message);
// if something went wrong with the message reject message, this will discard the message (optionally requeue the message)
$consumer->rejectMessage($message, $requeue = false);
});
Example
You can run the examples from the /example folder
First run the consumer, it will create the exchange and queue on the rabbitmq server
$ php example/consumer.php
[*] Consumer starting to listen for messages from rabbitmq...
[*] Received message: hello
As soon as you run the producer, the consumer will print the message text sent by the producer
$ php example/producer.php
[*] Producer started...
[*] Producer sent message to rabbitmq
Testing
Run phpunit
Author
Krishnaprasad MG [@sunspikes]
Contributing
Please feel free to send pull requests.
License
This is an open-sourced software licensed under the MIT license.
sunspikes/carrot 适用场景与选型建议
sunspikes/carrot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 08 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sunspikes/carrot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sunspikes/carrot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-08-03