

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用自動探索
<a name="AutoDiscovery.Using"></a>

若要開始將 Auto Discovery 與 ElastiCache for Memcached 搭配使用，請遵循下列步驟：
+ [取得組態端點](#AutoDiscovery.Using.ConfigEndpoint)
+ [下載 ElastiCache 叢集用戶端](#AutoDiscovery.Using.ClusterClient)
+ [修改您的應用程式](#AutoDiscovery.Using.ModifyApp)

## 取得組態端點
<a name="AutoDiscovery.Using.ConfigEndpoint"></a>

為了連線到叢集，用戶端程式必須知道叢集的組態端點。請參閱主題：[尋找叢集的端點 （主控台） (Memcached)](Endpoints.md#Endpoints.Find.Memcached)

您也可以使用 `aws elasticache describe-cache-clusters` 命令搭配 `--show-cache-node-info` 參數：

無論您用來尋找叢集端點的方法為何，組態端點的地址內都會一律包含 **.cfg**。

**Example 使用 AWS CLI for 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>

若要利用自動探索功能，用戶端程式必須使用 *ElastiCache 叢集用戶端*。ElastiCache 叢集用戶端適用於 Java、PHP 及 .NET，並包含探索及連線到所有快取節點的必要邏輯。

**下載 ElastiCache 叢集用戶端**

1. 登入 AWS 管理主控台，並在 https：//[https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/) 開啟 ElastiCache 主控台。

1. 從 ElastiCache 主控台中，選擇 **ElastiCache Cluster Client (ElastiCache 叢集用戶端)**，然後選擇 **Download (下載)**。

如需適用於 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>

修改您的應用程式以使用自動探索。以下各節示範如何使用適用於 Java、PHP 及 .NET 的 ElastiCache 叢集用戶端。

**重要**  
指定叢集的組態端點時，請務必確認端點的地址內包含 ".cfg"，如以下所示。請不要使用未包含 ".cfg" 的 CNAME 或端點。  

```
"mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
```
 明確指定叢集組態端點失敗，可能會導致設定至特定節點。