location /postgres {
internal;
default_type text/html;
set_by_lua $query_sql 'return ngx.unescape_uri(ngx.var.arg_sql)';
postgres_pass pg_server;
rds_json on;
rds_json_buffer_size 16k;
postgres_query $query_sql;
postgres_connect_timeout 1s;
postgres_result_timeout 2s;
}
这里有很多指令要素:
这样的配置就完成了初步的可以提供其他 location 调用的 location 了。但这里还差一个配置没说明白,就是这一行:
postgres_pass pg_server;
其实这一行引入了 名叫 pg_server 的 upstream 块,其定义应该像如下:
upstream pg_server {
postgres_server 192.168.1.2:5432 dbname=pg_database
user=postgres password=postgres;
postgres_keepalive max=800 mode=single overflow=reject;
}
这里有一些指令要素:
postgres_server 这个指令是必须带的,但可以配置多个,用于配置服务器连接参数,可以分解成若干参数:
password 是账号名称对应的密码。
postgres_keepalive 这个指令用于配置长连接连接池参数,长连接连接池有利于提高通讯效率,可以分解为若干参数:
overflow 是当长连接数量到达 max 之后的处理方案,有 ignore 和 reject 两种值。
这样就构成了我们 PostgreSQL 后端通讯的通用 location,在使用 lua 业务编码的过程中可以直接使用如下代码连接数据库(折腾了这么老半天):
local json = require "cjson"
function test()
local res = ngx.location.capture('/postgres',
{ args = {sql = "SELECT * FROM test" } }
)
local status = res.status
local body = json.decode(res.body)
if status == 200 then
status = true
else
status = false
end
return status, body
end
先来看一下lua-resty-mysql
模块的调用示例代码。
# you do not need the following line if you are using
# the ngx_openresty bundle:
lua_package_path "/path/to/lua-resty-mysql/lib/?.lua;;";
server {
location /test {
content_by_lua '
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) -- 1 sec
local ok, err, errno, sqlstate = db:connect{
host = "127.0.0.1",
port = 3306,
database = "ngx_test",
user = "ngx_test",
password = "ngx_test",
max_packet_size = 1024 * 1024 }
if not ok then
ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
return
end
ngx.say("connected to mysql.")
-- run a select query, expected about 10 rows in
-- the result set:
res, err, errno, sqlstate =
db:query("select * from cats order by id asc", 10)
if not res then
ngx.say("bad result: ", err, ": ", errno, ": ", sqlstate, ".")
return
end
local cjson = require "cjson"
ngx.say("result: ", cjson.encode(res))
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
';
}
}
看过这段代码,大家肯定会说:这才是我熟悉的,我想要的。为什么刚刚ngx_postgres
模块的调用这么诡异,配置那么复杂,其实这是发展历史造成的。ngx_postgres
起步比较早,当时OpenResty
也还没开始流行,所以更多的 Nginx 数据库都是以 ngx_c_module 方式存在。有了OpenResty
,才让我们具有了使用完整的语言来描述我们业务能力。
后面我们会单独说一说使用ngx_c_module
的各种不方便,也就是我们所踩过的坑。希望能给大家一个警示,能转到lua-resty-***
这个方向的,就千万不要和ngx_c_module
玩,ngx_c_module
的扩展性、可维护性、升级等各方面都没有lua-resty-***
好。
这绝对是经验的总结。不服来辩!