클라이언트 모범 사례(Valkey 및 Redis OSS) - Amazon ElastiCache

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

클라이언트 모범 사례(Valkey 및 Redis OSS)

일반적인 시나리오의 모범 사례를 알아보고 가장 인기 있는 오픈 소스 Valkey 및 Redis OSS 클라이언트 라이브러리(redis-py, PHPRedis 및 Lettuce)의 코드 예제와 함께 일반적으로 사용되는 오픈 소스 Memcached 클라이언트 라이브러리를 사용하여 ElastiCache 리소스와 상호 작용하기 위한 모범 사례를 따르세요.

듀얼 스택 클러스터에 선호되는 프로토콜 구성(Valkey 및 Redis OSS)

클러스터 모드가 활성화된 Valkey 또는 Redis OSS 클러스터의 경우, 클라이언트가 클러스터 내 노드에 연결하는 데 사용할 프로토콜을 IP Discovery 파라미터로 제어할 수 있습니다. IP Discovery 파라미터는 IPv4 또는 IPv6로 설정할 수 있습니다.

Valkey 또는 Redis OSS 클러스터의 경우, IP Discovery 파라미터가 cluster slots (), cluster shards (), cluster nodes () 출력에 사용되는 IP 프로토콜을 설정합니다. 이러한 명령은 클라이언트가 클러스터 토폴로지를 검색하는 데 사용됩니다. 클라이언트는 이들 명령 내의 IP를 사용하여 클러스터 내 다른 노드에 연결합니다.

IP Discovery를 변경해도 연결된 클라이언트는 가동 중지되지 않습니다. 하지만 변경 사항이 전파되려면 다소 시간이 소요됩니다. Valkey 또는 Redis OSS 클러스터에 변경 사항이 완전히 전파된 시점을 확인하려면 cluster slots의 출력을 모니터링하면 됩니다. 클러스터 슬롯 명령에서 반환된 모든 노드가 새 프로토콜이 있는 IP를 보고하면, 변경 사항이 완전히 전파된 것입니다.

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)

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