【问题标题】:Unable to connect to AWS ElastiCache clusters using Membase client lib for memcached无法使用用于 memcached 的 Membase 客户端库连接到 AWS ElastiCache 集群
【发布时间】:2011-12-29 22:42:23
【问题描述】:

我在从我的 EC2 实例获取/设置到 ElastiCache 集群时遇到问题。我收到 - SEVERE: net.spy.memcached.OperationTimeoutException: Timeout waiting for value - 错误。

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

我第一次看到,我至少可以获取集群中所有节点的 IP 地址,这样我就可以将它提供给我的 membase 客户端,并且我确实能够找出节点 IP 地址。 我还确保将我的所有 EC2 安全组也添加到默认缓存集群安全组中。

对此的任何指示都会非常有帮助。

更新

用于获取特定记录的代码 sn-p。

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 服务器的代码 sn-p

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

【问题讨论】:

  • 你能发布你的代码吗?如果不查看您在做什么,就无法诊断此问题。堆栈跟踪只是说该操作完成的时间太长。
  • @mikewied 请查看代码为 sn-p 的更新问题。谢谢。
  • 您的代码看起来是正确的,没有什么让我跳出来的。我会仔细检查地址/端口组合是否正确。如果是,则尝试从运行此代码的同一位置远程登录到集群中的一台机器,并检查是否可以获取。
  • @mikewied 是的,我实际上获取了堆栈跟踪中抛出的 ip 地址并使用它从我的一个 ec2 实例中远程登录,我可以做一个获取,正确放置。
  • 你找到问题所在了吗?

标签: java amazon-web-services timeout memcached amazon-elasticache


【解决方案1】:

使用 Amazon 生产的修改后的 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 集群客户端。

ElastiCache Cluster Client for Java 的源代码可从https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java 获得。该库基于流行的 Spymemcached 客户端。 ElastiCache 集群客户端是根据 Amazon 软件许可发布的。您可以随意修改源代码;您甚至可以将代码合并到其他开源 Memcached 库或您自己的客户端代码中。

当前版本为 1.0.1。我已经使用 1.0 版本一年多了,没有任何问题。

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 2019-09-12
    • 2014-03-10
    • 2018-01-22
    • 1970-01-01
    • 2016-05-16
    • 2020-03-31
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多