millerp/rabbitmq-service-provider

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

安装: 501

依赖者: 1

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 23

类型:silex-service-provider

2.0.1 2016-12-07 10:57 UTC

This package is auto-updated.

Last update: 2024-09-19 09:46:05 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 fiunchinho/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'     => '/'
        ],
        'another' => [
            'host'      => 'another_host',
            'port'      => 5672,
            'user'      => 'guest',
            'password'  => 'guest',
            'vhost'     => '/'
        ]
    ],
    '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'
        ]
    ]
]);

请注意,您在消费者中选择的回调必须是在 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 继承的新命令,并将它们添加到上面示例中。

致谢