wisembly/amqp-bundle

此包已被废弃且不再维护。未建议替代包。

用于处理 Symfony2 应用中 amqp 的 SwarrotBundle 的替代方案

安装数: 4,188

依赖项: 0

建议者: 0

安全: 0

星标: 3

关注者: 5

分支: 3

开放问题: 4

类型:symfony-bundle

v2.1.7 2018-05-30 15:10 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 代理。

但是,你可以通过实现 Wisembly\AmqpBundle\BrokerInterface 接口来创建更多的这些(例如 Redis 代理或其他任何!)。如果你使用 dic 3.4+ 的 autoconfigure 设置,那就足够了。如果你想添加别名或未使用 autoconfigure 功能,你可以添加一个 wisembly.amqp.broker 标签,该标签可以有一个别名(如果没有指定别名,它将使用服务名称)。

致谢

在 Wisembly 工厂用爱心开发,基于 SwarrotBundle