在lua中执行shell脚本并取得返回结果及lua使用连接池连接redis出现bad request问题

一、在lua中执行shell脚本并取得shell执行返回结果

    lua脚本中可以直接执行shell脚本,常用的方法是os.execute,但是这个方法不能返回执行结果。

    os.execute() : This function is equivalent to the C function system. It passes command to be executed by an operating system shell. It returns a status code, which is system-dependent. If command is absent, then it returns nonzero if a shell is available and zero otherwise

    io.popen()方法可以实现,io.popen()执行命令后返回的是一个文件,然后使用lua中文件的读取命令,取得内容。示例

local rsfile = io.popen('pwd')
local rschar = rsfile:read("*all")
ngx.say(rschar)

    io.popen() : Starts program prog in a separated process and returns a file handle that you can use to read data from this program (if mode is r, the default) or to write data to this program (if mode is w). This function is system dependent and is not available on all platforms. 

    如上即可实现执行shell脚本并打印出来。lua对os,文件还有其它的操作方法截图如下,对操作系统可使用的方法:

    对文件可使用的方法:

二、lua使用nginx连接池连接redis出现bad request

    lua连接redis有一个比较好的方法就是使用nginx的连接池。通过对连接池中的连接的重复利用,避免总是要去进行连接redis消耗时间,从而可以搭建高并发Web应用,外部l请求Nginx,Nginx通过Lua访问Redis,返回json数据。注这里使用的是:(openresty/lua-resty-redis),基本的操作方法如下:

local sock = ngx.socket.tcp()
sock:settimeout(连接超时时间设置值)
sock:connect(redis的IP地址,redis端口)
sock:send(组装后的发送数据)
sock:receive()
sock:setkeepalive(连接持续时间配置值, 连接个数配置大小)  --此条后关键,每次调用后。

    网上已经有雷锋给我们开发好了模块了。https://github.com/openresty/lua-resty-redis#limitations  非常好用。不过对接口进行压力测试时,发现总有非常多的接口调用失败,大量出现bad request 错误。

lua使用nginx连接池连接redis出现bad request(openresty/lua-resty-redis)

    一直找不到原因,而53这行代码正是使用openresty/lua-resty-redis 执行local ok, err = red:connect("127.0.0.1", 6379) 这句连接redis的时候,所幸在网上看到一点提示,然后在github上也看到了作者也说了,

lua使用nginx连接池连接redis出现bad request(openresty/lua-resty-redis)

    即在加载redis模块的时候 ,将值存储在local变量中(ngx.ctx table我没试过),否则会出现很多bad request ,而我正是考虑代码复用,将redis的加载和连接池初始化过程放在了页面初始位置处导致的。经过这样更改后,一切都OK了,单台服务器并发压力测试5000都没有出现数据丢失。足够对付我们的需求了。

相关推荐

  1. Redis使用Lua脚本

    2024-05-13 13:24:03       62 阅读
  2. Redis ,`EVAL` 命令用于执行一段 Lua 脚本

    2024-05-13 13:24:03       36 阅读
  3. 通过lua脚本redis处理json数据

    2024-05-13 13:24:03       63 阅读
  4. Lua脚本Redis的高效应用

    2024-05-13 13:24:03       73 阅读
  5. redis使用lua脚本处理业务逻辑

    2024-05-13 13:24:03       56 阅读
  6. lua脚本的基本语法,以及Redis简单使用

    2024-05-13 13:24:03       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 13:24:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 13:24:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 13:24:03       82 阅读
  4. Python语言-面向对象

    2024-05-13 13:24:03       91 阅读

热门阅读

  1. vue的生命周期

    2024-05-13 13:24:03       32 阅读
  2. day5.12 leetcode80 删除有序数组重复项

    2024-05-13 13:24:03       36 阅读
  3. Leetcode 3148. Maximum Difference Score in a Grid

    2024-05-13 13:24:03       34 阅读
  4. 即将研究生入学,记录一些遇到的疑问

    2024-05-13 13:24:03       28 阅读
  5. linux的Wget命令下载文件示例

    2024-05-13 13:24:03       35 阅读
  6. 如何在Python中自定义异常?

    2024-05-13 13:24:03       32 阅读
  7. JVM调优:JVM常用调优命令和参数

    2024-05-13 13:24:03       31 阅读
  8. LeetCode hot100-33-Y

    2024-05-13 13:24:03       29 阅读
  9. jdk安装使用(Linux)

    2024-05-13 13:24:03       30 阅读
  10. NIUKE SQL:进阶挑战 (上)

    2024-05-13 13:24:03       23 阅读
  11. 开发中遇到SQL IN传入参数的个数超过2100的bug

    2024-05-13 13:24:03       30 阅读