e-sixt / rabbitmq-service-provider
Silex RabbitMQ服务提供者。将php-amqplib/rabbitmq-bundle集成到Silex中。
Requires
- php: >=5.5.0
- ivoba/console-service-provider: ~2.0
- php-amqplib/rabbitmq-bundle: ~1.9
- silex/silex: ~1.3
Requires (Dev)
- phpunit/phpunit: ~4.8
Replaces
This package is not auto-updated.
Last update: 2022-02-01 12:57:39 UTC
README
请使用https://github.com/williamespindola/rabbitmq-service-provider代替!
RabbitMq Service Provider for Silex
关于
此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继承并添加它们来创建新命令,如上面所示。
鸣谢
- RabbitMqBundle 扩展包,最初由 Alvaro Videla 开发
- rabbitmq-service-provider 由 fiunchinho 开发