我在从我的 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));
}
最佳答案
使用亚马逊出品的修改过的ElastiCache Cluster客户端。
根据文档,下载ElastiCache Cluster Client:
- 登录 AWS 管理控制台并打开 ElastiCache 控制台 https://console.aws.amazon.com/elasticache/ .
- 在 ElastiCache 控制台中,单击下载 ElastiCache 集群客户端。
适用于 Java 的 ElastiCache 集群客户端的源代码位于 https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java。 .这个库基于流行的 Spymemcached 客户端。 ElastiCache 集群客户端根据 Amazon 软件许可发布。您可以根据需要自由修改源代码;您甚至可以将代码合并到其他开源 Memcached 库中,或合并到您自己的客户端代码中。
当前版本为1.0.1。我使用 1.0 版已经一年多了,没有任何问题。
关于java - 无法使用 memcached 的 Membase 客户端库连接到 AWS ElastiCache 集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8143948/