Skip to content

How to use multiply AMQP RabbitMQ connections in custom module

I need connect in custom module to multiply RabbitMQ hosts. So how setup it?
I tried something like this in app/etc/env.php

'queue' => [
    'amqp' => [
        'connections' => [
            'default' => [
                'host' => 'host1.test',
                'port' => '5672',
                'user' => 'guest',
                'password' => 'guest',
                'virtualhost' => '/'
            ],
            'site' => [
                'host' => 'host2.test',
                'port' => '5672',
                'user' => 'user2',
                'password' => 'pwd2',
                'virtualhost' => 'vh2'
            ],
            // Add more connections here as needed
        ]
    ],
    'consumers_wait_for_messages' => 0
],

I find solution:
It work like this for magento 2.4

'queue' => [
        'amqp' => [
            'host' => 'rabbitmq.magento2.docker',
            'port' => '5672',
            'user' => 'guest',
            'password' => 'guest',
            'virtualhost' => '/'
        ],
        'connections' => [
            'amqpRemoteDev' => [
                'host' => 'remote.developer.server',
                'port' => '5672',
                'user' => 'user',
                'password' => 'password',
                'virtualhost' => 'vh'
            ]
        ],
    ],

Then connection can be used in consumer

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/consumer.xsd">
    <consumer name="unique.name.consumer" queue="queue.name" connection="amqpRemoteDev"
              handler="VendorModuleModelRabbitMQConsumer::processMessage"/>
</config>