6

我在从EC2实例获取/设置ElastiCache群集时遇到问题。我越来越 - SEVERE: net.spy.memcached.OperationTimeoutException: Timeout waiting for value - 错误。无法连接到AWS ElastiCache群集使用Membase客户端lib进行memcached

当我试图获得或设置一个值。我在本地机器上使用了相同的代码(尽管与本地memcached服务器通信),一切正常。完整的堆栈跟踪可以在这里找到 - http://pastebin.com/tYcCJ6cj

我第一次看到,我可以至少得到一个集群中的所有节点的IP地址,以便我可以养活它在我的Membase的客户&我确实能够找到出节点的IP地址。 我还确保将所有EC2安全组都添加到默认缓存群集安全组中。

任何指针都会很有帮助。

UPDATE

的代码片段用来获取特定的记录。

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)); 
} 
+0

你可以发布你的代码吗?没有看到你在做什么,就无法诊断这个问题。堆栈跟踪只是说操作需要很长时间才能完成。 – mikewied

+0

@mikewied请参阅具有代码段的更新问题。谢谢。 – Chantz

+0

你的代码显示正确,没有任何东西会跳出来。我只是再次检查地址/端口组合是否正确。如果是,则尝试从该代码运行的同一位置远程登录到群集中的某台机器,然后检查是否可以执行get操作。 – mikewied

回答

0

使用亚马逊生产的改性ElastiCache集群客户端。

http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/AutoDiscovery.html#AutoDiscovery.ClusterClient

根据该文件,下载ElastiCache集群客户端:

  1. 登录到AWS管理控制台,并在https://console.aws.amazon.com/elasticache/打开ElastiCache控制台。
  2. 在ElastiCache控制台中,单击下载ElastiCache群集客户端。

适用于Java的ElastiCache集群客户端的源代码位于https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java。这个库基于流行的Spymemcached客户端。 ElastiCache群集客户端在Amazon软件许可下发布。您可以自由修改源代码,只要您认为合适;您甚至可以将代码合并到其他开源Memcached库或您自己的客户端代码中。

目前版本为1.0.1。一年多来,我一直在使用1.0版本,没有任何问题。

相关问题