

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Utilizzo del ElastiCache Cluster Client per Java
<a name="AutoDiscovery.Using.ModifyApp.Java"></a>

Il programma seguente mostra come utilizzare il ElastiCache Cluster Client per connettersi a un endpoint di configurazione del cluster e aggiungere un elemento di dati alla cache. Utilizzando Individuazione automatica, il programma si connette a tutti i nodi nel cluster senza ulteriori interventi.

```
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 Le cache serverless hanno sempre la crittografia in transito (TLS) abilitata. Puoi anche abilitare TLS sui cluster basati su nodi. Per connettersi a una TLS-enabled cache, configura il client con un SSLContext come mostrato nell'esempio seguente.

**Nota**  
Il `setSSLContext` metodo è disponibile solo nel AWS fork del spymemcached client. Per la fonte, vedi [aws-elasticache-cluster-client-memcached-for-java](https://github.com/awslabs/aws-elasticache-cluster-client-memcached-for-java) sul GitHub sito web. Questo metodo non fa parte della spymemcached libreria open source standard.

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

Per ulteriori informazioni sulla connessione con TLS, consulta. [Creazione di un client TLS Memcached utilizzando Java](in-transit-encryption.md#in-transit-encryption-connect-java) Per una cache serverless, utilizza l'endpoint Serverless della cache sulla porta 11211. L'endpoint Serverless non contiene «.cfg» nell'indirizzo. Per un cluster basato su nodi con TLS abilitato, utilizza l'endpoint di configurazione che contiene «.cfg».