关于nginx:resty-命令行工具演示

2次阅读

共计 2215 个字符,预计需要花费 6 分钟才能阅读完成。

https://www.bilibili.com/vide…

在本教程中,咱们将演示如何应用 OpenResty 附带的 resty 命令行工具。

cd ~
export PATH=/usr/local/openresty/bin:$PATH
which resty

个别都是这个门路。

咱们能够通过 -V 选项查看其版本号。

resty -V

如果你应用咱们预构建的 Linux 二进制包装置 OpenResty,那么你应该装置 openresty-resty 包。

dnf list installed openresty-resty

因为它不在 openresty 主包中。

比方用 resty 命令来做“hello world”,就容易多了。

resty -e 'print("Hello World")'

留神 -e 选项。

或者在终端上运行一个 Lua 脚本。

echo 'print("Hello World")' > hello.lua
cat hello.lua
resty hello.lua

所以这也是应用 OpenResty 编写新的命令行应用程序的好办法。

这里也能够实现非阻塞 I/O。

time resty -e 'ngx.sleep(1) ngx.say("done")'

让咱们应用 cosocket API 连贯到 openresty.com 的 443 端口。

resty -e 'local sock = ngx.socket.tcp() print(sock:connect("openresty.com", 443))'

或者应用轻线程。

resty -e 'ngx.thread.wait(ngx.thread.spawn(function () print("in thread!") end))'

你也能够很容易地应用 Lua 模块。让咱们创立一个 test 模块。

mkdir lua/
vim lua/test.lua

lua/test.lua 文件是这样的。

local _M = {}

function _M.hello() print("Hello") end

return _M

而后咱们应用 -I 选项将 lua/ 目录增加到 Lua 模块搜寻门路中。

resty -I lua/ -e 'require"test".hello()'

如果没有 -I 选项,它就找不到。

resty -e 'require"test".hello()'

这是因为 lua/ 目录默认不在 Lua 模块的搜寻门路中。

能够间接加载规范的 Lua 模块,比方 resty.shell

resty -e 'local ok, stdout = require"resty.shell".run([[echo ok]]) print(stdout)'

该模块用于非阻塞地运行大量 shell 命令。

咱们也能够通过 --shdict 选项来定义 lua 共享内存字典。

resty --shdict 'dogs 10m' -e 'print(ngx.shared.dogs:set("age", 11))'

能够这样定义多个共享词典。

resty --shdict 'dogs 7m' --shdict 'cats 5m' -e 'print(ngx.shared.dogs," ", ngx.shared.cats)'

它还能够不便地注入自定义的 nginx 配置代码。

resty --http-conf 'lua_regex_match_limit 102400;' -e 'print"ok"'

咱们还能够玩玩 LuaJIT 的 JIT 编译器。

让咱们创立一个跑得比拟热的 Lua 脚本。

echo 'local a = 0 for i = 1, 1e8 do a = a + 1 end print(a)' > bench.lua
cat bench.lua

而后齐全禁用 JIT 编译器。

time resty -joff bench.lua

为了比拟,咱们能够检查一下启用 JIT 编译器后速度有多快。

time resty bench.lua

或者咱们能够用 -jv 选项查看编译后的 Lua 代码门路,或者说“traces”。

resty -jv bench.lua

或者有更多的细节,比方编译后的字节码转储,IR 代码转储,以及机器代码转储。

resty -jdump bench.lua

你能够随时通过 -h 选项找到所有反对的性能。

resty -h

或者通过“restydoc”工具参考其文档。

restydoc resty-cli

如果你通过咱们预建的二进制包装置 openresty,那么你应该装置 openresty-docopenresty-restydoc 包。

dnf list installed openresty-doc

咱们将在另一个专门的视频教程中更深刻地理解“restydoc”工具。
如果你喜爱这个教程,请订阅这个博客网站和咱们的 YouTube 频道 或 B 站频道。谢谢!

<!– xray “ 通过 OpenResty XRay 产品晋升您利用的性能 ” –>

对于本文和关联视频

本文和相关联的视频都是齐全由咱们的 OpenResty Demo 零碎从一个极简略的剧本文件主动生成的。

对于作者

章亦春是开源我的项目 OpenResty® 的创始人,同时也是 OpenResty Inc. 公司的创始人和 CEO。他奉献了许多 Nginx 的第三方模块,相当多 Nginx 和 LuaJIT 外围补丁,并且设计了 OpenResty XRay 等产品。

关注咱们

如果您喜爱本文,欢送关注咱们 OpenResty Inc. 公司的博客网站。也欢送扫码关注咱们的微信公众号:

翻译

咱们提供了英文版原文和中译版(本文)。咱们也欢送读者提供其余语言的翻译版本,只有是全文翻译不带省略,咱们都将会思考采纳,非常感谢!

正文完
 0