Search code examples
javadnsservice-discoverymicroservicesconsul

How to point to specific DNS with custom port in Java


I'm using DNS Interface of Consul. It is available at localhost, port 8600. How can I point to this DNS in my Java program?

So far I can only change the address to localhost using Java system properties

System.setProperty("sun.net.spi.nameservice.nameservers", "localhost");
System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");

But I could not find the way for the port 8600. Please help?

This is the command to query for web.service.consul

dig @127.0.0.1 -p 8600 web.service.consul

Solution

  • I'm not sure if that's an option for you, but you could connect to the Consul agent using one of the libraries (e.g. consul-client) and utilize it's capabilities for service discovery.