wisembly / amqp-bundle
用于处理 Symfony2 应用中 amqp 的 SwarrotBundle 的替代方案
Requires
- php: ^7.1
- psr/log: ^1.0
- swarrot/swarrot: ^2.0 || ^3.0
- symfony/config: ^3.0 || ^4.0
- symfony/console: ^3.0 || ^4.0
- symfony/dependency-injection: ^3.3 || ^4.0
- symfony/event-dispatcher: ^3.0 || ^4.0
- symfony/http-kernel: ^3.0 || ^4.0
- symfony/options-resolver: ^3.3 || ^4.0
- symfony/process: ^3.4 || ^4.0
Requires (Dev)
- php-amqplib/php-amqplib: ^2.7
- phpunit/phpunit: ^6.0
Suggests
- odolbeau/rabbit-mq-admin-toolkit: Configuring automatically your queues, exchanges and bindings
- pecl-amqp: PHP AMQP extension
- videlalvaro/php-amqplib: If the pecl is not an option
Conflicts
- symfony/dependency-injection: < 3.3
This package is not auto-updated.
Last update: 2021-01-09 08:16:22 UTC
README
此扩展将 Swarrot 集成到 Symfony,采用了与 SwarrotBundle 不同的方法。目前处于开发初期...并将开源。
但是,与 SwarrotBundle 不同,此扩展不允许您(暂时还不允许)配置多个消费者,并强迫您使用 CommandProcessor
方法(每条消息都应该在 CommandProcessor
中处理,它将通过 Symfony Process
处理)。
安装
推荐的方式是通过 Composer。安装完成后,您应该运行 require 命令: composer require wisembly\amqp-bundle
,并选择 packagist 上可用的最新版本(您应避免使用 @stable
元约束)。注意,有一个 flex 食谱可用。 :}
配置参考
配置参考可以通过命令 app/console config:dump-reference WisemblyAmqpBundle
查找。
# Default configuration for extension with alias: "wisembly_amqp" wisembly_amqp: # Default connection to use default_connection: null # Broker to use broker: ~ # Required # Path to sf console binary console_path: ~ # Required # Logger channel to use when a logger is required logger_channel: amqp # Connections to AMQP to use connections: # Required # Prototype name: uri: null host: null port: null login: null password: null vhost: null query: null # Access gate for each dialog with AMQP gates: # Prototype name: # Does the queue and the exchange be declared before use them auto_declare: true # Connection to use with this gate connection: null # Routing key to use when sending messages through this gate routing_key: null # Queue to fetch the information from queue: # Required name: ~ # Required options: passive: false durable: true exclusive: false auto_delete: false arguments: # Prototype name: ~ # Exchange point associated to this gate exchange: # Required name: ~ # Required options: type: null passive: false durable: true auto_delete: false internal: false arguments: # Prototype name: ~
使用方法
"门"的概念
这个概念与 Swarrot Bundle 的最大区别在于“门”。它是一个简单的值对象,包含有关用于您操作的消息队列/交换机的信息(使用哪个连接,哪个队列应该是目标,使用哪个交换机,哪个路由键,...以及这些的配置,例如,如果交换机/队列不存在,是否应该声明它们等)。
有关“门”可以配置的内容,请参阅配置参考。一旦您至少有一个“门”,您就可以从一个“门”消费,或向一个“门”发布。有关详细信息,请参阅下文。
发布一条消息
要向“门”(接入点)发布一条新消息,您应检索服务 Wisembly\AmqpBundle\GatesBag
来获取正确的“门”,然后使用 Wisembly\AmqpBundle\Publisher
服务发布 CommandProcessor
可以理解的消息。
use Wisembly\AmqpBundle\Message; use Wisembly\AmqpBundle\Publisher; $message = new Message( 'symfony:command:to:run', [ 'list', 'of', 'arguments' 'and' 'options' ] ])); // Using swarrot's Swarrot\Broker\Message is also possible if you don't plan on // consuming the message through the provider consumer $publisher->publish($message, 'my_gate'); // or $publisher->publish($message, $gate); with `$gate` an instanceof Gate
您的消息现在准备好被消费者消费。但,如果您不想使用 AmqpBundle 的消费者,您不必强制使用相同的语法,可以使用您需要的任何东西!
消费一条消息
为了消费此扩展期望的消息,您只需运行以下命令
php app/console wisembly:amqp:consume gate
有许多选项可用(例如,激活 RPC 机制,定义轮询间隔等),但您可以在 --help
选项中查看所有这些选项。
每当消费者消费消息时,它将启动一个新的 Process
来处理它,并将环境变量和详细程度传递给运行的命令。然后,输出将被检索并打印在消费者的输出上。
实现代理
内置了两个代理
- 《Wisembly\AmqpBundle\Broker\PeclBroker》(php amqp 扩展)注意,如果 pecl 扩展未加载/安装,代理将不可用。
Wisembly\AmqpBundle\Broker\PhpAmqpLibBroker
,这是一个完全用 PHP 实现的
建议如果可用,使用 Wisembly\AmqpBundle\Broker\PeclBroker
代理。
但是,你可以通过实现 Wisembly\AmqpBundle\BrokerInterface
接口来创建更多的这些(例如 Redis 代理或其他任何!)。如果你使用 dic 3.4+ 的 autoconfigure
设置,那就足够了。如果你想添加别名或未使用 autoconfigure
功能,你可以添加一个 wisembly.amqp.broker
标签,该标签可以有一个别名(如果没有指定别名,它将使用服务名称)。
致谢
在 Wisembly 工厂用爱心开发,基于 SwarrotBundle。