• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

openResty的Redis模块踩坑记录

武飞扬头像
L小芸
帮助1

OpenResty提供了操作Redis的模块,我们只要引入该模块就能直接使用。说是这样说,但是实践起来好像并不太顺利。

1.设置了密码的redis,lua业务逻辑中需要添加身份认证代码

网上很多资料、文章似乎都是没有设置redis密码,说来也奇怪,哈哈!如果你的redis设置了密码,而lua业务逻辑中却没有加身份认证代码,就会报错。

35200#0: *466 [lua] common.lua:39: read_redis(): Getting key is failed!NOAUTH Authentication required.
2.redis对象的共享问题

这是99%的人都会犯的错误。受Java语言编程的影响,为了更好的复用resty.redis对象,自然而然的会将resty.redis对象的初始化放在lua模块级的变量中供多个请求共享。比如像下面common.lua文件这样:

  1.  
    local redis = require("resty.redis")
  2.  
    local red = redis:new()
  3.  
    red:set_timeouts(1000,1000,1000)
  4.  
     
  5.  
    local function read_redis(ip,port,password,key)
  6.  
     
  7.  
    local ok,err = red:connect(ip,port)
  8.  
    if not ok then
  9.  
    ngx.log(ngx.ERR,"Connecting Redis is failed!",err,",key = ",key)
  10.  
    return nil
  11.  
    end
  12.  
     
  13.  
    local res,err = red:auth(password)
  14.  
    if not res then
  15.  
    ngx.log(ngx.ERR,"Authentication is failed!",err);
  16.  
    return nil
  17.  
    end
  18.  
     
  19.  
    local resp,err = red:get(key)
  20.  
    if not resp then
  21.  
    ngx.log(ngx.ERR,"Getting key is failed!",err,",key = ",key)
  22.  
    end
  23.  
     
  24.  
    if resp==ngx.null then
  25.  
    resp = nil
  26.  
    ngx.log(ngx.ERR, "Redis value is null, key = ", key)
  27.  
    end
  28.  
     
  29.  
    release_redis(red)
  30.  
    return resp
  31.  
    end
学新通

common.lua是封装公共函数的lua模块文件,此时的resty.redis对象会被所有调用read_redis函数的请求所共享。当出现大量请求并发的情况,会有非常多的redis调用失败,而在后台出现大量的bad request错误,导致openResty服务不可用。

35306#0: *504 lua entry thread aborted: runtime error: /usr/local/openresty/lualib/common.lua:30: bad request

官方文档已经明确指出,我们应该始终在函数的局部变量,或ngx.ctx table中初始化resty.redis对象,这些地方对每个请求都有自己的数据副本。

学新通

关于resty.redis对象初始化的描述.png

正确的做法是在函数中初始化resty.redis对象。这里的例子中指的是read_redis函数。

  1.  
    local redis = require("resty.redis")
  2.  
     
  3.  
    local function read_redis(ip,port,password,key)
  4.  
    local red = redis:new()
  5.  
    red:set_timeouts(1000,1000,1000)
  6.  
    local ok,err = red:connect(ip,port)
  7.  
    if not ok then
  8.  
    ngx.log(ngx.ERR,"Connecting Redis is failed!",err,",key = ",key)
  9.  
    return nil
  10.  
    end
  11.  
     
  12.  
    local res,err = red:auth(password)
  13.  
    if not res then
  14.  
    ngx.log(ngx.ERR,"Authentication is failed!",err);
  15.  
    return nil
  16.  
    end
  17.  
     
  18.  
    local resp,err = red:get(key)
  19.  
    if not resp then
  20.  
    ngx.log(ngx.ERR,"Getting key is failed!",err,",key = ",key)
  21.  
    end
  22.  
     
  23.  
    if resp==ngx.null then
  24.  
    resp = nil
  25.  
    ngx.log(ngx.ERR, "Redis value is null, key = ", key)
  26.  
    end
  27.  
     
  28.  
    release_redis(red)
  29.  
    return resp
  30.  
    end
学新通
3.注意lua-resty-redis的版本

由于小编的上述代码中使用了set_timeouts函数,导致后台一直报错。

35829#0: *523 attempt to send data on a closed socket: u:0000000000000000, c:0000000000000000,

查阅了很多资料,最终发现set_timeouts函数是V0.28版本才加进来的。要想查看自己的lua-resty-redis的版本,可以打开lualib下的redis.lua文件查看,具体路径:

/usr/local/openresty/lualib/resty/redis.lua

你将会看到它的版本号。

学新通

lua-resty-redis的版本.png

解决方案:用set_timeout函数替代set_timeouts函数,即可解决。

red:set_timeout(1000)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkkifj
系列文章
更多 icon
同类精品
更多 icon
继续加载