My code will try to connect to an CloudAMQP 3-node cluster, which provides 2 flavours of DNS names:
- A record that returns 3 IP addresses.
- Three records that returns each of the underlying IP address. Example:
PS C:\Users\LiuSean> nslookup funny-plum-koala.rmq2.cloudamqp.com
...
Non-authoritative answer:
Name: funny-plum-koala.rmq2.cloudamqp.com
Addresses: 3.218.133.71
34.231.176.151
44.192.12.180
PS C:\Users\LiuSean> nslookup funny-plum-koala-02.rmq2.cloudamqp.com
...
Non-authoritative answer:
Name: funny-plum-koala-02.rmq2.cloudamqp.com
Address: 3.218.133.71
PS C:\Users\LiuSean> nslookup funny-plum-koala-03.rmq2.cloudamqp.com
...
Non-authoritative answer:
Name: funny-plum-koala-03.rmq2.cloudamqp.com
Address: 34.231.176.151
Per Spring AMQP document https://docs.spring.io/spring-amqp/reference/html/#cluster, it seems that the preferred way is to use the individual names:
@Bean
public CachingConnectionFactory ccf() {
CachingConnectionFactory ccf = new CachingConnectionFactory();
ccf.setAddresses("host1:5672,host2:5672,host3:5672");
return ccf;
}
But does this offer benefits over using the single DNS records that returns 3 addresses? I've done my share of searching but there doesn't seem to be a definitive answer.
I've tested with containers with 3 addresses and it seems to work:
ccf.setAddresses("host1:5672,host2:5672,host3:5672");
However it'll be a lot of work to set up a separate DNS server to return 3 addresses to test.
The
CachingConnectionFactoryexposes ansetAddressResolver(AddressResolver)option:One of the impl is probably what you are looking for:
It does this internally
InetAddress.getAllByName(hostName);From docs: https://docs.spring.io/spring-amqp/reference/html/#addressresolver