Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding AWS ElastiCache endpoints with Java

I'm trying to programmatically get a list of ElastiCache endpoints from my Java app using the latest Java AWS SDK. Things don't seem to be working - I can find a valid CacheCluster, but then when I list its nodes, it's empty. Here's my code:

CacheCluster cc = it.next();

System.out.println("Cache node type: " + cc.getCacheNodeType());
System.out.println("Number cache nodes: " + cc.getNumCacheNodes());

List<CacheNode> listCache = cc.getCacheNodes();

System.out.println("List size: " + listCache.size());

When I run it, I get the following output:

Cache node type: cache.m1.small 
Number cache nodes: 1 
List size: 0

This seems so simple, but doesn't seem to work. I have started an ElastiCache cluster with a single node, but the list comes up empty when I call getCacheNodes(). I've tried to run this code locally and on an EC2 instance, and I get the same thing both times.

Any ideas on what I could be doing wrong?

like image 418
Sander Smith Avatar asked Mar 13 '12 20:03

Sander Smith


People also ask

How do I get Redis endpoint?

To find a Redis (cluster mode enabled) cluster's endpointFrom the navigation pane, choose Redis clusters. The clusters screen will appear with a list of Redis (cluster mode disabled) and Redis (cluster mode enabled) clusters. Choose the Redis (cluster mode enabled) cluster you wish to connect to.

How do I check my AWS ElastiCache?

Sign in to the AWS Management Console and open the Amazon ElastiCache console at https://console.aws.amazon.com/elasticache/ . In the ElastiCache console dashboard, choose Redis to display a list of all your clusters that are running any version of Redis.

What is configuration endpoint in Redis?

Redis (Cluster Mode Enabled) endpoints A Redis (cluster mode enabled) cluster has a single configuration endpoint. By connecting to the configuration endpoint, your application is able to discover the primary and read endpoints for each shard in the cluster. For more information, see Finding connection endpoints.


1 Answers

According to the AWS team response to Not able to get cache nodes from ElastiCache cluster you'll need to use optional ShowDetails flag to obtain CacheNodes Information via the Class DescribeCacheClustersRequest parameter of method describeCacheClusters(). Looking closer there is no ShowDetails flag though, despite being documented for this class indeed:

An optional ShowDetails flag can be used to retrieve detailed information about the Cache Nodes associated with the Cache Cluster. Details include the DNS address and port for the Cache Node endpoint.

Presumably this actually targets setShowCacheNodeInfo(), which is An optional flag that can be included in the DescribeCacheCluster request to retrieve Cache Nodes information.

So the AWS team response seems imprecise and actually isn't addressing the question, why method getCacheNodes() from Class CacheCluster isn't returning that information, both being pretty unusual for such posts.

Anyway, you might simply want to try method getCacheNodes() from Class CacheCluster as returned by method getCacheClusters() from Class DescribeCacheClustersResult instead, hopefully it works as advertized (i.e. I haven't tried this myself).

Good luck!


Update

Here is the code Sander used successfully to achieve his goal, confirming the approach outlined above:

AmazonElastiCacheClient client = new AmazonElastiCacheClient(credentials);
DescribeCacheClustersRequest dccRequest = new DescribeCacheClustersRequest();
dccRequest.setShowCacheNodeInfo(true);

DescribeCacheClustersResult clusterResult = client.describeCacheClusters(dccRequest);

The missing pieces should be similar to his initial solution, e.g.:

List<CacheCluster> cacheClusters = clusterResult.getCacheClusters();
for (CacheCluster cacheCluster : cacheClusters) {
    List<CacheNode> cacheNodes = cacheCluster.getCacheNodes();

    System.out.println("List size: " + cacheNodes.size());
}
like image 194
Steffen Opel Avatar answered Sep 27 '22 16:09

Steffen Opel