OpenResty 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
目前openresty的组件包括:http://openresty.org/en/components.html
apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
yum install readline-devel pcre-devel openssl-devel gcc
./configure --prefix=/opt/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_postgres_module
make
make install
drwxr-xr-x. 2 root root 4096 9月 1 15:45 bin
drwxr-xr-x. 6 root root 4096 9月 1 15:45 luajit
drwxr-xr-x. 6 root root 4096 9月 1 15:45 lualib
drwxr-xr-x. 11 root root 4096 9月 1 15:46 nginx
drwxr-xr-x. 41 root root 4096 9月 1 15:45 pod
-rw-r--r--. 1 root root 157757 9月 1 15:45 resty.index
drwxr-xr-x. 3 root root 4096 9月 1 15:45 site
lua_package_path "/opt/openresty/lualib/?.lua;;"; #lua 模块
lua_package_cpath "/opt/openresty/lualib/?.so;;"; #c模块
include ../conf/myconf/luatest.conf;
location ~ /openapi/test/.* {
default_type 'text/html';
# content_by_lua 'ngx.say("hello")';
content_by_lua_file conf/myconf/lua/redis.lua;
}
local function close_redis(red)
if not red then
return
end
-- local ok, err = red:close()
-- if not ok then
-- ngx.say("close redis error : ", err)
-- end
--释放连接(连接池实现)
ngx.say("aaa")
local pool_max_idle_time = 10000 --毫秒
local pool_size = 100 --连接池大小
local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
if not ok then
ngx.say("set keepalive error : ", err)
end
end
--
local redis = require("resty.redis")
--创建实例
local red = redis:new()
--设置超时(毫秒
red:set_timeout(1000)
--建立连接
local ip = "192.168.0.43"
local port = 6381
local ok, err = red:connect(ip, port)
--调用API获取数据
local resp, err = red:get("DB_SMS:openresty")
if not resp then
ngx.say("get msg error : ", err)
return close_redis(red)
end
ngx.say(resp)
close_redis(red)
local function close_mysql(db)
if not db then
return
end
--(连接池实现)
local pool_max_idle_time = 10000 --毫秒
local pool_size = 100 --连接池大小
local ok, err = db:set_keepalive(pool_max_idle_time, pool_size)
if not ok then
ngx.say("set mysql keepalive error : ", err)
return
end
end
local function getUrlResourceName(strurl)
local ts = string.reverse(strurl)
local param1, param2 = string.find(ts, "/") -- 这里以"/"为例
local m = string.len(strurl) - param2 + 1
local result = string.sub(strurl, m+1, string.len(strurl))
return result
end
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.say("failed to instantiate mysql: ", err)
return
end
db:set_timeout(1000)
local ok, err, errcode, sqlstate = db:connect{
host = "192.168.0.109",
port = 3306,
database = "platform_qfang",
user = "root",
password = "123456",
max_packet_size = 1024 * 1024 }
if not ok then
ngx.say("failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
local uri= getUrlResourceName(ngx.var.uri)
local res, err, errcode, sqlstate =
db:query("select FName from t_base_position where fid='"..uri.."'",1)
if not res then
ngx.say("bad result: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
--local cjson = require "cjson"
--ngx.say("result,json: ", cjson.encode(res))
local size=table.getn(res)
if size>0 then
ngx.say("result:",res[1].FName)
else
ngx.say("no result")
end
close_mysql(db)