lua 连接redis集群
1.連接redis集群需要用到llua-resty-redis-cluster模塊
github地址:https://github.com/cuiweixie/lua-resty-redis-cluster
下載完成后,只需要用到包中2個文件rediscluster.lua和redis_slot.c
2.將lua-resty-redis-cluster/lib/redis_slot.c 拷貝到 openresty/lualib 目錄下
將lua-resty-redis-cluster/lib/resty/rediscluster.lua 拷貝到 openresty/lualib/resty 目錄下
3.cd 到 openresty/lualib目錄下 執(zhí)行下面命令進(jìn)行編譯
gcc redis_slot.c -fPIC -shared -o libredis_slot.so
問題:如果出現(xiàn)編譯報錯:failed to set…
找不到lua.h等文件
解決:應(yīng)該是lua版本不對,自帶的lua應(yīng)該不好使
方式一:刪除自帶的lua,一般是/usr/lua和/usr/luac ,刪除這兩個文件
方式二:yum install lua-devel 下載一個依賴
方式三:自己重新再lua官網(wǎng)下載一個lua,重新安裝一個lua(這個很好使)
4.在openresty的lualib目錄下 創(chuàng)建文件lua2redis-cluster.lua文件并添加:
local config = {name = "test",serv_list = {{ip="192.168.206.178", port = 7001},{ip="192.168.206.178", port = 7002},{ip="192.168.206.178", port = 7003},{ip="192.168.206.178", port = 7004},{ip="192.168.206.178", port = 7005},{ip="192.168.206.178", port = 7006},}, } local redis_cluster = require "resty.rediscluster" local red = redis_cluster:new(config) for i = 1, 2 dored:init_pipeline()red:set("k1", "hello")red:get("k1")red:set("k2", "world")red:get("k2")local results = red:commit_pipeline()local cjson = require "cjson"ngx.say(cjson.encode(results)) end red:close()5.將上面的lua2redis-cluster.lua 文件路徑添加到 nginx.conf
6.并重啟nginx
nginx -s reload
7.測試:
在redis集群任一節(jié)點(diǎn)都可以 get k1的值 即說明lua連接redis集群成功
總結(jié)
以上是生活随笔為你收集整理的lua 连接redis集群的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杰理之蓝牙连接提示音使能【篇】
- 下一篇: LDF文件步骤及内容详解