

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 自動検出の使用
<a name="AutoDiscovery.Using"></a>

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 Cluster Client は、Java、PHP、および .NET 向けが用意されており、すべてのキャッシュノードを検出して接続するのに必要なロジックすべてが含まれています。

**ElastiCache クラスタークライアントをダウンロードするには**

1. AWS マネジメントコンソールにサインインして、Amazon ElastiCache コンソール ([https://console.aws.amazon.com/elasticache/](https://console.aws.amazon.com/elasticache/)) を開きます。

1. ElastiCache コンソールで、[**ElastiCache Cluster Client**] を選択して [**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 インスタンスにインストールする必要があります。詳細については、「[ElastiCache Cluster Client for PHP のインストール](Appendix.PHPAutoDiscoverySetup.md)」を参照してください。  
TLS がサポートされているクライアントの場合は、PHP バージョン 7.4 以上のバイナリをダウンロードします。  
.NET 向けの ElastiCache Cluster Client を使用するには、まず Amazon EC2 インスタンスにインストールする必要があります。詳細については、「[ElastiCache Cluster Client for .NET のインストール](Appendix.DotNETAutoDiscoverySetup.md)」を参照してください。

## アプリケーションプログラムを変更する
<a name="AutoDiscovery.Using.ModifyApp"></a>

自動検出を使用するようにアプリケーションプログラムを変更する準備ができました。以下のセクションでは、Java、PHP、および .NET 向けの ElastiCache Cluster Client を使用する方法を示します。

**重要**  
クラスターの設定エンドポイントを指定する際は必ず、ここに示す設定エンドポイントのアドレスに「.cfg」が含まれていることを確認してください。「.cfg」のない CNAME またはエンドポイントは使用しないでください。  

```
"mycluster.fnjyzo.cfg.use1.cache.amazonaws.com";
```
 クラスターの設定エンドポイントを明示的に指定しない場合は、特定のノードが設定されます。

# Java 向けの ElastiCache クラスタークライアントの使用
<a name="AutoDiscovery.Using.ModifyApp.Java"></a>

以下のプログラムは、ElastiCache クラスタークライアントを使用してクラスター設定エンドポイントに接続し、キャッシュにデータ項目を追加する方法を示しています。さらに操作を行わなくても、プログラムは自動検出を使用してクラスター内のすべてのノードに接続します。

```
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 クラスタークライアントを使用してクラスター設定エンドポイントに接続し、キャッシュにデータ項目を追加する方法を示しています。さらに操作を行わなくても、プログラムは自動検出を使用してクラスター内のすべてのノードに接続します。

PHP 向けの ElastiCache クラスタークライアントを使用するには、まず Amazon EC2 インスタンスにインストールする必要があります。詳細については、[ElastiCache Cluster Client for 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');  
  ?>
```

TLS を有効にして ElastiCache Cluster Client を使用する方法の例については、「[Using in transit encryption with PHP and Memcached](in-transit-encryption.md#in-transit-encryption-connect-php-mc)」を参照してください。

# ElastiCache Cluster Client for .NET の使用
<a name="AutoDiscovery.Using.ModifyApp.DotNET"></a>

**注記**  
ElastiCache .NET Cluster Client は 2022 年 5 月に廃止されました。

ElastiCache の .NET クライアントは、オープンソースとして [https://github.com/awslabs/elasticache-cluster-config-net](https://github.com/awslabs/elasticache-cluster-config-net) から入手できます。

 .NET アプリケーションは、通常、config ファイルから設定を取得します。サンプルアプリケーションの config ファイルを以下に示します。

```
<?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 Cluster Client を使用してクラスター設定エンドポイントに接続し、キャッシュにデータ項目を追加する方法を示しています。さらに操作を行わなくても、プログラムは自動検出を使用してクラスター内のすべてのノードに接続します。

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