

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à.

# Uso di Individuazione automatica
<a name="AutoDiscovery.Using"></a>

Per iniziare a utilizzare Auto Discovery with ElastiCache for Memcached, segui questi passaggi:
+ [Ottenete l'endpoint di configurazione](#AutoDiscovery.Using.ConfigEndpoint)
+ [Scarica il ElastiCache Cluster Client](#AutoDiscovery.Using.ClusterClient)
+ [Modifica il tuo programma applicativo](#AutoDiscovery.Using.ModifyApp)

## Ottenete l'endpoint di configurazione
<a name="AutoDiscovery.Using.ConfigEndpoint"></a>

Per connettersi a un cluster, i programmi client devono conoscere l'endpoint di configurazione del cluster. Consulta l'argomento [Individuazione degli endpoint di un cluster (console) (Memcached)](Endpoints.md#Endpoints.Find.Memcached)

Puoi anche utilizzare il comando `aws elasticache describe-cache-clusters` con il parametro `--show-cache-node-info`:

Qualunque sia il metodo utilizzato per trovare gli endpoint del cluster, l'indirizzo dell'endpoint di configurazione conterrà sempre **.cfg**.

**Example Ricerca degli endpoint utilizzando il comando for AWS CLI ElastiCache**  
Per Linux, macOS o Unix:  

```
aws elasticache describe-cache-clusters \
    --cache-cluster-id mycluster \
    --show-cache-node-info
```
Per Windows:  

```
aws elasticache describe-cache-clusters ^
    --cache-cluster-id mycluster ^
    --show-cache-node-info
```
Questa operazione genera un output simile al seguente (formato 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"
        }
    ]
}
```

## Scarica il ElastiCache Cluster Client
<a name="AutoDiscovery.Using.ClusterClient"></a>

Per sfruttare Individuazione automatica, i programmi client devono utilizzare il *Client del cluster ElastiCache*. Il ElastiCache Cluster Client è disponibile per Java, PHP e.NET e contiene tutta la logica necessaria per scoprire e connettersi a tutti i nodi della cache.

**Per scaricare il ElastiCache Cluster Client**

1. Accedi alla console di AWS gestione e apri la ElastiCache console all'indirizzo [https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/).

1. Dalla ElastiCache console, scegli **ElastiCache Cluster Client**, quindi scegli **Scarica**.

Il codice sorgente per ElastiCache Cluster Client for Java è disponibile all'indirizzo [https://github.com/amazonwebservices/aws-elasticache-cluster-client- memcached-for-java](https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java). Questa libreria è basata sul client Spymemcached di uso comune. Il ElastiCache Cluster Client è rilasciato con la licenza Amazon Software [https://aws.amazon.com/asl](https://aws.amazon.com/asl). Sei libero di modificare il codice fonte come meglio credi. Puoi persino incorporare il codice in altre librerie Memcached open source o nel tuo codice client.

**Nota**  
Per utilizzare ElastiCache Cluster Client for PHP, devi prima installarlo sulla tua EC2 istanza Amazon. Per ulteriori informazioni, consulta [Installazione del client ElastiCache cluster per PHP](Appendix.PHPAutoDiscoverySetup.md).  
Per un client con supporto TLS, scarica il file binario con PHP versione 7.4 o superiore.  
Per utilizzare il ElastiCache Cluster Client for .NET, devi prima installarlo sulla tua EC2 istanza Amazon. Per ulteriori informazioni, consulta [Installazione del client ElastiCache cluster per.NET](Appendix.DotNETAutoDiscoverySetup.md).

## Modifica il tuo programma applicativo
<a name="AutoDiscovery.Using.ModifyApp"></a>

Modifica il programma dell'applicazione in modo che utilizzi Individuazione automatica. Le seguenti sezioni mostrano come utilizzare il ElastiCache Cluster Client per Java, PHP e.NET. 

**Importante**  
Durante la specifica dell'endpoint di configurazione del cluster, assicurati che l'indirizzo dell'endpoint contenga ".cfg" come mostrato qui. Non utilizzare un CNAME o un endpoint che non contiene ".cfg" al suo interno.   

```
"mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
```
 La mancata specifica esplicita dell'endpoint di configurazione del cluster determina la configurazione di un nodo specifico.

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

# Utilizzo del ElastiCache Cluster Client per PHP
<a name="AutoDiscovery.Using.ModifyApp.PHP"></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 in un cluster senza ulteriori interventi.

Per utilizzare ElastiCache Cluster Client for PHP, devi prima installarlo sulla tua EC2 istanza Amazon. Per ulteriori informazioni, consulta [Installazione del client ElastiCache cluster per PHP](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');  
  ?>
```

Per un esempio su come utilizzare il ElastiCache Cluster Client con TLS abilitato, consulta [Utilizzo della crittografia in transito con PHP](in-transit-encryption.md#in-transit-encryption-connect-php-mc) e Memcached.

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

**Nota**  
Il client del cluster ElastiCache .NET è obsoleto a maggio 2022.

Il client.NET per ElastiCache è open source all'indirizzo. [https://github.com/awslabs/elasticache-cluster-config-net](https://github.com/awslabs/elasticache-cluster-config-net)

 Le applicazioni .NET in genere recuperano le loro configurazioni dal file config. Di seguito viene riportato un file config dell'applicazione di esempio.

```
<?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>
```

Il programma C\$1 riportato di seguito 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 in un cluster senza ulteriori interventi.

```
// *****************
// 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
```