lua開發之--mysql和http模塊

Mysql客戶端

lua-resty-mysql是爲基於cosocket API的ngx_lua提供的Lua Mysql客戶端,通過它可以完成Mysql的操作。默認安裝OpenResty時已經自帶了該模塊,使用文檔可參考https://github.com/openresty/lua-resty-mysql

編輯test_mysql.lua

local function close_db(db)
    if not db then
        return
    end
    db:close()
end

local mysql = require("resty.mysql")
--創建實例
local db, err = mysql:new()
if not db then
    ngx.say("new mysql error : ", err)
    return
end
--設置超時時間(毫秒)
db:set_timeout(1000)

local props = {
    host = "127.0.0.1",
    port = 3306,
    database = "mysql",
    user = "root",
    password = "123456"
}

local res, err, errno, sqlstate = db:connect(props)

if not res then
   ngx.say("connect to mysql error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

--刪除表
local drop_table_sql = "drop table if exists test"
res, err, errno, sqlstate = db:query(drop_table_sql)
if not res then
   ngx.say("drop table error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

--創建表
local create_table_sql = "create table test(id int primary key auto_increment, ch varchar(100))"
res, err, errno, sqlstate = db:query(create_table_sql)
if not res then
   ngx.say("create table error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

--插入
local insert_sql = "insert into test (ch) values('hello')"
res, err, errno, sqlstate = db:query(insert_sql)
if not res then
   ngx.say("insert error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

res, err, errno, sqlstate = db:query(insert_sql)

ngx.say("insert rows : ", res.affected_rows, " , id : ", res.insert_id, "<br/>")

--更新
local update_sql = "update test set ch = 'hello2' where id =" .. res.insert_id
res, err, errno, sqlstate = db:query(update_sql)
if not res then
   ngx.say("update error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

ngx.say("update rows : ", res.affected_rows, "<br/>")
--查詢
local select_sql = "select id, ch from test"
res, err, errno, sqlstate = db:query(select_sql)
if not res then
   ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end


for i, row in ipairs(res) do
   for name, value in pairs(row) do
     ngx.say("select row ", i, " : ", name, " = ", value, "<br/>")
   end
end

ngx.say("<br/>")
--防止sql注入
local ch_param = ngx.req.get_uri_args()["ch"] or ''
--使用ngx.quote_sql_str防止sql注入
local query_sql = "select id, ch from test where ch = " .. ngx.quote_sql_str(ch_param)
res, err, errno, sqlstate = db:query(query_sql)
if not res then
   ngx.say("select error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

for i, row in ipairs(res) do
   for name, value in pairs(row) do
     ngx.say("select row ", i, " : ", name, " = ", value, "<br/>")
   end
end

--刪除
local delete_sql = "delete from test"
res, err, errno, sqlstate = db:query(delete_sql)
if not res then
   ngx.say("delete error : ", err, " , errno : ", errno, " , sqlstate : ", sqlstate)
   return close_db(db)
end

ngx.say("delete rows : ", res.affected_rows, "<br/>")


close_db(db)

對於新增/修改/刪除會返回如下格式的響應:

{
    insert_id = 0,
    server_status = 2,
    warning_count = 1,
    affected_rows = 32,
    message = nil
}

affected_rows表示操作影響的行數,insert_id是在使用自增序列時產生的id。

對於查詢會返回如下格式的響應:

{  
        { id= 1, ch= "hello"},  
        { id= 2, ch= "hello2"}  
    }  

null將返回ngx.null。

example.conf配置文件

     location /lua_mysql {
        default_type 'text/html';
        lua_code_cache on;
        content_by_lua_file /usr/example/lua/test_mysql.lua;
     }

3、訪問如http://192.168.1.2/lua_mysql?ch=hello進行測試,得到如下結果

    insert rows : 1 , id : 2  
    update rows : 1  
    select row 1 : ch = hello  
    select row 1 : id = 1  
    select row 2 : ch = hello2  
    select row 2 : id = 2  
    select row 1 : ch = hello  
    select row 1 : id = 1  
    delete rows : 2  

客戶端目前還沒有提供預編譯SQL支持(即佔位符替換位置變量),這樣在入參時記得使用ngx.quote_sql_str進行字符串轉義,防止sql注入;連接池和之前Redis客戶端完全一樣就不介紹了。

對於Mysql客戶端的介紹基本夠用了,更多請參考https://github.com/openresty/lua-resty-mysql

其他如MongoDB等數據庫的客戶端可以從github上查找使用。

Http客戶端

OpenResty默認沒有提供Http客戶端,需要使用第三方提供;當然我們可以通過ngx.location.capture 去方式實現,但是有一些限制,後邊我們再做介紹。

我們可以從github上搜索相應的客戶端,比如https://github.com/pintsized/lua-resty-http

lua-resty-http

1、下載lua-resty-http客戶端到lualib

cd /usr/example/lualib/resty/  
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua  
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua 

2、test_http_1.lua

local http = require("resty.http")
--創建http客戶端實例
local httpc = http.new()

local resp, err = httpc:request_uri("http://s.taobao.com", {
    method = "GET",
    path = "/search?q=hello",
    headers = {
        ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
    }
})

if not resp then
    ngx.say("request error :", err)
    return
end

--獲取狀態碼
ngx.status = resp.status

--獲取響應頭
for k, v in pairs(resp.headers) do
    if k ~= "Transfer-Encoding" and k ~= "Connection" then
        ngx.header[k] = v
    end
end
--響應體
ngx.say(resp.body)

httpc:close()

響應頭中的Transfer-Encoding和Connection可以忽略,因爲這個數據是當前server輸出的。

3、example.conf配置文件

  location /lua_http_1 {
        default_type 'text/html';
        lua_code_cache on;
        content_by_lua_file /usr/example/lua/test_http_1.lua;
     }

4、在nginx.conf中的http部分添加如下指令來做DNS解析

    resolver 8.8.8.8;  

記得要配置DNS解析器resolver 8.8.8.8,否則域名是無法解析的。
5、訪問如http://192.168.1.2/lua_http_1會看到淘寶的搜索界面。

使用方式比較簡單,如超時和連接池設置和之前Redis客戶端一樣,不再闡述。更多客戶端使用規則請參考https://github.com/pintsized/lua-resty-http

ngx.location.capture

ngx.location.capture也可以用來完成http請求,但是它只能請求到相對於當前nginx服務器的路徑,不能使用之前的絕對路徑進行訪問,但是我們可以配合nginx upstream實現我們想要的功能。

1、在nginx.cong中的http部分添加如下upstream配置

  upstream backend {  
        server s.taobao.com;  
        keepalive 100;  
    }  

即我們將請求upstream到backend;另外記得一定要添加之前的DNS解析器。

2、在example.conf配置如下location

    location ~ /proxy/(.*) {  
       internal;  
       proxy_pass http://backend/$1$is_args$args;  
    }  

internal表示只能內部訪問,即外部無法通過url訪問進來; 並通過proxy_pass將請求轉發到upstream。

3、test_http_2.lua

    local resp = ngx.location.capture("/proxy/search", {  
        method = ngx.HTTP_GET,  
        args = {q = "hello"}  

    })  
    if not resp then  
        ngx.say("request error :", err)  
        return  
    end  
    ngx.log(ngx.ERR, tostring(resp.status))  

    --獲取狀態碼  
    ngx.status = resp.status  

    --獲取響應頭  
    for k, v in pairs(resp.header) do  
        if k ~= "Transfer-Encoding" and k ~= "Connection" then  
            ngx.header[k] = v  
        end  
    end  
    --響應體  
    if resp.body then  
        ngx.say(resp.body)  
    end  

通過ngx.location.capture發送一個子請求,此處因爲是子請求,所有請求頭繼承自當前請求,還有如ngx.ctx和ngx.var是否繼承可以參考官方文檔http://wiki.nginx.org/HttpLuaModule#ngx.location.capture。 另外還提供了ngx.location.capture_multi用於併發發出多個請求,這樣總的響應時間是最慢的一個,批量調用時有用。
4、example.conf配置文件
location /lua_http_2 {
default_type ‘text/html’;
lua_code_cache on;
content_by_lua_file /usr/example/lua/test_http_2.lua;
}
5、訪問如http://192.168.1.2/lua_http_2進行測試可以看到淘寶搜索界面。

我們通過upstream+ngx.location.capture方式雖然麻煩點,但是得到更好的性能和upstream的連接池、負載均衡、故障轉移、proxy cache等特性。

不過因爲繼承在當前請求的請求頭,所以可能會存在一些問題,比較常見的就是gzip壓縮問題,ngx.location.capture不會解壓縮後端服務器的GZIP內容,解決辦法可以參考https://github.com/openresty/lua-nginx-module/issues/12;因爲我們大部分這種http調用的都是內部服務,因此完全可以在proxy location中添加proxy_pass_request_headers off;來不傳遞請求頭

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章