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

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

我在 AWS 中使用 Jedis 连接到我的 Redis instance/cluster,但我一直收到此错误,这是代码,我在 SO 上进行了广泛搜索,发现最接近的是:String hostname from properties file: Java

两种方法我都试过了,都不适合我。 所以请帮忙。

这是我的 Java 代码:

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());
        }
    }

上面代码的最后一行一直抛出这个错误,这里是堆栈跟踪:

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

我做错了什么? 请指出。

根据 AWS 文档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.

所以你有以下两个选择:-

  1. 要么您在 AWS 中托管您的应用程序,并具有适当的安全组设置以允许从部署您的应用程序的 ec2 实例访问您的弹性缓存集群。

  2. 如果您想 运行 您的应用程序在 AWS 之外,那么您必须修改网络地址转换 (NAT) 以提供外部访问。

IMO,如果您对网络和 NAT 不是很熟悉,很容易在 AWS-Ec2 实例中部署代码并进行测试。

我曾经有本地 memcache 和 redis 实例,我曾经在其中连接以进行本地开发和其他环境,如 qa、stg、prod,用于将其部署在 AWS ec2 实例中。

如果您有任何问题,请告诉我。

你的设置应该是这样的:

Jedis jedis = new Jedis("REDISNAME.nquffl.0001.apn2.cache.amazonaws.com",6379);

不是这样:

Jedis jedis = new Jedis("REDISNAME.nquffl.0001.apn2.cache.amazonaws.com:6379");

在我的例子中,6379 端口不接受连接,所以我更改了我为 Redis 配置了不同的端口,并且成功了。