OpenResty + lua-resty-http 使用笔记

关于OpenResty 

作者:耿小扭
链接:https://www.zhihu.com/question/266535644/answer/705067582
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

OpenResty解决的是高并发的痛点。现在服务的后台大部分是java写的,但是用java写出稳定的高并发服务是很复杂的一件事,首先是服务器的选择,web服务器有几个选型,tomcat,apache,weblogic,还有商用webphere. 1、tomcat官方宣称的并发量是1000,厉害点的做点参数调优,也不过3000并发,如果要开发一个并发百万的服务,1000000/3000,需要1000台服务器,想想都不可能。 2、apache的并发比tomcat更不堪,200-300 3、weblogic的并发稍好,平均能达到3000左右,但是也没有达到好一个数量级

但是nginx就不一样了,处理几万的请求很轻松,内存占用也不高,之前我们只是把它用作负载均衡,没想过当做一个web服务器,OpenResty的出现解决了享受nginx高并发优势的拦路虎,因为nginx是使用异步 事件模型,跟传统的编程思想不一样,而lua是用c解释执行的脚本语言(执行效率很高),可以用传统的同步编程思想上,在nginx请求接进来后处理稍复杂的逻辑。

对于高并发的系统来说,都是基于内存的,或者说是基于缓存的,题主说的用mysql支撑高并发是不现实的,mysql的并发量在4000-8000,超过这个量mysql性能就会急剧下降。一次内存读取的时间是几十纳秒,一次缓存读取是几毫秒,大家可能对纳秒比较陌生,一纳秒等于1秒的1000000000分之一,一毫秒等于1秒的1000分之一,请求过来之后直接走内存读取,在需要和数据库交互的时候把数据写入内存,然后再批量入库,快速响应。

web流量也符合二八原则,百分之八十的流量集中在百分之二十的页面,比如电商的首页,产品详情页,使用openResty支撑产品详情页的高并发访问,在处理订购单,购物车等环节用其他的高并发框架处理,比如java的NIO网络框架netty。

java的netty也是处理高并发的利器,不过我做过测试,整体性能可以达到nginx的80%,所以,脏活累活都让nginx做吧,关键业务用netty。

 

 


 

 

当前使用服务器
CentOS 8.2 64位

按照官方建议安装流程安装版本

http://openresty.org/cn/installation.html

CentOS

你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum check-update 命令)。 运行下面的命令就可以添加我们的仓库(对于 CentOS 8 或以上版本,应将下面的 yum 都替换成 dnf):

# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/

# update the yum index:
sudo yum check-update

然后就可以像下面这样安装软件包,比如 openresty

sudo yum install -y openresty

 

 


 

新手上路 Hello World

http://openresty.org/cn/getting-started.html

 

1 创建目录

mkdir ~/work
cd ~/work
mkdir logs/ conf/

  

work/conf

work/log

 

2 创建一个 conf/nginx.conf 文件实现输出 helloworld

 

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
    }
}

 

3 配置环境变量 

PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH

  启动nginx

openresty -p `pwd`/ -c conf/nginx.conf

 

4 本地调用http 访问我们的服务

curl http://localhost:8080/

 

成功

 

 

 

补充 openresty 指令

openresty 指令
nginx version: openresty/1.17.8.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/openresty/nginx/)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file

 

  


 

Lua-resty_http

https://github.com/ledgetech/lua-resty-http

lua-resty-http是一个基于Openresty/ngx_lua的HTTP客户端,支持POST方法上传数据。

基本用法是在conf文件中调用lua脚本.

引用文章 https://blog.csdn.net/panguangyuu/article/details/88892330

1 安装 resty.http 库

取得git项目 

git clone https://github.com/ledgetech/lua-resty-http.git

# 直接拷贝文件到 openresty 的 lualib

cd lua-resty-http/lib/resty
cp ./http_headers.lua /usr/local/openresty/lualib/resty/
cp ./http.lua /usr/local/openresty/lualib/resty/

2 使用http

git  请求

local http = require('resty.http')
local httpc = http.new()
local res, err = httpc:request_uri('http://localhost/hello', {
    keepalive_timeout = 2000 -- 毫秒
})

if not res or res.status then
    ngx.log(ngx.ERR, "request error#", err)
    return
end

ngx.log(ngx.ERR, "request status#", res.status)
ngx.say(res.body)

post 请求

local http = require "resty.http"
local httpc = http.new()
local res, err = httpc:request_uri("http://localhost/hello", {
  method = "POST",
  body = "a=1&b=2",
  headers = {
    ["Content-Type"] = "application/x-www-form-urlencoded",
  },
  keepalive_timeout = 60,
  keepalive_pool = 10
})

if not res or res.status then
    ngx.log(ngx.ERR, "request error#", err)
    return
end

ngx.log(ngx.ERR, "request status#", res.status)
ngx.say(res.body)

  



posted @ 2021-11-02 15:35  虚空中的天行者  阅读(2193)  评论(1)    收藏  举报