connection string of RabbitMQ cluster using nodejs?

1.3k views Asked by At

I am trying to connect to RabbitMQ cluster using nodejs library amqplib

I cannot find any information on connection string. I am trying var

amqp_url = 
 ["amqp://admin:[email protected]:5672/vhost_test","amqp://admin:[email protected]:5672/vhost_test","amqp://admin:[email protected]:5672/vhost_test"];

is there any other library supports cluster?

1

There are 1 answers

0
Lebeau Daïkini On

What version are you use ?

If you are using version 0.5.0 or earlier, you can use this configuration:

const hosts = [
  'rabbitmq-node1:5672',
  'rabbitmq-node2:5672',
  'rabbitmq-node3:5672'
];

const connection = await amqp.connect({
  port: 5672,
  username: 'guest',
  password: 'guest',
  hosts: hosts, // specify the cluster nodes
  heartbeat: 60 // send a health check every 60 seconds to check that the connection is still active
});

otherwise if it's a version higher than 0.5.0, I think amqplib doesn't take into account this ability to specify hosts anymore. In fact, it should do

const amqp_uri = 'amqp://guest:guest@rabbitmq-node1:5672';

const connection = await amqp.connect(amqp_uri);

and behind it, RabbitMQ provides the other urls of the nodes during the connection.