http://lua.ren/topic/135/%E5%85%B3%E4%BA%8E-openresty-%E7%9A%84%E4%B8%A4%E4%B8%89%E4%BA%8B
基礎(chǔ)原理 Nginx 采用的是 master-worker 模型,一個 master 進(jìn)程管理多個 worker 進(jìn)程,基本的事件處理都是放在 woker 中,master 負(fù)責(zé)一些全局初始化,以及對 worker 的管理。
每個 woker 使用一個 LuaVM,當(dāng)請求被分配到 woker 時,將在這個 LuaVM 里創(chuàng)建一個 coroutine。協(xié)程之間數(shù)據(jù)隔離,每個協(xié)程具有獨(dú)立的全局變量 _G。
關(guān)于 LUACODECACHE 關(guān)閉 luacodecache 時,require 的處理方式是每次都強(qiáng)制重新加載和解析,也就是說,你對代碼的任何修改的效果,都將在上傳后立即體現(xiàn)。
開啟 luacodecache 時,在同一個 LuaVM 中,模塊將在首次加載并解析后被緩存,之后再次 require 將直接返回緩存的內(nèi)容。換句話說,同一 worker 上的所有請求將共享已加載的模塊,任意一個請求對于模塊屬性的修改,都將影響到同一 worker 上的其他請求。
不應(yīng)使用模塊級的局部變量以及模塊屬性,存放任何請求級的數(shù)據(jù)。否則在 luacodecache 開啟時,會造成請求間相互影響和數(shù)據(jù)競爭,產(chǎn)生不可預(yù)知的異常狀況。
關(guān)閉 luacodecache 會極大的降低性能,在生產(chǎn)環(huán)境中應(yīng)開啟 luacodecache 。
雖然開發(fā)環(huán)境中關(guān)閉 luacodecache 會有一些便利性,但我強(qiáng)烈建議開啟 luacodecache ,與線上保持一致,以減少不必要的差異性問題和額外測試需求。
開啟 luacodecache 時,可用 nginx -s reload 或 kill -HUP masterPID 方式熱重載代碼,無需重啟 Nginx。
關(guān)于 PATH 和 CPATH OpenResty 會將它的 lib 目錄加入 package.path 和 package.cpath,但你的項目目錄需要自己處理。
在入口文件中,將項目目錄加入 package.path 和 package.cpath 是不可取的。因為 luacodecache 開啟時,package 模塊是同一 worker 上所有請求共享的,如果無條件追加,package.path 和 package.cpath 將不斷變長,并最終導(dǎo)致內(nèi)存溢出。
以下是我采用的解決方案:
local ok, app = pcall(require, "core.app")
if ok then
app:run()
else
local rootPath = ngx.var.document_root
if not (package.path:find(rootPath)) then
package.path = package.path .. ";" .. rootPath .. "/?.lua;;"
end
if not (package.cpath:find(rootPath)) then
package.cpath = package.cpath .. ";" .. rootPath .. "/?.so;;"
end
require("core.app"):run()
end
關(guān)于 LUA-RESTY-MYSQL 和 LUA-RESTY-REDIS 不應(yīng)使用模塊級的局部變量以及模塊屬性,存放 resty.mysql 和 resty.redis 實(shí)例。否則,在 luacodecache 開啟時,同一 worker 的所有請求將共享該實(shí)例,造成數(shù)據(jù)競爭問題。建議將 resty.mysql 和 resty.redis 實(shí)例存放到 ngx.ctx 中。不能在 require 過程中實(shí)例化 resty.mysql 和 resty.redis 實(shí)例,否則會報錯。例如,模塊返回一個 function,此 function 直接或間接調(diào)用實(shí)例化 resty.mysql 和 resty.redis 的代碼,將會導(dǎo)致報錯。
在首次查詢時實(shí)例化是一個比較好的解決方案:
local mysql = require("resty.mysql")
local exception = require("core.exception")
local dbConf = require("config.mysql")
local sysConf = require("config.system")
local MySQL = {}
--- 獲取連接
--
-- @return resty.mysql MySQL連接
-- @error mysql.socketFailed socket建立失敗
-- @error mysql.cantConnect 無法連接數(shù)據(jù)庫
-- @error mysql.queryFailed 數(shù)據(jù)查詢失敗
function MySQL:getClient()
if ngx.ctx[MySQL] then
return ngx.ctx[MySQL]
end
local client, errmsg = mysql:new()
if not client then
exception:raise("mysql.socketFailed", { message = errmsg })
end
client:set_timeout(3000)
local options = {
user = dbConf.USER,
password = dbConf.PASSWORD,
database = dbConf.DATABASE
}
if dbConf.SOCK then
options.path = dbConf.SOCK
else
options.host = dbConf.HOST
options.port = dbConf.PORT
end
local result, errmsg, errno, sqlstate = client:connect(options)
if not result then
exception:raise("mysql.cantConnect", {
message = errmsg,
code = errno,
state = sqlstate
})
end
local query = "SET NAMES " .. sysConf.DEFAULT_CHARSET
local result, errmsg, errno, sqlstate = client:query(query)
if not result then
exception:raise("mysql.queryFailed", {
query = query,
message = errmsg,
code = errno,
state = sqlstate
})
end
ngx.ctx[MySQL] = client
return ngx.ctx[MySQL]
end
--- 關(guān)閉連接
function MySQL:close()
if ngx.ctx[MySQL] then
ngx.ctx[MySQL]:set_keepalive(0, 100)
ngx.ctx[MySQL] = nil
end
end
--- 執(zhí)行查詢
--
-- 有結(jié)果數(shù)據(jù)集時返回結(jié)果數(shù)據(jù)集
-- 無數(shù)據(jù)數(shù)據(jù)集時返回查詢影響,如:
-- { insert_id = 0, server_status = 2, warning_count = 1, affected_rows = 32, message = nil}
--
-- @param string query 查詢語句
-- @return table 查詢結(jié)果
-- @error mysql.queryFailed 查詢失敗
function MySQL:query(query)
local result, errmsg, errno, sqlstate = self:getClient():query(query)
if not result then
exception:raise("mysql.queryFailed", {
query = query,
message = errmsg,
code = errno,
state = sqlstate
})
end
return result
end
return MySQL
使用 setkeepalive(maxidletimeout, poolsize) 替代 close() 將啟用連接池特性。set_keepalive 的意思可以理解為,保持連接,并將連接歸還到連接池內(nèi)。這樣在下次連接時,會首先會嘗試從連接池獲取連接,獲取不成功才會創(chuàng)建新的連接。在高并發(fā)下,連接池能大大的減少連接 MySQL 和 Redis 的次數(shù),明顯的提升性能。使用模塊緩存靜態(tài)數(shù)據(jù) 利用 luacodecache 開啟時模塊會被緩存的特性,我們可以使用模塊來緩存靜態(tài)數(shù)據(jù),其效率接近于將數(shù)據(jù)緩存在內(nèi)存中。
存儲方法:
local exception = require("core.exception")
local mysql = require("core.driver.mysql")
--- 實(shí)現(xiàn)示例,可以根據(jù)項目情況,完善后封裝在數(shù)據(jù)查詢層
local function makeCityCache()
local citys = mysql:query("SELECT * FROM `data_city` WHERE 1")
local cityData = {}
for _, city in ipairs(citys) do
cityData[city.id] = city
end
package.loaded["cache.city"] = cityData
end
讀取方法:
--- 實(shí)現(xiàn)示例,可以根據(jù)項目情況,完善后封裝在數(shù)據(jù)查詢層
local function getCityCache(id)
local ok, cacheData = pcall(require, "cache.city")
if ok then
return cacheData[id]
end
return nil
end
清理方法:
--- 實(shí)現(xiàn)示例,可以根據(jù)項目情況,完善后封裝在數(shù)據(jù)查詢層
local function clearCityCache()
package.loaded["cache.city"] = nil
end
關(guān)于 OPENRESTY 的兩三事 火星梅梅 | 5 八月, 2013 | OpenResty, 愛 Coding | 2條評論 基礎(chǔ)原理 Nginx 采用的是 master-worker 模型,一個 master 進(jìn)程管理多個 worker 進(jìn)程,基本的事件處理都是放在 woker 中,master 負(fù)責(zé)一些全局初始化,以及對 worker 的管理。每個 woker 使用一個 LuaVM,當(dāng)請求被分配到 woker 時,將在這個 LuaVM 里創(chuàng)建一個 coroutine。協(xié)程之間數(shù)據(jù)隔離,每個協(xié)程具有獨(dú)立的全局變量 _G。
關(guān)于 LUACODECACHE 關(guān)閉 luacodecache 時,require 的處理方式是每次都強(qiáng)制重新加載和解析,也就是說,你對代碼的任何修改的效果,都將在上傳后立即體現(xiàn)。
開啟 luacodecache 時,在同一個 LuaVM 中,模塊將在首次加載并解析后被緩存,之后再次 require 將直接返回緩存的內(nèi)容。換句話說,同一 worker 上的所有請求將共享已加載的模塊,任意一個請求對于模塊屬性的修改,都將影響到同一 worker 上的其他請求。
不應(yīng)使用模塊級的局部變量以及模塊屬性,存放任何請求級的數(shù)據(jù)。否則在 luacodecache 開啟時,會造成請求間相互影響和數(shù)據(jù)競爭,產(chǎn)生不可預(yù)知的異常狀況。
關(guān)閉 luacodecache 會極大的降低性能,在生產(chǎn)環(huán)境中應(yīng)開啟 luacodecache 。
雖然開發(fā)環(huán)境中關(guān)閉 luacodecache 會有一些便利性,但我強(qiáng)烈建議開啟 luacodecache ,與線上保持一致,以減少不必要的差異性問題和額外測試需求。
開啟 luacodecache 時,可用 nginx -s reload 或 kill -HUP masterPID 方式熱重載代碼,無需重啟 Nginx。
關(guān)于 PATH 和 CPATH OpenResty 會將它的 lib 目錄加入 package.path 和 package.cpath,但你的項目目錄需要自己處理。
在入口文件中,將項目目錄加入 package.path 和 package.cpath 是不可取的。因為 luacodecache 開啟時,package 模塊是同一 worker 上所有請求共享的,如果無條件追加,package.path 和 package.cpath 將不斷變長,并最終導(dǎo)致內(nèi)存溢出。
以下是我采用的解決方案:
local ok, app = pcall(require, "core.app")
if ok then
app:run()
else
local rootPath = ngx.var.document_root
if not (package.path:find(rootPath)) then
package.path = package.path .. ";" .. rootPath .. "/?.lua;;"
end
if not (package.cpath:find(rootPath)) then
package.cpath = package.cpath .. ";" .. rootPath .. "/?.so;;"
end
require("core.app"):run()
end
關(guān)于 LUA-RESTY-MYSQL 和 LUA-RESTY-REDIS 不應(yīng)使用模塊級的局部變量以及模塊屬性,存放 resty.mysql 和 resty.redis 實(shí)例。否則,在 luacodecache 開啟時,同一 worker 的所有請求將共享該實(shí)例,造成數(shù)據(jù)競爭問題。建議將 resty.mysql 和 resty.redis 實(shí)例存放到 ngx.ctx 中。不能在 require 過程中實(shí)例化 resty.mysql 和 resty.redis 實(shí)例,否則會報錯。例如,模塊返回一個 function,此 function 直接或間接調(diào)用實(shí)例化 resty.mysql 和 resty.redis 的代碼,將會導(dǎo)致報錯。
在首次查詢時實(shí)例化是一個比較好的解決方案:
local mysql = require("resty.mysql")
local exception = require("core.exception")
local dbConf = require("config.mysql")
local sysConf = require("config.system")
local MySQL = {}
--- 獲取連接
--
-- @return resty.mysql MySQL連接
-- @error mysql.socketFailed socket建立失敗
-- @error mysql.cantConnect 無法連接數(shù)據(jù)庫
-- @error mysql.queryFailed 數(shù)據(jù)查詢失敗
function MySQL:getClient()
if ngx.ctx[MySQL] then
return ngx.ctx[MySQL]
end
local client, errmsg = mysql:new()
if not client then
exception:raise("mysql.socketFailed", { message = errmsg })
end
client:set_timeout(3000)
local options = {
user = dbConf.USER,
password = dbConf.PASSWORD,
database = dbConf.DATABASE
}
if dbConf.SOCK then
options.path = dbConf.SOCK
else
options.host = dbConf.HOST
options.port = dbConf.PORT
end
local result, errmsg, errno, sqlstate = client:connect(options)
if not result then
exception:raise("mysql.cantConnect", {
message = errmsg,
code = errno,
state = sqlstate
})
end
local query = "SET NAMES " .. sysConf.DEFAULT_CHARSET
local result, errmsg, errno, sqlstate = client:query(query)
if not result then
exception:raise("mysql.queryFailed", {
query = query,
message = errmsg,
code = errno,
state = sqlstate
})
end
ngx.ctx[MySQL] = client
return ngx.ctx[MySQL]
end
--- 關(guān)閉連接
function MySQL:close()
if ngx.ctx[MySQL] then
ngx.ctx[MySQL]:set_keepalive(0, 100)
ngx.ctx[MySQL] = nil
end
end
--- 執(zhí)行查詢
--
-- 有結(jié)果數(shù)據(jù)集時返回結(jié)果數(shù)據(jù)集
-- 無數(shù)據(jù)數(shù)據(jù)集時返回查詢影響,如:
-- { insert_id = 0, server_status = 2, warning_count = 1, affected_rows = 32, message = nil}
--
-- @param string query 查詢語句
-- @return table 查詢結(jié)果
-- @error mysql.queryFailed 查詢失敗
function MySQL:query(query)
local result, errmsg, errno, sqlstate = self:getClient():query(query)
if not result then
exception:raise("mysql.queryFailed", {
query = query,
message = errmsg,
code = errno,
state = sqlstate
})
end
return result
end
return MySQL
使用 setkeepalive(maxidletimeout, poolsize) 替代 close() 將啟用連接池特性。set_keepalive 的意思可以理解為,保持連接,并將連接歸還到連接池內(nèi)。這樣在下次連接時,會首先會嘗試從連接池獲取連接,獲取不成功才會創(chuàng)建新的連接。在高并發(fā)下,連接池能大大的減少連接 MySQL 和 Redis 的次數(shù),明顯的提升性能。使用模塊緩存靜態(tài)數(shù)據(jù) 利用 luacodecache 開啟時模塊會被緩存的特性,我們可以使用模塊來緩存靜態(tài)數(shù)據(jù),其效率接近于將數(shù)據(jù)緩存在內(nèi)存中。
存儲方法:
local exception = require("core.exception")
local mysql = require("core.driver.mysql")
--- 實(shí)現(xiàn)示例,可以根據(jù)項目情況,完善后封裝在數(shù)據(jù)查詢層
local function makeCityCache()
local citys = mysql:query("SELECT * FROM `data_city` WHERE 1")
local cityData = {}
for _, city in ipairs(citys) do
cityData[city.id] = city
end
package.loaded["cache.city"] = cityData
end
讀取方法:
--- 實(shí)現(xiàn)示例,可以根據(jù)項目情況,完善后封裝在數(shù)據(jù)查詢層
local function getCityCache(id)
local ok, cacheData = pcall(require, "cache.city")
if ok then
return cacheData[id]
end
return nil
end
清理方法:
--- 實(shí)現(xiàn)示例,可以根據(jù)項目情況,完善后封裝在數(shù)據(jù)查詢層
local function clearCityCache()
package.loaded["cache.city"] = nil
end
數(shù)據(jù)存儲 _G請求級 table 變量,生命周期為本次請求,可存儲請求級任意 Lua 數(shù)據(jù)。
NGX.CTX
請求級 table 變量,生命周期為本次請求,可存儲請求級任意 Lua 數(shù)據(jù)。
NGX.SHARED.DICT
全局級 key-value 字典,使用共享內(nèi)存實(shí)現(xiàn),實(shí)現(xiàn)了讀寫鎖,所有請求均可安全讀寫。 value 只能為布爾值、數(shù)字和字符串。Reload Nginx 時不會受影響,只有當(dāng) Nginx 被關(guān)閉時才會丟失。
模塊屬性和模塊級局部變量
worker 級變量,同一 worker 的所有請求共享,沒有讀寫鎖,多個請求同時寫入時不安全。
多謝原作者的分享: http://zivn.me/?p=157
LUA.REN
www.lua.ren
posted on 2016-08-02 13:48
思月行云 閱讀(751)
評論(0) 編輯 收藏 引用 所屬分類:
Nginx\Openresty