ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# Redis客户端 lua-resty-redis是为基于cosocket API的ngx_lua提供的Lua redis客户端,通过它可以完成Redis的操作。默认安装OpenResty时已经自带了该模块,使用文档可参考[https://github.com/openresty/lua-resty-redis](https://github.com/openresty/lua-resty-redis)。 ## 基本操作 #### 1. 创建redis/test_redis_baisc.lua ``` local function close_redes( red ) if not red then return end local ok, err = red:close() if not ok then ngx.say("close redis error:", err) end end local redis = require("resty.redis") -- 创建实例 local red = redis:new() -- 设置超时(毫秒) red:set_timeout(2000) -- 建立连接 local ip = "172.19.73.87" local port = 6379 local ok, err = red:connect(ip, port) if not ok then return end local res, err = red:auth("wsy@123456") if not res then ngx.say("connect to redis error : ", err) return end -- 调用API进行处理 res, err = red:set("msg", "hello world") if not res then ngx.say("set msg error : ", err) return close_redes(red) end -- 调用API获取数据 local resp, err = red:get("msg") if not resp then ngx.say("get msg erro:", err) return close_redes(red) end -- 得到数据为空处理 if resp == ngx.null then resp = '' -- 比如默认值 end ngx.say("msg:", resp) close_redes(red) ``` 基本逻辑很简单,要注意此处判断是否为nil,需要跟ngx.null比较。 #### 2. openResty.conf配置文件 ``` location /lua_redis_basic { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/openResty/lua/redis/test_redis_basic.lua; } ``` 访问如http://127.0.0.1/lua_redis_basic进行测试,正常情况得到如下信息 msg : hello world #### 3. 连接池 建立TCP连接需要三次握手而释放TCP连接需要四次握手,而这些往返时延仅需要一次,以后应该复用TCP连接,此时就可以考虑使用连接池,即连接池可以复用连接。 我们只需要将之前的close_redis函数改造为如下即可: ``` local function close_redes( red ) if not red then return end -- 释放连接(连接池实现) local pool_max_idle_time = 10000 -- 毫秒 local pool_size = 100 --连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.say("set keepalive error : ", err) end end ``` 即设置空闲连接超时时间防止连接一直占用不释放;设置连接池大小来复用连接。 <br/> 此处假设调用red:set_keepalive(),连接池大小通过nginx.conf中http部分的如下指令定义: **默认连接池大小,默认30** lua_socket_pool_size 30; **默认超时时间,默认60s** lua_socket_keepalive_timeout 60s; <br/> **注意:** 1. 连接池是每Worker进程的,而不是每Server的; 2. 当连接超过最大连接池大小时,会按照LRU算法回收空闲连接为新连接使用; 3. 连接池中的空闲连接出现异常时会自动被移除; 4. 连接池是通过ip和port标识的,即相同的ip和port会使用同一个连接池(即使是不同类型的客户端如Redis、Memcached); 5. 连接池第一次set_keepalive时连接池大小就确定下了,不会再变更; 6. cosocket的连接池[http://wiki.nginx.org/HttpLuaModule#tcpsock:setkeepalive](http://wiki.nginx.org/HttpLuaModule#tcpsock:setkeepalive)。 #### 4. pipeline pipeline即管道,可以理解为把多个命令打包然后一起发送;MTU(Maxitum Transmission Unit 最大传输单元)为二层包大小,一般为1500字节;而MSS(Maximum Segment Size 最大报文分段大小)为四层包大小,其一般是1500-20(IP报头)-20(TCP报头)=1460字节;因此假设我们执行的多个Redis命令能在一个报文中传输的话,可以减少网络往返来提高速度。因此可以根据实际情况来选择走pipeline模式将多个命令打包到一个报文发送然后接受响应,而Redis协议也能很简单的识别和解决粘包。 修改之前的代码段 test_pipeline.lua ``` -- local function close_redes( red ) -- if not red then -- return -- end -- local ok, err = red:close() -- if not ok then -- ngx.say("close redis error:", err) -- end -- end local function close_redes( red ) if not red then return end -- 释放连接(连接池实现) local pool_max_idle_time = 10000 -- 毫秒 local pool_size = 100 --连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.say("set keepalive error : ", err) end end local redis = require("resty.redis") -- 创建实例 local red = redis:new() -- 设置超时(毫秒) red:set_timeout(2000) -- 建立连接 local ip = "172.19.73.87" local port = 6379 local ok, err = red:connect(ip, port) if not ok then return end local res, err = red:auth("wsy@123456") if not res then ngx.say("connect to redis error : ", err) return end red:init_pipeline() red:set("msg1", "hello1") red:set("msg2", "hello2") red:get("msg1") red:get("msg2") local respTable, err = red:commit_pipeline() -- 得到数据为空处理 if respTable == ngx.null then respTable = {} end -- 结果是按照执行顺序返回的一个table for i, v in ipairs(respTable) do ngx.say("msg : ", v, "<br/>") end close_redes(red) ``` 通过init_pipeline()初始化,然后通过commit_pipieline()打包提交init_pipeline()之后的Redis命令;返回结果是一个lua table,可以通过ipairs循环获取结果; 配置相应location,测试得到的结果 openResty.conf配置文件 ``` location /lua_redis_pipeline { default_type 'text/html'; lua_code_cache on; content_by_lua_file /usr/openResty/lua/redis/test_pipeline.lua; } ``` msg : OK msg : OK msg : hello1 msg : hello2 <br/> Redis Lua脚本 利用Redis单线程特性,可以通过在Redis中执行Lua脚本实现一些原子操作。如之前的red:get("msg")可以通过如下两种方式实现: 直接eval: ``` local resp, err = red:eval("return redis.call('get', KEYS[1])", 1, "msg"); ``` script load然后evalsha  SHA1 校验和,这样可以节省脚本本身的服务器带宽: ``` local sha1, err = red:script("load",  "return redis.call('get', KEYS[1])");   if not sha1 then      ngx.say("load script error : ", err)   return close_redis(red)   end   ngx.say("sha1 : ", sha1, "")   local resp, err = red:evalsha(sha1, 1, "msg"); ``` 首先通过script load导入脚本并得到一个sha1校验和(仅需第一次导入即可),然后通过evalsha执行sha1校验和即可,这样如果脚本很长通过这种方式可以减少带宽的消耗。 此处仅介绍了最简单的redis lua脚本,更复杂的请参考官方文档学习使用。 另外Redis集群分片算法该客户端没有提供需要自己实现,当然可以考虑直接使用类似于Twemproxy这种中间件实现。