

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# Auto Discovery 사용
<a name="AutoDiscovery.Using"></a>

ElastiCache와 함께 Auto Discovery를 사용하려면 다음 단계를 따르세요.
+ [구성 엔드포인트 확보](#AutoDiscovery.Using.ConfigEndpoint)
+ [ElastiCache 클러스터 클라이언트 다운로드](#AutoDiscovery.Using.ClusterClient)
+ [애플리케이션 프로그램 수정](#AutoDiscovery.Using.ModifyApp)

## 구성 엔드포인트 확보
<a name="AutoDiscovery.Using.ConfigEndpoint"></a>

클라이언트 프로그램은 클러스터에 연결하기 위해 클러스터 구성 Endpoint를 알아야 합니다. [클러스터 엔드포인트 찾기(콘솔)(Memcached)](Endpoints.md#Endpoints.Find.Memcached) 항목을 참조하세요.

또한 다음과 같이 `aws elasticache describe-cache-clusters` 파라미터를 포함하여 `--show-cache-node-info` 명령을 사용할 수 있습니다.

클러스터의 엔드포인트를 찾기 위해 어떤 방법을 사용하든지 구성 엔드포인트는 항상 주소에 **.cfg**를 포함합니다.

**Example ElastiCache용 AWS CLI를 사용하여 엔드포인트 찾기**  
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 관리 콘솔에 로그인하고 [https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/)에서 ElastiCache 콘솔을 엽니다.

1. ElastiCache 콘솔에서 **ElastiCache Cluster Client(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 Software License([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"를 포함해야 합니다. ".cfg"를 포함하지 않은 엔드포인트 또는 CNAME을 사용하지 마십시오.  

```
"mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
```
 클러스터의 구성 엔드포인트를 명시적으로 지정하지 못하면 특정 노드로 구성됩니다.

# Java용 ElastiCache 클러스터 클라이언트 사용
<a name="AutoDiscovery.Using.ModifyApp.Java"></a>

아래 프로그램은 ElastiCache 클러스터 클라이언트를 사용하여 클러스터 구성 엔드포인트에 연결하고 데이터 항목을 캐시에 추가하는 방법을 설명합니다. 프로그램은 Auto Discovery를 사용하여 추가 개입 없이 클러스터에 있는 모든 노드에 연결합니다.

```
package com.amazon.elasticache;

import java.io.IOException;
import java.net.InetSocketAddress;

// Import the &AWS;-provided library with Auto Discovery support 
import net.spy.memcached.MemcachedClient;  

public class AutoDiscoveryDemo {

    public static void main(String[] args) throws IOException {
            
        String configEndpoint = "mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
        Integer clusterPort = 11211;

        MemcachedClient client = new MemcachedClient(
                                 new InetSocketAddress(configEndpoint, 
                                                       clusterPort));       
        // The client will connect to the other cache nodes automatically.

        // Store a data item for an hour.  
        // The client will decide which cache host will store this item. 
        client.set("theKey", 3600, "This is the data value");
    }
}
```

# PHP용 ElastiCache 클러스터 클라이언트 사용
<a name="AutoDiscovery.Using.ModifyApp.PHP"></a>

아래 프로그램은 ElastiCache 클러스터 클라이언트를 사용하여 클러스터 구성 엔드포인트에 연결하고 데이터 항목을 캐시에 추가하는 방법을 설명합니다. 프로그램은 Auto Discovery를 사용하여 추가 개입 없이 클러스터에 있는 모든 노드에 연결합니다.

PHP용 ElastiCache 클러스터 클라이언트를 사용하려면 먼저 클라이언트를 Amazon EC2 인스턴스에 설치해야 합니다. 자세한 내용은 [PHP용 ElastiCache 클러스터 클라이언트 설치](Appendix.PHPAutoDiscoverySetup.md) 섹션을 참조하세요.

```
<?php
	
 /**
  * Sample PHP code to show how to integrate with the Amazon ElastiCache
  * Auto Discovery feature.
  */

  /* Configuration endpoint to use to initialize memcached client. 
   * This is only an example. 	*/
  $server_endpoint = "mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
  
  /* Port for connecting to the ElastiCache cluster. 
   * This is only an example 	*/
  $server_port = 11211;

 /**
  * The following will initialize a Memcached client to utilize the Auto Discovery feature.
  * 
  * By configuring the client with the Dynamic client mode with single endpoint, the
  * client will periodically use the configuration endpoint to retrieve the current cache
  * cluster configuration. This allows scaling the cluster up or down in number of nodes
  * without requiring any changes to the PHP application. 
  *
  * By default the Memcached instances are destroyed at the end of the request. 
  * To create an instance that persists between requests, 
  *    use persistent_id to specify a unique ID for the instance. 
  * All instances created with the same persistent_id will share the same connection. 
  * See [http://php.net/manual/en/memcached.construct.php](http://php.net/manual/en/memcached.construct.php) for more information.
  */
  $dynamic_client = new Memcached('persistent-id');
  $dynamic_client->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);
  $dynamic_client->addServer($server_endpoint, $server_port);
  
  /**
  * Store the data for 60 seconds in the cluster. 
  * The client will decide which cache host will store this item.
  */  
  $dynamic_client->set('key', 'value', 60);  


 /**
  * Configuring the client with Static client mode disables the usage of Auto Discovery
  * and the client operates as it did before the introduction of Auto Discovery. 
  * The user can then add a list of server endpoints.
  */
  $static_client = new Memcached('persistent-id');
  $static_client->setOption(Memcached::OPT_CLIENT_MODE, Memcached::STATIC_CLIENT_MODE);
  $static_client->addServer($server_endpoint, $server_port);

 /**
  * Store the data without expiration. 
  * The client will decide which cache host will store this item.
  */  
  $static_client->set('key', 'value');  
  ?>
```

TLS가 활성화된 ElastiCache 클러스터 클라이언트를 사용하는 방법의 예제는 [PHP 및 Memcached에서 전송 시 암호화 사용](in-transit-encryption.md#in-transit-encryption-connect-php-mc) 섹션을 참조하세요.

# .NET용 ElastiCache 클러스터 클라이언트 사용
<a name="AutoDiscovery.Using.ModifyApp.DotNET"></a>

**참고**  
ElastiCache .NET 클러스터 클라이언트는 2022년 5월부터 사용 중단되었습니다.

ElastiCache용 .NET 클라이언트는 [https://github.com/awslabs/elasticache-cluster-config-net](https://github.com/awslabs/elasticache-cluster-config-net)에 있는 오픈 소스입니다.

 .NET 애플리케이션은 대개 구성 파일에서 구성을 가져옵니다. 다음은 샘플 애플리케이션 구성 파일입니다.

```
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section 
            name="clusterclient" 
            type="Amazon.ElastiCacheCluster.ClusterConfigSettings, Amazon.ElastiCacheCluster" />
    </configSections>

    <clusterclient>
        <!-- the hostname and port values are from step 1 above -->
        <endpoint hostname="mycluster.fnjyzo.cfg.use1.cache.amazonaws.com" port="11211" />
    </clusterclient>
</configuration>
```

아래 C\$1 프로그램은 ElastiCache 클러스터 클라이언트를 사용하여 클러스터 구성 엔드포인트에 연결하고 데이터 항목을 캐시에 추가하는 방법을 설명합니다. 프로그램은 Auto Discovery를 사용하여 추가 개입 없이 클러스터에 있는 모든 노드에 연결합니다.

```
// *****************
// Sample C# code to show how to integrate with the Amazon ElastiCcache Auto Discovery feature.

using System;

using Amazon.ElastiCacheCluster;

using Enyim.Caching;
using Enyim.Caching.Memcached;

public class DotNetAutoDiscoveryDemo  {

    public static void Main(String[] args)  {
    
        // instantiate a new client.
        ElastiCacheClusterConfig config = new ElastiCacheClusterConfig();
        MemcachedClient memClient = new MemcachedClient(config);
        
        // Store the data for 3600 seconds (1hour) in the cluster. 
        // The client will decide which cache host will store this item.
        memClient.Store(StoreMode.Set, 3600, "This is the data value.");
        
    }  // end Main
    
}  // end class DotNetAutoDiscoverDemo
```