我使用Jedis连接到我在AWS中的Redis实例/群集,但我一直收到此错误,这里是代码,我在SO上进行了广泛搜索,发现最接近的是:{{ 3}}
我尝试了两种方式,既不适合我。 所以请帮忙。
这是我的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
我做错了什么? 请指出。
答案 0 :(得分:5)
你的设置应该是这样的:
Jedis jedis = new Jedis("REDISNAME.nquffl.0001.apn2.cache.amazonaws.com",6379);
不是这样的:
Jedis jedis = new Jedis("REDISNAME.nquffl.0001.apn2.cache.amazonaws.com:6379");
答案 1 :(得分:3)
根据AWS文档http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Access.Outside.html
Amazon ElastiCache是一项提供基于云的AWS服务 内存中的键值存储。在后端它使用 Memcached或Redis引擎。该服务旨在访问 完全来自AWS内部。但是,如果ElastiCache群集是 托管在VPC中,您可以使用网络地址转换(NAT) 实例提供外部访问。
所以你有两个选择: -
您可以在AWS内部托管您的应用,并拥有适当的安全组设置,以允许从部署了您的应用的ec2-instance访问弹性缓存群集。
如果您想在AWS之外运行您的应用,则必须修改网络地址转换(NAT)以提供外部访问。
IMO,它很容易在AWS-Ec2实例中部署代码并在您不熟悉网络和NAT时对其进行测试。
我曾经拥有本地memcache和redis实例,我曾经用于连接本地开发以及用于在AWS ec2实例中部署它的qa,stg,prod等其他环境。
如果您有任何问题,请告诉我。
答案 2 :(得分:0)
在我的情况下,6379端口不接受连接,因此我更改了将Redis配置为其他端口的方法,然后它起作用了。