

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Auto Discovery
<a name="AutoDiscovery.Using"></a>

要开始将 Auto Discovery 与 ElastiCache Memcached 配合使用，请执行以下步骤：
+ [获取配置端点](#AutoDiscovery.Using.ConfigEndpoint)
+ [下载集 ElastiCache 群客户端](#AutoDiscovery.Using.ClusterClient)
+ [修改应用程序编程](#AutoDiscovery.Using.ModifyApp)

## 获取配置端点
<a name="AutoDiscovery.Using.ConfigEndpoint"></a>

如要连接至某个集群，客户端程序必须知道集群的配置终端节点。请参阅主题[查找集群的端点（控制台）（Memcached）](Endpoints.md#Endpoints.Find.Memcached)。

您也可以使用带有 `--show-cache-node-info` 参数的 `aws elasticache describe-cache-clusters` 命令：

不论您使用什么方法查找集群的终端节点，配置终端节点的地址中始终有 **.cfg**。

**Example 使用查找终端节点 AWS CLI 对于 ElastiCache**  
对于 Linux、macOS 或 Unix：  

```
aws elasticache describe-cache-clusters \
    --cache-cluster-id {{mycluster}} \
    --show-cache-node-info
```
对于 Windows：  

```
aws elasticache describe-cache-clusters ^
    --cache-cluster-id {{mycluster}} ^
    --show-cache-node-info
```
此操作将生成类似于以下内容的输出（JSON 格式）：  

```
{
    "CacheClusters": [
        {
            "Engine": "memcached", 
            "CacheNodes": [
                {
                    "CacheNodeId": "0001", 
                    "Endpoint": {
                        "Port": 11211, 
                        "Address": "mycluster.fnjyzo.cfg.0001.use1.cache.amazonaws.com"
                    }, 
                    "CacheNodeStatus": "available", 
                    "ParameterGroupStatus": "in-sync", 
                    "CacheNodeCreateTime": "2016-10-12T21:39:28.001Z", 
                    "CustomerAvailabilityZone": "us-east-1e"
                }, 
                {
                    "CacheNodeId": "0002", 
                    "Endpoint": {
                        "Port": 11211, 
                        "Address": "mycluster.fnjyzo.cfg.0002.use1.cache.amazonaws.com"
                    }, 
                    "CacheNodeStatus": "available", 
                    "ParameterGroupStatus": "in-sync", 
                    "CacheNodeCreateTime": "2016-10-12T21:39:28.001Z", 
                    "CustomerAvailabilityZone": "us-east-1a"
                }
            ], 
            "CacheParameterGroup": {
                "CacheNodeIdsToReboot": [], 
                "CacheParameterGroupName": "default.memcached1.4", 
                "ParameterApplyStatus": "in-sync"
            }, 
            "CacheClusterId": "mycluster", 
            "PreferredAvailabilityZone": "Multiple", 
            "ConfigurationEndpoint": {
                "Port": 11211, 
                "Address": "mycluster.fnjyzo.cfg.use1.cache.amazonaws.com"
            }, 
            "CacheSecurityGroups": [], 
            "CacheClusterCreateTime": "2016-10-12T21:39:28.001Z", 
            "AutoMinorVersionUpgrade": true, 
            "CacheClusterStatus": "available", 
            "NumCacheNodes": 2, 
            "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:", 
            "CacheSubnetGroupName": "default", 
            "EngineVersion": "1.4.24", 
            "PendingModifiedValues": {}, 
            "PreferredMaintenanceWindow": "sat:06:00-sat:07:00", 
            "CacheNodeType": "cache.r3.large"
        }
    ]
}
```

## 下载集 ElastiCache 群客户端
<a name="AutoDiscovery.Using.ClusterClient"></a>

如要利用 Auto Discovery，客户端程序必须使用 *ElastiCache 集群客户端*。 ElastiCache 集群客户端可用于 Java、PHP 和.NET，包含发现和连接所有缓存节点所需的全部逻辑。

**下载 ElastiCache 群集客户端**

1. 登录 AWS 管理控制台并打开控制 ElastiCache 台，网址为[https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/)。

1. 在 ElastiCache 控制台中，选择**ElastiCache 集群客户端**，然后选择**下载**。

Java 版 ElastiCache 集群客户机的源代码可在以下网址获得[https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java](https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java)。此库是以常见的 Spymemcached 客户端为基础。集 ElastiCache 群客户端根据 Amazon 软件许可发布[https://aws.amazon.com/asl](https://aws.amazon.com/asl)。您可以按照自己的方式自由修改源代码。您甚至可以将代码并入其他开放源 Memcached 库中或者您自己的客户端代码中。

**注意**  
要使用适用于 PHP 的 ElastiCache 集群客户端，您首先需要将其安装在您的 Amazon EC2 实例上。有关更多信息，请参阅 [安装适用于 PHP 的 ElastiCache 集群客户端](Appendix.PHPAutoDiscoverySetup.md)。  
对于支持 TLS 的客户端，请下载 PHP 版本 7.4 或更高版本的二进制文件。  
要使用适用于.NET 的 ElastiCache 集群客户端，您首先需要将其安装在您的 Amazon EC2 实例上。有关更多信息，请参阅 [安装适用于.NET 的 ElastiCache 群集客户端](Appendix.DotNETAutoDiscoverySetup.md)。

## 修改应用程序编程
<a name="AutoDiscovery.Using.ModifyApp"></a>

修改您的应用程序，以便它可以使用 Auto Discovery。以下各节介绍如何使用适用于 Java、PHP 和.NET 的 ElastiCache 群集客户端。

**重要**  
指定集群的配置终端节点时，请确保其地址中有 ".cfg"，如此处所示。请勿使用 CNAME 或其中没有 ".cfg" 的终端节点。  

```
"mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
```
 未明确指定集群的配置终端节点会导致配置到特定节点。