资讯专栏INFORMATION COLUMN

OpenResty debugger: lua-resty-repl

zhonghanwen / 1826人阅读

摘要:根据作者介绍这是一个简单和容易调试运行在的。简单介绍一下这次大会,这次大会的主题是开发,涉及到在前端系统框架集群服务语音云服务智能硬件等方面的实践,以及软件基金会背后的故事。

在2016年第二届 OpenResty 的全球开发者大会上看到了一个比较有意思的项目 lua-resty-repl,后来听闻一些开发者看了项目的介绍后还是觉得一头雾水,不知道怎么使用。这篇文章主要是介绍一下这个项目的使用方法。

根据作者介绍这是一个简单和容易调试运行在 OpenRestylua

安装 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",
   = {
    __index = 
  }
}
[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 gatewayweb 框架、集群服务、语音云服务、智能硬件等方面的实践,以及 OpenResty 软件基金会背后的故事。想要回顾大会优质的视频回放和 PPT,请点击这里查看。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39395.html

相关文章

  • (学习到实践)六、docker自定义nginx/openresty

    摘要:但官方没有发布相关东西,所以以结合安装参考官方的为原则编写。运行测试运行成功,大小,太大了感觉,提交到云端。启动官方镜像提交到云端,偶然想搜索下有没有,竟然反问有官方镜像,了个下来,还不错。 前言 为什么要使用openresty? 官方说明:OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项...

    BingqiChen 评论0 收藏0
  • OpenResty安装、配置与使用

    摘要:用于方便地搭建能够处理超高并发扩展性极高的动态应用服务和动态网关。安装安装依赖库下载及安装激活组件被用于构建。大部组件默认是激活的,也有部件不是。您需要通过以下选项在编译的时候将它们各自激活,和。 OpenResty简介 OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处...

    stackfing 评论0 收藏0
  • 极客时间《OpenResty从入门到实战》 返现福利 + 学习路径图

    摘要:从入门到实战课程大纲摘自极客时间专栏。从入门到实战作者温铭,同时也是软件基金会主席,最佳实践作者。学习路径图参考资料 极客时间《OpenResty从入门到实战》 返现福利 + 学习路径图 showImg(https://segmentfault.com/img/bVbsg9O?w=258&h=258);关注有课学微信公众号,回复暗号 OR 获取购买《OpenResty从入门到实战》极客...

    DataPipeline 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<