bernard/bernard-bundle

将 Bernard 集成到 Symfony2 应用程序中。

安装次数: 620,763

依赖者: 3

建议者: 1

安全: 0

星标: 96

关注者: 6

分支: 34

开放问题: 11

类型:symfony-bundle

v2.0.2 2017-07-23 12:07 UTC

README

将 Bernard 精确集成到 Symfony 应用程序中。

Build Status

入门指南

一切从安装包开始。这通过 composer 完成,您需要在您的 composer.json 文件中添加以下行,并运行 composer update bernard/bernard-bundle

{
    "require" : {
        "bernard/bernard-bundle" : "~1.0"
    }
}

接下来是将包添加到您的 kernel 中,并在 config.yml 中进行配置。

// app/AppKernel.php
// .. previous class definition
public function registerBundles()
{
    // .. all the other bundles you have registered.
    $bundles[] = new Bernard\BernardBundle\BernardBundle();
    // .. the rest of the method
}
# .. previous content of app/config/config.yml
bernard:
    driver: file # you can choose predis, phpredis, file, doctrine, sqs etc.

太好了!您现在可以使用这个小工具了。请访问 bernard.readthedocs.org 上的 Bernard 文档的其余部分。

运行消费者

如果您不知道如何运行消费者,消息队列有什么用?幸运的是,此包自动将命令注册到您的应用程序中。因此,如果您运行 php app/console,应该会看到 bernard:consumebernard:produce。它们的工作方式与文档描述的一样,但如果您不确定,请在运行命令时添加 --help

在长时间运行消费者时,使用 --no-debug 非常重要。这是因为 Symfony 默认在调试模式下收集大量的信息和日志,如果不省略这些信息,您迟早会遇到内存问题。

添加接收者

为了知道哪些消息需要去哪里,您需要注册一些接收者。这通过在您的服务定义中使用标签来完成。

my_receiver:
    class: Acme\Receiver
    # public: true # Make sure the service is public
    tags:
         - { name: bernard.receiver, message: SendNewsletter }
         - { name: bernard.receiver, message: ImportUsers }

如示例所示,可以注册相同的接收者用于许多不同的消息类型。

配置选项

有一些不同的选项可以设置,可以改变各种驱动程序的行为。

Doctrine

当使用 doctrine 驱动程序时,在 Bernard 中使用单独的连接可能很有用。为了更改它,请使用 connection 选项。如果默认连接不是 default,也需要设置它。

doctrine:
    dbal:
        connections:
            bernard:
                host:     "%database_host%"
                charset:  UTF8

bernard:
    driver: doctrine
    options:
        connection: bernard # default is the default value

FlatFile

文件驱动程序需要知道它应该使用哪个目录来存储消息及其队列元数据。

bernard:
    driver: file
    options:
        directory: %kernel.cache_dir%/bernard

上述示例将消息转储到缓存文件夹中。在大多数情况下,您会想更改这个设置,因为每次清除缓存时都会删除缓存文件夹(显然)。

PhpAmqp / RabbitMQ

PhpAmqp 依赖于一个名为 old_sound_rabbit_mq.connection.default 的服务,该服务具有一个配置的连接实例,该实例扩展了 \PhpAmqpLib\Connection\AbstractConnection 类。如果您想使用不同的名称,请使用 phpamqp_service 选项

bernard:
    driver: phpamqp
    options:
        phpamqp_service: my_phpamqp_service

您需要定义 phpamqp_exchange。可选,您可以定义 phpamqp_default_message_parameters

bernard:
    driver: phpamqp
    options:
        phpamqp_exchange: my_phpamqp_service
        phpamqp_default_message_parameters:
            content_type: application/json
            delivery_mode: 2

PhpRedis

PhpRedis 依赖于一个名为 snc_redis.bernard 的服务,该服务具有一个配置的 Redis 实例。如果您想使用不同的名称,请使用 phpredis_service 选项

bernard:
    driver: phpredis
    options:
        phpredis_service: my_redis_service

如果您使用的是 SncRedisBundle,您必须将 bernhard 客户端的日志设置为 false,以确保它是 Redis 实例而不是包装实例。此外,如果消费者抛出 RedisException: read error on connection,您需要将 connection_timeout(请参阅 SncRedisBundle 配置选项)选项设置为大于 5(秒)的值。

IronMQ

当使用 IronMQ 驱动程序时,您必须配置一个 IronMQ 连接实例。您可以像以下这样进行配置

services:
    ironmq_connection:
        class: IronMQ
        arguments:
            - { token: %ironmq_token%, project_id: %ironmq_project_id% }
        public: false

bernard:
    driver: ironmq
    options:
        ironmq_service: ironmq_connection

Amazon SQS

要使用 Amazon SQS,请按如下方式配置您的驱动程序

services:
    my_sqs_client:
        class: Aws\Sqs\SqsClient
        factory: Aws\Sqs\SqsClient::factory
        arguments:
            region: "your aws region" # e.g. "eu-west-1"
            key: "your aws user's key"
            secret: "your aws user's secret"

bernard:
    driver: sqs
    options:
        sqs_service: my_sqs_client
        sqs_queue_map: # optional for aliasing queue urls (default alias is the url section after the last "/"), e.g.:
            send_newsletter: https://sqs.eu-west-1.amazonaws.com/...
        prefetch: 1 # optional, but beware the default is >1 and you may run into invisibility timeout problems with that

Pheanstalk

要使用 Pheanstalk(pda/pheanstalk),请按如下方式配置您的驱动程序

services:
    my.pheanstalk.connection:
        class: Pheanstalk\Pheanstalk
        arguments:
            - %some.parameter.containing.pheanstalk.host%

bernard:
    driver: pheanstalk
    options:
        pheanstalk_service: my.pheanstalk.connection