OpenResty debugger: lua-resty-repl
文章目录
在2016年第二届 OpenResty 的全球开发者大会上看到了一个比较有意思的项目 lua-resty-repl,后来听闻一些开发者看了项目的介绍后还是觉得一头雾水,不知道怎么使用。这篇文章主要是介绍一下这个项目的使用方法。
根据作者介绍这是一个简单和容易调试运行在 OpenResty 的 lua。
安装 luarocks
根据 官网 介绍,快速开始的姿势如下:
$ wget https://luarocks.org/releases/luarocks-2.4.1.tar.gz
$ tar zxpf luarocks-2.4.1.tar.gz
$ cd luarocks-2.4.1
$ ./configure; sudo make bootstrap
$ sudo luarocks install luasocket
$ lua
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> require "socket"
如果遇到系统上缺少 lualib.h 依赖导致无法正常的通过的,请到 Lua 官网 上先下载好最新的 Lua 源码编译安装解决,或指定 Lua 源码中lualib.h 的目录即可。相信安装 luarocks 过程中各种失败问题大家很容易能解决,这里就不详细展开。
安装 lua-resty-repl
luarocks install lua-resty-repl
创建 nginx.conf
先创建一个 nginx.conf 文件并向文件中写入配置信息:
master_process off; # 关闭 master 进程
error_log stderr notice; # error_log 日志级别是 stderr notice
daemon off; # 关闭守护进程模式,即打开调试模式
events {
worker_connections 1024;
}
http {
server {
listen 8080;
lua_code_cache off;
location / {
content_by_lua_block {
require('resty.repl').start()
}
}
}
}
将上面的 nginx.conf 替换掉 OpenResty 安装后 conf 文件夹中的 nginx.conf 后,启动 OpenResty:
运行 lua-resty-repl
$nginx
Time [alert] #0: lua_code_cache is off; this will hurt performance in nginx.conf
nginx: [alert] lua_code_cache is off; this will hurt performance in nginx.conf
Time [notice] #0: using the "epoll" event method
Time [notice] #0: openresty/1.11.2.1
Time [notice] #0: built by gcc 4.9.2 (Debian 4.9.2-10)
Time [notice] #0: OS: Linux 4.4.0-38-generic
Time [notice] #0: getrlimit(RLIMIT_NOFILE): 65536:65536
可以看到打出一些调试信息,现在我们启动另外一个终端,或者使用 tmux 分屏,在另一个屏上输入:
curl -H X-Header:buz 172.17.0.2:8080?foo=bar
那么你将会在原来的调试信息上看到如下输出内容:
Time [alert] 639#0: lua_code_cache is off; this will hurt performance in nginx.conf
nginx: [alert] lua_code_cache is off; this will hurt performance in nginx.conf
Time [notice] 639#0: using the "epoll" event method
Time [notice] 639#0: openresty/1.11.2.1
Time [notice] 639#0: built by gcc 4.9.2 (Debian 4.9.2-10)
Time [notice] 639#0: OS: Linux 4.4.0-38-generic
Time [notice] 639#0: getrlimit(RLIMIT_NOFILE): 65536:65536
[1] ngx(content)>
在 > 后面写入 ngx.req.get_headers() 和 ngx.req.get_uri_args()之类的命令后可以看到:
Time [alert] 639#0: lua_code_cache is off; this will hurt performance in nginx.conf
nginx: [alert] lua_code_cache is off; this will hurt performance in nginx.conf
Time [notice] 639#0: using the "epoll" event method
Time [notice] 639#0: openresty/1.11.2.1
Time [notice] 639#0: built by gcc 4.9.2 (Debian 4.9.2-10)
Time [notice] 639#0: OS: Linux 4.4.0-38-generic
Time [notice] 639#0: getrlimit(RLIMIT_NOFILE): 65536:65536
[1] ngx(content)> ngx.req.get_headers()
=> {
accept = "*/*",
host = "172.17.0.2:8080",
["user-agent"] = "curl/7.47.0",
["x-header"] = "buz",
<metatable> = {
__index = <function 1>
}
}
[2] ngx(content)> ngx.req.get_uri_args()
=> {
foo = "bar"
}
[3] ngx(content)> ngx.say 'it works!'
=> 1
[4] ngx(content)> ngx.exit(ngx.OK)
172.17.0.1 - - [Time +0000] "GET /?foo=bar HTTP/1.1" 200 20 "-" "curl/7.47.0"
从上面可以看出,当一个请求过来的时候,开始执行我们手动输入的代码,直到遇到 ngx.exit() 才返回结果给客户端。
顺带一提,这次看完在腾讯举办的 第二届 OpenResty 的全球开发者大会 后觉得很赞,干货满满的,还有 OpenResty 创始人 agentzh 和几位牛逼的讲师在大会上出色演讲和精彩答疑,确实让大家收获很大。
简单介绍一下这次大会,这次 OpenResty 大会的主题是 web 开发,涉及到 OpenResty 在前端系统、API gateway、 web 框架、集群服务、语音云服务、智能硬件等方面的实践,以及 OpenResty 软件基金会背后的故事。想要回顾大会优质的视频回放和 PPT,请点击这里查看。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论