I have a 3 Node cluster running on GCP in Kubernetes. I am able to forward the port and connect with my DB Tool to the cluster:
$ kubectl port-forward elassandra-0 9042
When I try to connect to the cassandra cluster from my Spring Boot application, I get the following error:
2018-11-14 17:43:36,339 INFO [5914] [localhost-startStop-1] c.d.d.c.Cluster [Cluster.java:1587] New Cassandra host /10.4.3.3:9042 added
2018-11-14 17:43:36,339 INFO [5914] [localhost-startStop-1] c.d.d.c.Cluster [Cluster.java:1587] New Cassandra host /10.4.2.4:9042 added
2018-11-14 17:43:36,340 INFO [5915] [localhost-startStop-1] c.d.d.c.Cluster [Cluster.java:1587] New Cassandra host /127.0.0.1:9042 added
2018-11-14 17:43:41,391 WARN [10966] [cluster1-nio-worker-2] c.d.d.c.HostConnectionPool [HostConnectionPool.java:184] Error creating connection to /10.4.2.4:9042
com.datastax.driver.core.exceptions.TransportException: [/10.4.2.4:9042] Cannot connect
at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:167)
at com.datastax.driver.core.Connection$1.operationComplete(Connection.java:150)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:511)
at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:504)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:483)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:424)
at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:121)
I am trying to connect to 127.0.0.1:9042
, the other hosts are being pulled from the cluster by the spring-data framework.
What am I doing wrong here?
Thanks.
To add more details since you have automatic address resolution enabled, your client it's trying to add contact points to all the nodes in your cluster:
10.4.3.3:9042
10.4.2.4:9042
127.0.0.1:9042
However, the top 2 nodes cannot be found from your localhost because they are not being proxied to. If you added them as proxies it wouldn't work either because you can't proxy on the same port from your laptop. The solution like @AlexTbk mentioned is to use a single contact point but specifying address resolution on the client with the single 127.0.0.1:9042
contact point.
Answering own question:
You need to specify custom adress resolution driver side as described here:
https://docs.datastax.com/en/developer/java-driver/2.1/manual/address_resolution/
In detail when you want to use only one node:
public class MyAdressTranslator implements AddressTranslator {
@Override
public void init(Cluster cluster) {}
public InetSocketAddress translate(InetSocketAddress address) {
return new InetSocketAddress("127.0.0.1", 9042);
}
@Override
public void close() {}
}