

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

# 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");
    }
}
```

ElastiCache Serverless 캐시에는 항상 전송 중 데이터 암호화(TLS)가 활성화되어 있습니다. 노드 기반 클러스터에서 TLS를 활성화할 수도 있습니다. TLS 지원 캐시에 연결하려면 다음 예제와 같이 SSLContext로 클라이언트를 구성합니다.

**참고**  
메`setSSLContext`서드는 spymemcached 클라이언트의 AWS 포크에서만 사용할 수 있습니다. 소스는 GitHub 웹 [aws-elasticache-cluster-client-memcached-for-java](https://github.com/awslabs/aws-elasticache-cluster-client-memcached-for-java) 사이트의 섹션을 참조하세요. 이 방법은 표준 오픈 소스 spymemcached 라이브러리의 일부가 아닙니다.

```
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.MemcachedClient;
public class TLSDemo {
    public static void main(String[] args) throws Exception {
        ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder();
        // Build SSLContext
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init((KeyStore) null);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        // Create the client in TLS mode
        connectionFactoryBuilder.setSSLContext(sslContext);
        MemcachedClient client = new MemcachedClient(connectionFactoryBuilder.build(), AddrUtil.getAddresses("mycluster.fnjyzo.cfg.use1.cache.amazonaws.com:11211"));

        // Store a data item for an hour.
        client.set("theKey", 3600, "This is the data value");
    }
}
```

TLS 연결에 대한 자세한 내용은 섹션을 참조하세요[Java를 사용하여 TLS Memcached 클라이언트 생성](in-transit-encryption.md#in-transit-encryption-connect-java). 서버리스 캐시의 경우 포트 11211에서 캐시의 서버리스 엔드포인트를 사용합니다. 서버리스 엔드포인트는 주소에 ".cfg"를 포함하지 않습니다. TLS가 활성화된 노드 기반 클러스터의 경우 ".cfg"가 포함된 구성 엔드포인트를 사용합니다.