Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Praktik terbaik untuk klien (Valkey dan Redis OSS)
Pelajari praktik terbaik untuk skenario umum dan ikuti contoh kode dari beberapa pustaka klien Valkey dan Redis OSS open source paling populer (redis-py, phPredis, dan Lettuce), serta praktik terbaik untuk berinteraksi dengan sumber daya dengan pustaka klien Memcached open-source yang umum digunakan. ElastiCache
Topik
Mengkonfigurasi protokol pilihan untuk cluster tumpukan ganda (Valkey dan Redis OSS)
Untuk mode cluster diaktifkan Valkey atau Redis OSS cluster, Anda dapat mengontrol klien protokol yang akan digunakan untuk terhubung ke node di cluster dengan parameter IP Discovery. Parameter Penemuan IP dapat diatur ke IPv4 atau IPv6.
Untuk cluster Valkey atau Redis OSS, parameter penemuan IP menetapkan protokol IP yang digunakan dalam slot cluster (), pecahan cluster ()
Perubahan pada Penemuan IP tidak akan mengakibatkan waktu henti untuk klien yang terhubung. Namun, perubahan ini akan memakan waktu untuk disebarkan. Untuk menentukan kapan perubahan telah sepenuhnya disebarkan untuk Cluster Valkey atau Redis OSS, pantau output dari. cluster slots Setelah semua simpul yang ditampilkan oleh perintah slot klaster melaporkan IP dengan protokol baru, berarti perubahan telah selesai disebarkan.
Contoh dengan Redis-Py:
cluster = RedisCluster(host="xxxx", port=6379) target_type = IPv6Address # Or IPv4Address if changing to IPv4 nodes = set() while len(nodes) == 0 or not all((type(ip_address(host)) is target_type) for host in nodes): nodes = set() # This refreshes the cluster topology and will discovery any node updates. # Under the hood it calls cluster slots cluster.nodes_manager.initialize() for node in cluster.get_nodes(): nodes.add(node.host) self.logger.info(nodes) time.sleep(1)
Contoh dengan Lettuce:
RedisClusterClient clusterClient = RedisClusterClient.create(RedisURI.create("xxxx", 6379)); Class targetProtocolType = Inet6Address.class; // Or Inet4Address.class if you're switching to IPv4 Set<String> nodes; do { // Check for any changes in the cluster topology. // Under the hood this calls cluster slots clusterClient.refreshPartitions(); Set<String> nodes = new HashSet<>(); for (RedisClusterNode node : clusterClient.getPartitions().getPartitions()) { nodes.add(node.getUri().getHost()); } Thread.sleep(1000); } while (!nodes.stream().allMatch(node -> { try { return finalTargetProtocolType.isInstance(InetAddress.getByName(node)); } catch (UnknownHostException ignored) {} return false; }));