背景

随着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

http://openresty.org/

Done.

最新文章

  1. 微信测试服务器验证sha1加密法,工具类
  2. Android RSA加密解密
  3. SSI-Server Side Inclued
  4. Advanced Puppet 系列的前言
  5. python BDD 框架之lettuce
  6. Asteroids(匈牙利算法入门)
  7. Oracle 学习笔记3:新建数据库没有scott用户解决办法
  8. jquery加入购物车飞入的效果
  9. Shell 脚本编程笔记(一) Hello Shell
  10. python - 类成员修饰符
  11. Cordova各个插件使用介绍系列(二)—$cordovaBarcodeScanner扫描二维码与生成二维码
  12. DSAPI 调用串口选择界面
  13. codeforces131D
  14. MySQL的binlog恢复(Windows下)
  15. so静态分析进阶练习——一个CreakeMe的分析思路
  16. poi-对于word的操作(一)
  17. 谈谈你对MVC的理解
  18. Linux-(ps,grep)
  19. Hadoop HBase概念学习系列之HBase的Shell(步骤非常清晰)(二十四)
  20. 32bit 天堂服务端假设教程

热门文章

  1. xml解析
  2. 初识NodeJS
  3. phpstudy配置伪静态的方法
  4. [LeetCode] Split Array Largest Sum 分割数组的最大值
  5. [LeetCode] Move Zeroes 移动零
  6. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
  7. ASP.NET MVC VS2010中更改默认调试浏览器
  8. java中静态方法和静态类的学习
  9. 进度条投票-W
  10. Leetcode 15. 3Sum