steve0511 / resty-redis-cluster

Openresty lua client for redis cluster.
Apache License 2.0
373 stars 133 forks source link

Does it support connecting to multiple redis clusters? #89

Closed loongs-zhang closed 3 years ago

loongs-zhang commented 3 years ago

Does it support connecting to multiple redis clusters?

loongs-zhang commented 3 years ago

not work as I supposed, only write to cluster1 ,and here are my code:

package.path = package.path .. ";/Users/admin/Downloads/study/1.19.9.1_1/nginx/conf/lua/redis/?.lua"
local redis_cluster = require("rediscluster")
local config1 = {
    --shared dictionary name for locks, if default value is not used
    dict_name = "redis_cluster_slot_locks",
    --shared dictionary name prefix for lock of each worker, if default value is not used
    refresh_lock_key = "refresh_lock",
    --rediscluster name
    name = "testCluster",
    enable_slave_read = true,
    --redis cluster node list(host and port),
    serv_list = {
        ---集群1
        { ip = "127.0.0.1", port = 7001 },
        { ip = "127.0.0.1", port = 7002 },
        { ip = "127.0.0.1", port = 7003 },
        { ip = "127.0.0.1", port = 7004 },
        { ip = "127.0.0.1", port = 7005 },
        { ip = "127.0.0.1", port = 7006 }
    },
    --redis connection pool idle timeout
    keepalive_timeout = 60000,
    --redis connection pool size
    keepalive_cons = 1000,
    --timeout while connecting
    connect_timeout = 1000,
    --maximum retry attempts for redirection
    max_redirection = 5,
    --maximum retry attempts for connection
    max_connection_attempts = 1
}
local config2 = {
    --shared dictionary name for locks, if default value is not used
    dict_name = "redis_cluster_slot_locks",
    --shared dictionary name prefix for lock of each worker, if default value is not used
    refresh_lock_key = "refresh_lock",
    --rediscluster name
    name = "testCluster",
    enable_slave_read = true,
    --redis cluster node list(host and port),
    serv_list = {
        ---集群2
        { ip = "127.0.0.1", port = 7007 },
        { ip = "127.0.0.1", port = 7008 },
        { ip = "127.0.0.1", port = 7009 },
        { ip = "127.0.0.1", port = 7010 },
        { ip = "127.0.0.1", port = 7011 },
        { ip = "127.0.0.1", port = 7012 }
    },
    --redis connection pool idle timeout
    keepalive_timeout = 60000,
    --redis connection pool size
    keepalive_cons = 1000,
    --timeout while connecting
    connect_timeout = 1000,
    --maximum retry attempts for redirection
    max_redirection = 5,
    --maximum retry attempts for connection
    max_connection_attempts = 1
}

local cluster1 = redis_cluster:new(config1)
local cluster2 = redis_cluster:new(config2)

local clusterMapping = {}
clusterMapping["127.0.0.1:7001"] = cluster1;
clusterMapping["127.0.0.1:7002"] = cluster1;
clusterMapping["127.0.0.1:7003"] = cluster1;
clusterMapping["127.0.0.1:7004"] = cluster1;
clusterMapping["127.0.0.1:7005"] = cluster1;
clusterMapping["127.0.0.1:7006"] = cluster1;

clusterMapping["127.0.0.1:7007"] = cluster2;
clusterMapping["127.0.0.1:7008"] = cluster2;
clusterMapping["127.0.0.1:7009"] = cluster2;
clusterMapping["127.0.0.1:7010"] = cluster2;
clusterMapping["127.0.0.1:7011"] = cluster2;
clusterMapping["127.0.0.1:7012"] = cluster2;

local ipAndPort = "127.0.0.1:7001";
local ipAndPort2 = "127.0.0.1:7007";

ngx.say("cluster1")
ngx.say(tostring(clusterMapping[ipAndPort]))
local value, err = clusterMapping[ipAndPort]:hset("hset", "key", "value")
if err then
    ngx.log(ngx.ERR, "hset err: ", err)
else
    ngx.say(value)
end
local value, err = clusterMapping[ipAndPort]:hgetall("hset")
if err then
    ngx.log(ngx.ERR, "hgetall err: ", err)
else
    for k, v in ipairs(value) do
        ngx.say(k, " ", v)
    end
    ngx.say(value[1], ":", value[2])
end
ngx.say("cluster1")
ngx.say()

ngx.say("cluster2")
ngx.say(tostring(clusterMapping[ipAndPort2]))
local value, err = clusterMapping[ipAndPort2]:hset("hset2", "key2", "value2")
if err then
    ngx.log(ngx.ERR, "hset err: ", err)
else
    ngx.say(value)
end
local value, err = clusterMapping[ipAndPort2]:hgetall("hset2")
if err then
    ngx.log(ngx.ERR, "hgetall err: ", err)
else
    for k, v in ipairs(value) do
        ngx.say(k, " ", v)
    end
    ngx.say(value[1], ":", value[2])
end
ngx.say("cluster2")

local value, err = clusterMapping[ipAndPort2]:hget("hset2", "key2")
if err then
    ngx.log(ngx.ERR, "hget err: ", err)
else
    ngx.say(value)
end
loongs-zhang commented 3 years ago

sorry, when I fix name, everything goes correct.

local config2 = {
    --shared dictionary name for locks, if default value is not used
    dict_name = "redis_cluster_slot_locks",
    --shared dictionary name prefix for lock of each worker, if default value is not used
    refresh_lock_key = "refresh_lock",
    --rediscluster name
    name = "testCluster2",
    enable_slave_read = true,
    --redis cluster node list(host and port),
    serv_list = {
        ---集群2
        { ip = "127.0.0.1", port = 7007 },
        { ip = "127.0.0.1", port = 7008 },
        { ip = "127.0.0.1", port = 7009 },
        { ip = "127.0.0.1", port = 7010 },
        { ip = "127.0.0.1", port = 7011 },
        { ip = "127.0.0.1", port = 7012 }
    },
    --redis connection pool idle timeout
    keepalive_timeout = 60000,
    --redis connection pool size
    keepalive_cons = 1000,
    --timeout while connecting
    connect_timeout = 1000,
    --maximum retry attempts for redirection
    max_redirection = 5,
    --maximum retry attempts for connection
    max_connection_attempts = 1
}