连接redis集群需要用到llua-resty-redis-cluster模块
github地址:https://github.com/cuiweixie/lua-resty-redis-cluster
下载完成后,只需要用到包中2个文件rediscluster.lua和redis_slot.c
.c文件无法在nginx配置文件中引入,需要编译成.so文件,编译命令: gcc SOURCE_FILES -fPIC -shared -o TARGET
如下则是连接redis集群代码:
local config = {
name = "test",
serv_list = {
{ip="127.0.0.1", port = },
{ip="127.0.0.1", port = },
{ip="127.0.0.1", port = },
{ip="127.0.0.1", port = },
{ip="127.0.0.1", port = },
{ip="127.0.0.1", port = },
},
}
local redis_cluster = require "resty.rediscluster"
local red = redis_cluster:new(config)
for i = , do
red:init_pipeline()
red:set("dog", "an animal")
red:get("dog")
red:set("dog", "hello")
red:get("dog")
local results = red:commit_pipeline()
local cjson = require "cjson"
ngx.say(cjson.encode(results))
end
red:close()