Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure multiple exchanges in laravel-queue-rabbitmq?

I have found famous library for using RabbitMQ in Laravel.

In configuration config/queue.php I can specify only one exchange and queue name. Does it support multiple exchanges?

like image 521
OPV Avatar asked Nov 06 '25 01:11

OPV


1 Answers

For future people who will search the answer for this question. There is one(bad) way to add multiple exchanges. You have to duplicate rabbitmq connection with new exchange and when you will want to publish the message to new exchange, you will just change connection.

This is a very ugly way, but i didn't another one. Laravel queue doesn't provide a method to change the exchange, but there is the method onConnection what you help you.

Here is a simple example

        'conn_one' => [

        'driver' => 'rabbitmq',
        'queue' => env('RABBITMQ_QUEUE', 'default'),
        'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

        'hosts' => [
            [
                'host' => env('RABBITMQ_HOST', '127.0.0.1'),
                'port' => env('RABBITMQ_PORT', 5672),
                'user' => env('RABBITMQ_USER', 'guest'),
                'password' => env('RABBITMQ_PASSWORD', 'guest'),
                'vhost' => env('RABBITMQ_VHOST', '/'),
            ],
        ],

        'options' => [
            'ssl_options' => [
                'cafile' => env('RABBITMQ_SSL_CAFILE', null),
                'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
                'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
                'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
            ],
            'queue' => [
                'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
                'exchange' => 'exchange_two',
                'exchange_type' => 'fanout',
            ],
        ],

     ],

    'conn_two' => [
        'driver' => 'rabbitmq',
        'queue' => env('RABBITMQ_QUEUE', 'default'),
        'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

        'hosts' => [
            [
                'host' => env('RABBITMQ_HOST', '127.0.0.1'),
                'port' => env('RABBITMQ_PORT', 5672),
                'user' => env('RABBITMQ_USER', 'guest'),
                'password' => env('RABBITMQ_PASSWORD', 'guest'),
                'vhost' => env('RABBITMQ_VHOST', '/'),
            ],
        ],

        'options' => [
            'ssl_options' => [
                'cafile' => env('RABBITMQ_SSL_CAFILE', null),
                'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
                'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
                'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
            ],
            'queue' => [
                'job' => VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob::class,
                'exchange' => 'exchange_two',
                'exchange_type' => 'fanout',
            ],
        ],
    ],

Use

ExampleJob::dispach($data)->onConnection('conn_one');
ExampleJob::dispach($data)->onConnection('conn_two');
like image 106
Alexandru Gojinetchi Avatar answered Nov 07 '25 14:11

Alexandru Gojinetchi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!