在CentOS 6.9 x86_64上玩转OpenResty 1.13.6.1中的resty-cli模块,openrestyresty-cli


resty-cli是OpenResty中命令行工具的集合,其中resty工具是最有用的。

安装好OpenResty之后,resty-cli就会默认安装,以我的安装为例,参见博文

http://blog.csdn.net/tao_627/article/details/78912545

它的位置在/opt/openresty/bin下面



前提条件

OpenResty 1.7.7.2+


配置环境变量

vim /etc/profile

将末尾添加进目录/opt/openresty/bin

export PATH=/opt/openresty/nginx/sbin:/opt/openresty/bin:$PATH

保存退出,然后运行下面的命令生效

source /etc/profile

执行下面的命令检查

echo $PATH



安装依赖库

yum -y install perl-Time-HiRes

原因参见博文

http://blog.csdn.net/tao_627/article/details/78919286
然后我们可以查看resty的路径和版本号

resty -h

resty -v




业务需求
通常我们验证nginx_lua中的脚本,或者调试lua相关代码片段,或者了解ngx.md5之类函数的用法等,只能通过加入nginx.conf中的配置,在nginx框架上玩。
但是这未免比较不便,如果我们有一个命令行工具类似lua, luajit, python等直接执行该多好啊。那么这里的resty就是你想要的工具。

测试样例

下面的几个例子都是来自resty-cli模块的官网

resty -e 'print("hello world")'

time resty -e 'ngx.sleep(3) print("done\n")'

resty -e 'ngx.say(ngx.md5("hello"))'

resty -e 'io.stderr:write("hello world\n")' > /dev/null


更深入的例子及高深玩法有待后续继续挖掘

resty -e 'print("got: ", io.stdin:read("*l"))'


其中taoyunxing是我从键盘输入的内容。

time resty -e 'local ths = {}
                 for i = 1, 3 do
                     ths[i] = ngx.thread.spawn(function ()
                                  ngx.sleep(3) ngx.say("done ", i)
                              end)
                 end
                 for i = 1, #ths do ngx.thread.wait(ths[i]) end'


resty --shdict='dogs 1m' -e 'local dict = ngx.shared.dogs
                               dict:set("Tom", 56)
                               print(dict:get("Tom"))'



参考文献

[1].https://openresty.org/cn/resty-cli.html

[2].https://github.com/openresty/resty-cli#readme

[3].

相关内容

    暂无相关文章