williamespindola/rabbitmq-service-provider

Silex 服务提供商用于 RabbitMQ。将 php-amqplib/rabbitmq-bundle 集成到 Silex。

2.3.0 2017-10-03 12:56 UTC

README

关于

此 Silex 服务提供商将令人惊叹的 RabbitMqBundle 集成到您的 Silex 应用程序中。通过安装由 Alvaro Videla 创建的此捆绑包,您可以使用 RabbitMQ 消息功能,在您的应用程序中使用 php-amqplib 库。

这是 fiunchinho 的 rabbitmq-service-provider 的分支,原始维护者不再维护。原始命名空间已保留,以便于升级。

安装此服务提供商后,从控制器发送消息可能是这样的

$app->post('/message', function(Request $request) use ($app){
    $producer = $app['rabbit.producer']['my_exchange_name'];
    $producer->publish('Some message');

    return new Response($msg_body);
});

稍后,当您想从名为 'my_queue' 的队列中消费 50 条消息时,只需在 CLI 上运行即可

$ ./app/console rabbitmq:consumer -m 50 my_queue

要了解您可以使用此捆绑包做什么,请阅读捆绑包的 README

安装

使用 Composer 需求库

$ composer require williamespindola/rabbitmq-service-provider

然后,要激活服务,在创建您的 Silex 应用程序后注册服务提供商

use Silex\Application;
use fiunchinho\Silex\Provider\RabbitServiceProvider;

$app = new Application();
$app->register(new RabbitServiceProvider());

开始发送消息 ;)

用法

在 Symfony 捆绑包的 README 文件中,您可以查看所有可用的选项。例如,要使用两个不同的连接和一对生产者和一个消费者配置我们的服务,我们将传递以下配置

$app->register(new RabbitServiceProvider(), [
    'rabbit.connections' => [
        'default' => [
            'host'                  => 'localhost',
            'port'                  => 5672,
            'user'                  => 'guest',
            'password'              => 'guest',
            'vhost'                 => '/',
            'lazy'                  => false,
            'connection_timeout'    => 3,
            'read_write_timeout'    => 6,
            'keepalive'             => false,
            'heartbeat'             => 3
        ],
        'another' => [
            'host'                  => 'another_host',
            'port'                  => 5672,
            'user'                  => 'guest',
            'password'              => 'guest',
            'vhost'                 => '/'
            'lazy'                  => false,
            'connection_timeout'    => 3,
            'read_write_timeout'    => 6,
            'keepalive'             => false,
            'heartbeat'             => 3
        ]
    ],
    'rabbit.producers' => [
        'first_producer' => [
            'connection'        => 'another',
            'exchange_options'  => ['name' => 'a_exchange', 'type' => 'topic']
        ],
        'second_producer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'a_exchange', 'type' => 'topic']
        ],
    ],
    'rabbit.consumers' => [
        'a_consumer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'a_exchange','type' => 'topic'],
            'queue_options'     => ['name' => 'a_queue', 'routing_keys' => ['foo.#']],
            'callback'          => 'your_consumer_service',
            'graceful_max_execution' => [
                'timeout' => 900,
                'exit_code' => 10,
            ],
        ]
    ]
]);

请注意,消费者中选择的回调需要是一个已在 Pimple 容器中注册的服务。消费者服务实现了 ConsumerInterface,该接口有一个公共的 execute() 方法。

命令行中的消费者

我们建议您使用 Consumer 命令从队列中消费消息。要使用此命令,只需为控制台创建可执行文件(如在任何控制台应用程序中)

#!/usr/bin/env php
<?php

require_once 'vendor/autoload.php';

use Silex\Application;
use fiunchinho\Silex\Provider\RabbitServiceProvider;
use fiunchinho\Silex\Command\Consumer;
use Ivoba\Silex\Provider\ConsoleServiceProvider;

$app = new Application();
require __DIR__.'/config/dev.php';

$app->register(new RabbitServiceProvider(), array(
    'rabbit.consumers' => [
        'my_consumer' => [
            'connection'        => 'default',
            'exchange_options'  => ['name' => 'my_exchange_name','type' => 'topic'],
            'queue_options'     => ['name' => 'a_queue', 'routing_keys' => ['foo.#']],
            'callback'          => 'my_service'
        ]
    ]
));

$app->register(new ConsoleServiceProvider(), array(
    'console.name'              => 'MyApplication',
    'console.version'           => '1.0.0',
    'console.project_directory' => __DIR__
));

$application = $app['console'];
$application->add(new Consumer());
$application->run();

在此示例中,我们依赖于 Ivoba\Silex\Provider\ConsoleServiceProvider 来简化操作,因此您还需要安装它。您可以通过从示例 Consumer 继承并添加它们来创建新的命令,如上面的示例所示。

鸣谢