Resolving addresses of the VM located in different regions : Google Cloud

I am an absolute beginner with Google Cloud Platform and the networking. I am trying to communicate between two VM located in different regions. When I ping the hosts or when I ssh using the IP Address they are working file.

But when I trying to access the VM's from a java program I am getting below exception. Can someone help me

java.io.IOException: Can't resolve address: kafka-3:9092
        at org.apache.kafka.common.network.Selector.doConnect(Selector.java:235) ~[kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.common.network.Selector.connect(Selector.java:214) ~[kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:864) [kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.NetworkClient.access$700(NetworkClient.java:64) [kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:1035) [kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:920) [kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:508) [kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:239) [kafka-clients-2.0.0.jar!/:?]
        at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:163) [kafka-clients-2.0.0.jar!/:?]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_181]
Caused by: java.nio.channels.UnresolvedAddressException
        at sun.nio.ch.Net.checkAddress(Net.java:101) ~[?:1.8.0_181]
        at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622) ~[?:1.8.0_181]
        at org.apache.kafka.common.network.Selector.doConnect(Selector.java:233) ~[kafka-clients-2.0.0.jar!/:?]
        ... 9 more

Solution 1:

Provided you work only with GCP Compute Instances, you have several options:

  1. configure /etc/hosts with all hardcoded IPs and hostnames in all instances (not recommended, just for debugging purposes)
  2. setup a hosted Cloud DNS zone, and create an entry for each instance, then use DNS: https://cloud.google.com/dns/docs/overview
  3. use internal DNS, a sort of DNS zone provided by default by Google: https://cloud.google.com/compute/docs/internal-dns

You might have more options but those 3 are commonly used.