OpenResty技术预研

OpenResty技术预研

近期项目有可能会用到高并发的场景,OpenResty基于Nginx的非阻塞I/O模型,可以很好的实现高性能的Web应用。同时据了解京东、淘宝、百度、去哪儿等许多互联网公司都在使用。

关于OpenResty

OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

安装OpenResty开发环境

linux和mac下的安装步骤具体可以参考OpenResty官方网站中安装部分内容,在这里只介绍通过docker方式搭建OpenResty开发环境。

docker安装方式

获取openresty镜像

1
$ docker pull openresty/openresty

获取的镜像OpenResty的版本是:
1.11.2.2

运行docker容器

1
$ docker run -d --name="openresty" -p 8080:80 openresty/openresty

-p 8080:80将容器的80端口映射到本地的8080端口

浏览器访问http://localhost:8080,可以看到下面的结果说明OpenResty环境正常了
Welcome to OpenResty

停止容器

可以在终端通过如下命令停止容器并删除容器

1
$ docker kill openresty && docker rm openresty

也可以用Kitematic进行管理,更加方便直观

第一个应用 - hello openresty

环境正常了,按照惯例写一个OpenResty的hello world
在本地创建工作目录work,目录结构如下

1
2
3
4
work
|____conf
| |____nginx.conf
|____logs

修改work/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
default_type text/html;
content_by_lua '
ngx.say("<p>hello, openresty</p>")
';
}
}
}

启动docker容器

在终端或执行如下命令

1
$ docker run -d --name="openresty" -p 8080:80 -v /Users/henery/openresty/work/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro -v /Users/henery/openresty/work/logs:/usr/local/openresty/nginx/logs openresty/openresty

说明

将本地文件/Users/henery/openresty/work/conf/nginx.conf映射到容器的/usr/local/openresty/nginx/conf/nginx.conf,相当于替换容器原有的nginx配置文件。
将本地目录/Users/henery/openresty/work/logs映射到容器的/usr/local/openresty/nginx/logs目录,方便查看日志输入。

查看结果

  • 终端命令访问

    1
    2
    $ curl http://localhost:8080
    <p>hello, openresty</p>
  • 通过浏览器访问
    http://localhost:8080
    Hello OpenResty

更新

当修改本地work/conf/nginx.conf文件后需要在容器执行如下命令,重新reload,才能生效

1
$ /usr/local/openresty/nginx/sbin/nginx -s reload

lua代码

为了便于维护,把nginx配置文件中的代码移到lua文件中,在work/conf目录下创建lua子目录用于存放lua源代码文件
work/conf/lua/test.lua

1
ngx.say("hello, openresty");

把nginx.conf改为

1
2
3
4
5
6
...
locaiton / {
default_type text/html;
content_by_lua_file conf/lua/test.lua;
}
...

其中,conf/lua/test.lua相对于nginx的安装目录,此处也可以使用绝对路径/usr/local/openresty/nginx/conf/lua/test.lua

1
docker run -d --name="openresty" -p 8080:80 -v /Users/henery/openresty/work/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro -v /Users/henery/openresty/work/conf/lua:/usr/local/openresty/nginx/conf/lua -v /Users/henery/openresty/work/logs:/usr/local/openresty/nginx/logs openresty/openresty

增加一个映射将本地存放lua代码的目录映射到容器中

lua_code_cache

上面提到了修改了nginx.conf或者lua源文件,只有reload nginx才生效,比较麻烦。
lua_code_cache默认是开启状态,可以在开发阶段通过关闭lua代码缓存的方式lua_code_cache off;,这样每次修改完lua代码,都可以立即看到结果

1
2
3
4
5
6
7
...
location / {
default_type text/html;
lua_code_cache off;
content_by_lua_file conf/lua/test.lua;
}
...

注意,在正式环境下一定要开启缓存
nginx运行时,会提示如下警告信息

1
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:11

优化工作目录

1
2
3
4
5
6
7
8
9
10
11
work
|____conf
| |____nginx.conf --- 公用nginx配置文件
|____logs --- 日志目录
|____test --- 项目目录
| |____lua --- lua源代码目录
| | |____*.lua --- lua源码
| |____lualib --- lua依赖库/第三方依赖库
| | |____*.lua --- lua模块
| | |____*.so --- c模块
| |____test.conf --- 项目自己的nginx配置文件

修改公共nginx.conf文件

work/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
...
http {
...
# lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/local/openresty/nginx下找
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
# 包含项目test的test.conf
include test/test.conf;
...
}
...

json

cjson

1
docker run -d --name="openresty" -p 8080:80 -v /Users/henery/openresty/work/cjson/cjson.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro -v /Users/henery/openresty/work/cjson/lua:/usr/local/openresty/nginx/conf/lua -v /Users/henery/openresty/work/logs:/usr/local/openresty/nginx/logs openresty/openresty

访问mysql

访问redis

参考

坚持原创技术分享,您的支持将鼓励我继续创作!