Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Menggunakan Kli ElastiCache en Cluster untuk Java
Program di bawah ini menunjukkan cara menggunakan Klien ElastiCache Cluster untuk terhubung ke titik akhir konfigurasi cluster dan menambahkan item data ke cache. Dengan Penemuan Otomatis, program ini terhubung ke semua simpul dalam klaster tanpa intervensi lebih lanjut.
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 Cache tanpa server selalu mengaktifkan enkripsi dalam transit (TLS). Anda juga dapat mengaktifkan TLS pada cluster berbasis node. Untuk terhubung ke TLS-enabled cache, konfigurasikan klien dengan SSLContext seperti yang ditunjukkan pada contoh berikut.
catatan
setSSLContextMetode ini hanya tersedia di AWS fork spymemcached klien. Untuk sumbernya, lihat aws-elasticache-cluster-client-memcached-for-java
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"); } }
Untuk informasi selengkapnya tentang menghubungkan dengan TLS, lihatMembuat klien Memcached TLS menggunakan Java. Untuk cache Tanpa Server, gunakan titik akhir tanpa server cache pada port 11211. Titik akhir tanpa server tidak berisi “.cfg” di alamat. Untuk cluster berbasis node dengan TLS diaktifkan, gunakan titik akhir konfigurasi yang berisi “.cfg”.