我在从我的 EC2 实例获取/设置到 ElastiCache 集群时遇到问题。我得到 - SEVERE: net.spy.memcached.OperationTimeoutException: Timeout waiting for value
- 错误。
当我试图获取或设置一个值时。我在本地机器上使用了相同的代码(尽管与本地 memcached 服务器通信)并且一切正常。完整的堆栈跟踪可以在这里找到 - http://pastebin.com/tYcCJ6cj
我首先看到,我至少可以获取集群中所有节点的 IP 地址,以便我可以将其提供给我的 membase 客户端,并且我确实能够找出节点 IP 地址。我还确保将我的所有 EC2 安全组也添加到默认缓存集群安全组中。
对此的任何指示都会非常有帮助。
更新
用于获取特定记录的代码片段。
public String getCachedValue(String namespace, String key) {
String value = null;
try {
MemcachedClient client
= CacheConnectionUtil.connectToElastiCacheMemcachedServer();
// Point of origin for the exception.
return (String) client.get(namespace + "$" + hashKey(key));
} catch (IOException e) {
e.printStackTrace();
}
return value;
}
用于连接 ElastiCache 服务器的代码片段
private static MemcachedClient connectToElastiCacheMemcachedServer()
throws IOException {
DescribeCacheClustersResult cacheClustersInfo = null;
DescribeCacheClustersRequest cacheClusterRequest
= new DescribeCacheClustersRequest();
cacheClusterRequest.setShowCacheNodeInfo(true);
try {
cacheClustersInfo = AWSConnectionUtil
.getElastiCacheObject(null)
.describeCacheClusters(cacheClusterRequest);
} catch (Exception e) {
e.printStackTrace();
throw new IOException("Unable to connect to ElastiCache Cluster.", e);
}
if (cacheClustersInfo == null) {
throw new IOException("ElastiCache Cluster Info Object is null.");
}
List<CacheCluster> clusters = cacheClustersInfo.getCacheClusters();
if (clusters == null || clusters.isEmpty()) {
throw new IOException("No ElastiCache Clusters available.");
}
List<String> serverList = new ArrayList<String>();
for (CacheCluster cluster : clusters) {
if (cluster != null
&& AWSConstants
.CACHE_CLUSTER_ID
.equalsIgnoreCase(cluster.getCacheClusterId())) {
List<CacheNode> nodes = cluster.getCacheNodes();
if (nodes != null ) {
for (CacheNode node : nodes) {
if (node != null) {
Endpoint endpoint = node.getEndpoint();
if (endpoint != null
&& endpoint.getAddress() != null) {
serverList.add(endpoint.getAddress()
+ ":"
+ endpoint.getPort());
}
}
}
}
}
}
if (serverList.isEmpty()) {
throw new IOException("No Cached nodes available for cluster - "
+ AWSConstants.CACHE_CLUSTER_ID);
}
return new MemcachedClient(AddrUtil.getAddresses(serverList));
}