semako / rabbitmq-service-provider
Silex 服务提供程序,用于 RabbitMQ
1.0.2
2017-04-06 15:08 UTC
Requires
- php: >=5.4.0
- knplabs/console-service-provider: ~1.0
- oldsound/rabbitmq-bundle: ~1.5
- silex/silex: 2.0.4
Requires (Dev)
- phpunit/phpunit: ~4.2
This package is auto-updated.
Last update: 2024-09-07 22:45:48 UTC
README
关于
此 Silex 服务提供程序将惊人的 RabbitMqBundle 集成到您的 Silex 应用程序中。安装由 Alvaro Videla 创建的这个包后,您可以使用 RabbitMQ 消息功能,并通过 php-amqplib 库来使用您的应用程序。
安装此服务提供程序后,从控制器发送消息可能会像这样
$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 Knp\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();
我们依靠 Knp\Provider\ConsoleServiceProvider 来简化操作,因此您还需要注册它。您可以通过从示例 Consumer 继承并添加它们来创建新的命令,就像上面示例中那样。