swarrot/swarrot-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

swarrot/swarrot-bundle

Composer 安装命令:

composer require swarrot/swarrot-bundle

包简介

SwarrotBundle

README 文档

README

Latest Stable Version Latest Unstable Version

A bundle to use Swarrot inside your Symfony application.

Installation

The recommended way to install this bundle is through Composer. Just run:

composer require swarrot/swarrot-bundle

Register the bundle in the kernel of your application:

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Swarrot\SwarrotBundle\SwarrotBundle(),
    );

    return $bundles;
}

Configuration reference

swarrot:
    provider: pecl # pecl or amqp_lib (require php-amqplib/php-amqplib)
    default_connection: rabbitmq
    default_command: swarrot.command.base # Swarrot\SwarrotBundle\Command\SwarrotCommand
    logger: logger # logger or channel logger like monolog.logger.[my_channel]
    connections:
        rabbitmq:
            url: "amqp://%rabbitmq_login%:%rabbitmq_password%@%rabbitmq_host%:%rabbitmq_port%/%rabbitmq_vhost%"
    consumers:
        my_consumer:
            processor: my_consumer.processor.service # Symfony service id implementing Swarrot\Processor\ProcessorInterface
            middleware_stack: # order matters
                 - configurator: swarrot.processor.signal_handler
                   # extras:
                   #     signal_handler_signals:
                   #         - SIGTERM
                   #         - SIGINT
                   #         - SIGQUIT
                 # - configurator: swarrot.processor.insomniac
                 - configurator: swarrot.processor.max_messages
                   # extras:
                   #     max_messages: 100
                 - configurator: swarrot.processor.max_execution_time
                   # extras:
                   #     max_execution_time: 300
                 - configurator: swarrot.processor.memory_limit
                   # extras:
                   #     memory_limit: null
                 - configurator: swarrot.processor.doctrine_connection
                   # extras:
                   #     doctrine_ping: true
                   #     doctrine_close_master: true
                 - configurator: swarrot.processor.doctrine_object_manager
                 - configurator: swarrot.processor.exception_catcher

                 - configurator: swarrot.processor.ack
                   # extras:
                   #     requeue_on_error: false
                 - configurator: swarrot.processor.retry
                   # extras:
                   #     retry_exchange: retry
                   #     retry_attempts: 3
                   #     retry_routing_key_pattern: 'retry_%%attempt%%'

                 # - configurator: swarrot.processor.services_resetter

            extras:
                poll_interval: 500000
    messages_types:
        my_publisher:
            connection: rabbitmq # use the default connection by default
            exchange: my_exchange
            routing_key: my_routing_key

Publishing a message

First step is to retrieve the Swarrot publisher service from your controller.

$messagePublisher = $this->get('swarrot.publisher');

After that, you need to prepare your message with the Message class.

use Swarrot\Broker\Message;

$message = new Message('"My first message with the awesome swarrot lib :)"');

Then you can publish a new message into a predefined configuration (connection, exchange, routing_key, etc.) from your message_types.

$messagePublisher->publish('my_publisher', $message);

When publishing a message, you can override the message_types configuration by passing a third argument:

$messagePublisher->publish('my_publisher', $message, array(
    'exchange'    => 'my_new_echange',
    'connection'  => 'my_second_connection',
    'routing_key' => 'my_new_routing_key'
));

Consuming a message

Swarrot will automatically create one command per consumer defined in your configuration. These command need the queue name to consume as first argument. You can also use a named connection as second argument if you don't want to use the default one.

app/console swarrot:consume:my_consumer queue_name [connection_name]

Your consumer (my_consumer.processor.service) must implements Swarrot\Processor\ProcessorInterface

use Swarrot\Processor\ProcessorInterface;

class MyProcessor implements ProcessorInterface
{
    public function process(Message $message, array $options)
    {
        var_dump($message->getBody()); // "My first message with the awesome swarrot lib :)"
    }
}

Your processor will also be decorated automatically by all processors listed in the middleware_stack section. The order matters.

All these processors are configurable. You can add an extras key on each configurator definition in your config.yml. Take a look at the configuration reference to see available extras for existing Configurators.

You can also use options of the command line:

  • --poll-interval [default: 500000]: Change the polling interval when no message found in broker
  • --requeue-on-error (-r): Re-queue the message in the same queue if an error occurred.
  • --no-catch (-C): Disable the ExceptionCatcher processor (available only if the processor is in the stack)
  • --max-execution-time (-t) [default: 300]: Configure the MaxExecutionTime processor (available only if the processor is in the stack)
  • --max-messages (-m) [default: 300]: Configure the MaxMessages processor (available only if the processor is in the stack)
  • --no-retry (-R): Disable the Retry processor (available only if the processor is in the stack)

Default values will be overriden by your config.yml and usage of options will override default config values.

Run your command with -h to have the full list of options.

Note that you can define one or more aliases for this command using the command_alias configuration:

swarrot:
    consumers:
        my_consumer:
            command_alias: 'my:super:commmand'

Thus allowing you to consume messages using a more appropriate wording:

app/console my:super:command queue_name [connection_name]

Implementing your own Provider

If you want to implement your own provider (like Redis), you first have to implement the Swarrot\SwarrotBundle\Broker\FactoryInterface. Then, you can register it along with the others services and tag it with swarrot.provider_factory.

services:
    app.swarrot.custom_provider_factory:
        class: AppBundle\Provider\CustomFactory
        tags:
            - {name: swarrot.provider_factory}
    app.swarrot.redis_provider_factory:
        class: AppBundle\Provider\RedisFactory
        tags:
            - {name: swarrot.provider_factory, alias: redis}

Now you can tell Swarrot to use it in the config.yml file.

swarrot:
  provider: app.swarrot.custom_provider_factory

or with the alias

swarrot:
  provider: redis

Using a custom processor

If you want to use a custom processor, you need two things. The Processor itself and a ProcessorConfigurator. For the Processor, you can refer to the swarrot/swarrot documentation. For the ConfigurationProcessor, you need to implement the ProcessorConfiguratorInterface and to register it as an abstract service, like this:

services:
  my_own_processor_configurator_service_id:
    abstract: true
    class: MyProject\MyOwnProcessorConfigurator

Once done, just add it to the middleware stack of your consumer:

middleware_stack:
  - configurator: swarrot.processor.signal_handler
  - configurator: my_own_processor_configurator_service_id

As usual, take care of the order of your middleware_stack.

Running your tests without publishing

If you use Swarrot, you may not want to actually publish messages when in test environment for example. You can use the BlackholePublisher to achieve this.

Simply override the swarrot.publisher.class parameter in the DIC with the Swarrot\SwarrotBundle\Broker\BlackholePublisher class, by updating config_test.yml for instance:

parameters:
    swarrot.publisher.class: Swarrot\SwarrotBundle\Broker\BlackholePublisher

Broker configuration

This bundle goal is to deal with message consuming, not to deal with your broker configuration. We don't want to mix the infrastructure logic with the consuming one.

If you're looking for a tool to configure your broker, take a look at odolbeau/rabbit-mq-admin-toolkit.

License

This bundle is released under the MIT License. See the bundled LICENSE file for details.

swarrot/swarrot-bundle 适用场景与选型建议

swarrot/swarrot-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.86M 次下载、GitHub Stars 达 90, 最近一次更新时间为 2014 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「bundle」 「swarrot」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 swarrot/swarrot-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 swarrot/swarrot-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 swarrot/swarrot-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.86M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 91
  • 点击次数: 9
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 90
  • Watchers: 3
  • Forks: 61
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-27