Search code examples
javaamazon-web-servicesredisjedisamazon-elasticache

redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException


I'm using Jedis to connect to my Redis instance/cluster in AWS, but I kept getting this error, here's the code, I searched extensively on SO, found the closest one is: String hostname from properties file: Java

I tried both ways, neither worked for me. So please help.

Here's my Java code:

public static void main(String[] args) {
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
            + "Please make sure that your credentials file is at the correct "
            + "location (/Users/USERNAME/.aws/credentials), and is in valid format.", e);
    }

    AmazonElastiCacheClient client = new AmazonElastiCacheClient(credentials);
    client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_2));
    DescribeCacheClustersRequest dccRequest = new DescribeCacheClustersRequest();
    dccRequest.setShowCacheNodeInfo(true);

    DescribeCacheClustersResult clusterResult = client.describeCacheClusters(dccRequest);
List<CacheCluster> cacheClusters = clusterResult.getCacheClusters();
        for (CacheCluster cacheCluster : cacheClusters) {
        for (CacheNode cacheNode : cacheCluster.getCacheNodes()) {
            String addr = cacheNode.getEndpoint().getAddress();
            int port = cacheNode.getEndpoint().getPort();
            String url =  addr + ":" + port;
            System.out.println("formed url is: " + url);

            Jedis jedis = new Jedis(url);
            System.out.println("Connection to server sucessfully");
            // check whether server is running or not
            System.out.println("Server is running: " + jedis.ping());
        }
    }

The last line in the above code keeps throwing this error, here's the stacktrace:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException: REDISNAME.nquffl.0001.apn2.cache.amazonaws.com:6379
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
at sporadic.AmazonElastiCacheClientExample.main(AmazonElastiCacheClientExample.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)Caused by: java.net.UnknownHostException: REDISNAME.nquffl.0001.apn2.cache.amazonaws.com:6379
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 11 more

What am I doing wrong? Please point out.


Solution

  • According to AWS Documentation http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Access.Outside.html

    Amazon ElastiCache is an AWS service that provides cloud-based in-memory key-value store. On the back end it uses either the Memcached or Redis engine. The service is designed to be accessed exclusively from within AWS. However, if the ElastiCache cluster is hosted inside a VPC, you can use a Network Address Translation (NAT) instance to provide outside access.

    So you have below two options :-

    1. Either you host your app inside the AWS and have proper security group setting to allow access to your elastic-cache cluster from your ec2-instance where your app is deployed.

    2. If you want to run your app outside of AWS then you have to modify the Network Address Translation (NAT) to provide outside access.

    IMO, its easy to deploy the code in AWS-Ec2 instance and test it if you are not very familiar with the networking and NAT.

    I used to have locally memcache and redis instance where i used to connect for local developement and for other environment like qa,stg,prod used to deploy it in AWS ec2 instance.

    Let me know if you any issues.