sunspikes/carrot

简单的 RabbitMQ 抽象

v0.3.1 2016-08-07 22:04 UTC

This package is auto-updated.

Last update: 2024-08-29 04:25:55 UTC


README

SensioLabsInsight Scrutinizer Code Quality Code Coverage Code Climate Build Status Latest Stable Version License

简单的 RabbitMQ 抽象。

Carrot 致力于使 RabbitMQ 和 PHP 的入门变得简单,同时保持实施所有支持的消息模式的灵活性。

要求

您必须有一个运行中的 rabbitmq 服务器才能使用此包。有关更多信息,请参阅 RabbitMQ 文档

安装

使用 Composer

$ composer require sunspikes/carrot

使用方法

配置 RabbitMQ 连接

Carrot::config([  
    'host' => '127.0.0.1',
    'port' => 5672,
    'username' => 'guest',
    'password' => 'guest',
    'vhost' => '/'
]);

向 RabbitMQ 发送消息

// Create producer which sends messages to 'test-exchange'
$producer = Carrot::producer('test-exchange');

// Send a message to 'TestQueue'
$producer->send('TestQueue', [
    'text' => 'hello',
]);

从 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');

更多使用方法

您不必使用静态方法来构建和使用生产者和消费者,该库还简化了在不同交换类型之间的切换。

例如,您还可以创建一个直接交换并向其发送消息,如:

$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'
]);

消费者也可以创建并监听直接交换以接收消息,如:

$carrot = new Carrot('test-exchange', 'direct', $config);
$consumer = $carrot->getConsumer();

$consumer->add('TestQueue', function($message) {
    print $message->text;
});

$consumer->listen('test-exchange');

默认情况下,Carrot 生产者将自动序列化消息,消费者将反序列化消息并确认接收到的消息,您可以通过将配置参数 delegate 设置为 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 文件夹运行示例

首先运行消费者,它将在 rabbitmq 服务器上创建交换机和队列

$ php example/consumer.php

[*] Consumer starting to listen for messages from rabbitmq...
[*] Received message: hello

一旦运行生产者,消费者将打印出生产者发送的消息文本

$ php example/producer.php

[*] Producer started...
[*] Producer sent message to rabbitmq

测试

运行 phpunit

作者

Krishnaprasad MG [@sunspikes]

贡献

请随时发送 pull 请求。

许可

这是一个开源软件,根据 MIT 许可 许可。