fivelab/amqp-bundle

用于将fivelab/amqp库集成到Symfony应用程序的扩展包

v2.1.0 2024-02-23 11:08 UTC

This package is auto-updated.

Last update: 2024-09-23 13:38:44 UTC


README

 #StandWithUkraine 

AMQP 扩展包

Build Status

AMQP库集成到您的Symfony应用程序。

示例配置

fivelab_amqp:
    connections:
        default:
            dsn: 'amqp://guest:guest@127.0.0.1:5672/%2f?read_timeout=30&other_parameter=some'

    exchanges:
        primary:
            connection: default
            name: direct
            type: direct
            durable: true

    publishers:
        primary:
            exchange: primary

    queues:
        collect_reports:
            name: reports.collect
            connection: default
            bindings:
                - { exchange: primary, routing: customer.register }
                - { exchange: primary, routing: customer.activate }

    consumers:
        collect_reports:
            queue: collect_repots
            message_handlers: 'acme.service.collect_reports_message_handler'

初始化

安装并配置扩展包后,您可以初始化所有交换机和队列

./bin/console event-broker:initialize:exchanges
./bin/console event-broker:initialize:queues

初始化交换机和队列后,您可以运行消费者

./bin/console event-broker:consumer:run collect_reports

为了调试,您可以使用详细级别。

发布消息

为了将消息发布到代理,我们使用Publisher系统。您可以配置更多发布者。

<?php

namespace Acme\Controller;

use FiveLab\Component\Amqp\Publisher\PublisherInterface;
use FiveLab\Component\Amqp\Message\Message;
use FiveLab\Component\Amqp\Message\Payload;

class MyController 
{
    public function __construct(private PublisherInterface $publisher)
    {
        $this->publisher = $publisher;
    }

    public function handleAction(): void
    {
        $payload = new Payload('hello world');
        $message = new Message($payload);
        
        $this->publisher->publish($message, 'customer.register');
    }   
}

事务性

RabbitMQ支持事务层,我们实现了这一功能。要使用事务层,您必须为事务层创建新通道,并在发布者中使用此通道

fivelab_amqp:
    # Configure connections, etc...
    channels:
        transactional:
            connection: default 

    publishers:
        # Publishers
        primary_transactional:
            exchange: primary
            channel: transactional
            savepoint: false

注意:您应该在应用程序中(例如在命令总线上的中间件层)直接启动/提交/回滚事务。请参阅FiveLab\Component\Amqp\Channel\ChannelInterface::(start|commit|rollback)Transaction方法。

如果您想使用保存点,可以将savepoint设置为true。我们实现了这一功能(\FiveLab\Component\Amqp\Publisher\SavepointPublisherDecorator)。

开发

为了方便开发,您可以使用Docker

docker build -t amqp-bundle .
docker run -it \
    --name amqp-bundle \
    -v $(pwd):/code \
    amqp-bundle bash

成功运行并连接到容器后,您必须安装依赖项

composer install

在创建PR或合并到develop之前,请运行以下命令以验证代码

./bin/phpunit

./bin/phpcs --config-set show_warnings 0
./bin/phpcs --standard=vendor/escapestudios/symfony2-coding-standard/Symfony/ src/
./bin/phpcs --standard=tests/phpcs-ruleset.xml tests/