How to java connect redis cluster and read queries to slaves

Posted: Tháng Năm 20, 2021 in Cluster, Redis
Thẻ:, ,

Trong bài viết này, tôi tiếp tục giới thiệu thêm một sample mà cho phép client connect redis cluster và thực hiện read đến slave nodes.

Sử dụng Lettuce-core cho Java redis client để connect redis cluster

Link lettuce-core: https://github.com/lettuce-io/lettuce-core

Một số chế độ read mà Lettuce route đến Redis cluster:

Mặc định Lettuce route thao tác read đến master nodes. Lettuce-core sử dụng ReadFrom để thiết lập cách Lettuce-core route đến các nodes.

SettingDescription
MASTERDefault mode. Read from the current master node.
MASTER_PREFERREDRead from the master, but if it is unavailable, read from replica nodes.
REPLICARead from replica nodes.
REPLICA_PREFERREDRead from the replica nodes, but if none is unavailable, read from the master.
NEARESTRead from any node of the cluster with the lowest latency.

Dựa vào 5 read modes đó, chúng ta có thể chọn mode REPLICA_PREFERRED để ưu tiên read từ các slave nodes và nếu không có slave nodes thì nó sẽ read từ master nodes.

Ví dụ cấu hình kết nối redis cluster với mode read từ slave nodes

package com.keepwalking.redis;

import io.lettuce.core.ReadFrom;
import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;

import java.util.Arrays;

/**
 * Hello world!
 *
 */
public class App {
    public static void main(String[] args) {
        //Connecting to Redis server on localhost
        RedisURI node1 = RedisURI.create("192.168.1.240", 30001);
        RedisURI node2 = RedisURI.create("192.168.1.240", 30002);
        RedisURI node3 = RedisURI.create("192.168.1.240", 30003);
        RedisURI node4 = RedisURI.create("192.168.1.240", 30004);
        RedisURI node5 = RedisURI.create("192.168.1.240", 30005);
        RedisURI node6 = RedisURI.create("192.168.1.240", 30006);

        RedisClusterClient clusterClient = RedisClusterClient.create(Arrays.asList(node1, node2, node3, node4, node5, node6));
        StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
        connection.setReadFrom(ReadFrom.REPLICA_PREFERRED);
        System.out.println("Connected to Redis");

        RedisAdvancedClusterCommands<String, String> sync = connection.sync();
        sync.set("hi1", "keepwalking1");
        sync.set("hi2", "keepwalking2");
        sync.set("hi3", "keepwalking3");

        sync.get("hi1"); // replica read
        sync.get("hi2"); // replica read
        sync.get("hi3"); // replica read
        //sync.get(hi2); // replica read

        connection.close();
        clusterClient.shutdown();
    }
}

Trong đó:

  • 192.168.1.240 với các ports 30001-30006 là địa chỉ các instance của redis cluster. Thay địa chỉ cụm redis cluster phù hợp với thực tế.
  • scaleReads: "all" cấu hình với chế độ read từ các slave instances.

Tôi chạy thử và set với 3 keys hi1,hi2,hi3 và get các keys tương đó.

Tham khảo mẫu tại đây: https://github.com/keepwalking86/redis-cluster/tree/master/examples/java

Bình luận về bài viết này