背景
隨着openresty的出現,讓nginx使用lua解決一些業務的能力大幅度提高,ngx_lua可以使用nginx自生的基於事件驅動的IO模型,和后端的存儲,業務等系統實現非阻塞的連接交互。
如何使用ngx_lua連接后端的Thrift-Server呢?
基於這個需求,本人為ngx_lua做了一下增強。
增強后的業務架構圖

前端使用http對外提供服務,將請求的數據調用ngx_lua邏輯,使用定義好的Thrift的IDL文件生成lua代碼,調用Thrift的服務,實現業務邏輯。
開源實現github:https://github.com/gityf/ngx_lua_thrift
支持的協議protocol
Support protocol: binary,compact and JSON.
binary: TBinaryProtocol.lua
compact: TCompactProtocol.lua
JSON: TJsonProtocol.lua
Support transport: RawSocket,Buffered,Framed and Http.
支持的socket
使用nginx的tcp實現socket通信 socket: ngx.socket.tcp()
安裝步驟
下載 https://openresty.org/download/openresty-x.y.z.a.tar.gz .
install openresty.
To copy all files in lualib to directory openresty/lualib/
To cpoy all conf file to directory openresty/nginx/conf/
compile source in directory openresty/lualib/libthrift/c
Start openresty nginx server.
Nginx的配置
location = /v1/lua_thrift{
access_log logs/access.log main;
add_header 'Content-Type' 'text/html';
content_by_lua '
local cln = require "test_cln"
ngx.say(cln.demoFunc());
';
}
ngx_lua的thrift客戶端代碼
function _M.demoFunc()
local socket = TSocket:new{
host='127.0.0.1',
port=8090
}
--local protocol = TBinaryProtocol:new{
-- local protocol = TCompactProtocol:new{
--trans = socket
--}
local protocol = TJSONProtocolFactory:getProtocol(socket)
--local protocol = TCompactProtocolFactory:getProtocol(socket)
client = RpcServiceClient:new{
protocol = protocol
}
local argStruct = ArgStruct:new{
argByte = 53,
argString = "str 測試字符串\"\t\n\r\'\b\fvalue",
argI16 = 54,
argI32 = 12.3,
argI64 = 43.32,
argDouble = 11.22,
argBool = true
}
-- Open the socket
socket:open()
pmap = {}
pmap.name = "namess"
pmap.pass = "vpass"
pistrmap = {}
pistrmap[10] = "val10"
pistrmap[20] = "val20"
ret = client:funCall(argStruct, 53, 54, 12, 34, 11.22, "login", pmap,
pistrmap,
{"ele1", "ele2", "ele3"},
{11,22,33},
{"l1.","l2."}, false);
res = ""
for k,v in pairs(ret)
do
print(k, v)
res = res .. k .."." .. v .. "<br>"
end
return res
end
簡單的demo測試
To access web url http://127.0.0.1:8000/v1/lua_thrift and get result.
1.return 1 by FunCall.
2.return 2 by FunCall.
代碼實現:
https://github.com/gityf/ngx_lua_thrift
參考
https://github.com/apache/thrift/tree/master/lib/lua
Done.
