在CentOS 6.9 x86_64上玩转OpenResty 1.13.6.1中的resty-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.dogsdict:set("Tom", 56)print(dict:get("Tom"))'
 
 
參考文獻
[1].https://openresty.org/cn/resty-cli.html
[2].https://github.com/openresty/resty-cli#readme
[3].
總結
以上是生活随笔為你收集整理的在CentOS 6.9 x86_64上玩转OpenResty 1.13.6.1中的resty-cli模块的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: OpenResty中遇到Can't lo
- 下一篇: 在CentOS 6.9 x86_64搭建
