微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

http://www.jb51.cc/article/p-zqpfkffm-bro.html

它的位置在/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://www.jb51.cc/article/p-ndykzggf-bro.html
然后我们可以查看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].

原文地址:https://www.jb51.cc/centos/374949.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。