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

Fisher Coder

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.

Elasticsearch Ninja

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JedisCluster : redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster

redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool

Flink: java.io.NotSerializableException: redis.clients.jedis.JedisCluster

redis.clients.jedis.exceptions.JedisDataException: Please close pipeline or multi block before calling this method

Caused by: java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: redis/clients/jedis/Tuple - Redis Spring Boot

redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: malformed number near

AWS Redis : JedisConnectionException: java.net.SocketException: Too many open files

java.net.UnknownHostException: postgres

java.net.UnknownHostException on Docker

Kafka Error connecting to node ubuntukafka:9092 (id: 0 rack: null) (org.apache.kafka.clients.NetworkClient) java.net.UnknownHostException:

Why Getting java.lang.ClassNotFoundException: redis.clients.jedis.util.SafeEncoder exception when using spring-boot-starter-data-redis?

Consider defining a bean of type 'redis.clients.jedis.JedisPool' in your configuration. error while integrating Redis Jedis

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure - Caused by: java.net.UnknownHostException: null: unknown error

JedisConnectionException: java.net.SocketTimeoutException: connect timed out

Redis for Local Testing using Java Jedis Client without a REDIS Server

java.net.UnknownHostException error in InetAddress

java.net.UnknownHostException: Name or service not known

java.net.UnknownHostException on file:// method

java.net.UnknownHostException harvest Dspace collection

java.net.UnknownHostException spring ,zuul ,eureka

Websphere 8.5 - java.net.UnknownHostException

Error: -copyFromLocal: java.net.UnknownHostException

How to listen for keyspace notifications of redis cluster in java using jedis client?

How to get all records using key in Redis using java (Jedis)

Spring Data Redis JedisConnectionException: Unexpected end of stream

Redis/Jedis not serving properly

Redis/Jedis - Delete by pattern?

Redisson vs Jedis for redis

java.net.UnknownHostException Cannot create socket. Java