nginx使用ngx_lua访问后端Thrift-Server实现和介绍
背景
随着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.
nginx使用ngx_lua访问后端Thrift-Server实现和介绍的更多相关文章
- Nginx实现前端访问后端本地接口
Nginx配置两个地方就行: 先是配置好自己项目的服务,确保自己的项目能运行: location / { root /web/xiangmu/public; // 本地项目的路径 index inde ...
- nginx前端负载,后端apache获取真实IP设置
原文链接: nginx前端负载,后端apache获取真实IP设置 参考文献: 前端Nginx,后端Apache获取用户真实IP地址 按照第二种方法设置不成功! 网站最前端是nginx,做的PROXY ...
- 配置tomcat限制指定IP地址访问后端应用
1. 场景后端存在N个tomcat实例,前端通过nginx反向代理和负载均衡. tomcat1 tomcatN | | | ...
- Nginx负载均衡中后端节点服务器健康检查的操作梳理
正常情况下,nginx做反向代理,如果后端节点服务器宕掉的话,nginx默认是不能把这台realserver踢出upstream负载集群的,所以还会有请求转发到后端的这台realserver上面,这样 ...
- Nginx——使用 Nginx 提升网站访问速度【转载+整理】
原文地址 本文是写于 2008 年,文中提到 Nginx 不支持 Windows 操作系统,但是现在它已经支持了,此外还支持 FreeBSD,Solaris,MacOS X~ Nginx(" ...
- Nginx 反向代理、后端检测模块
简介: Nginx 反向代理模块:ngx_http_proxy_module.ngx_http_upstream_module 后端检测模块:nginx_http_upstream_check_mod ...
- 使用 Nginx 提升网站访问速度
使用 Nginx 提升网站访问速度 http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/ Nginx 简介 Nginx ("engine ...
- 使用 Nginx 提升网站访问速度(转)
Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Ig ...
- [转帖]nginx 禁止ip访问以及禁止post方法的简单方法
nginx禁止IP访问站点的设置方法 http://www.512873.com/archives/471.html http://www.512873.com/archives/312.html c ...
随机推荐
- easy_UI 投票列表
首先我们考虑一下在项目投票种用到的属性(ID,投票标题,备选项目,参与人数) entity package cn.entity; public class GridNode { private Lon ...
- 《使用Hibernate开发租房系统》内部测试笔试题
笔试总结 1.在Hibernate中,以下关于主键生成器说法错误的是( C). A.increment可以用于类型为long.short或byte的主键 B.identity用于如SQL Server ...
- UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER
UNITY自带的PACKAGE的UTILITY 里面有一个自带的FPS COUNTER 可用,但是脚本是保密的?
- [No000096]程序员面试题集【上】
对几家的面试题凭记忆做个总结,基本全部拿到offer,由于时间比较长,题目只写大体意思,然后给出自己当时的答案(不保证一定正确): abstract类不可以被实例化 蛋糕算法: 平面分割空间:(n-1 ...
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 区块链(Blockchain)
一 .什么是区块链? 区块链(Blockchain)是由节点参与的分布式数据库系统[1],它的特点是不可更改,不可伪造,也可以将其理解为账簿系统(ledger).它是比特币的一个重要概念,完整比特币区 ...
- 网站收集ing....
1.账号注册网址 http://bugmenot.com/ PS:只要输入相关网站域名就能立即完成网站注册 2.博客网站 CSDN,博客园,开源中国 3.破解网站 吾爱破解 4.矢量图标 http:/ ...
- TCP/IP是一种十一状态
1.建立连接协议(三次握手) 三次握手过程说明: 1. 在最开始,客户端和服务器都是处于CLOSED状态 2.服务器会创建sockert开始监听,服务器状态LISTEN 3.客户端向服务器端发送SY ...
- MySQL字符串函数substring:字符串截取
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...
- celery 框架
转自:http://www.cnblogs.com/forward-wang/p/5970806.html 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据 ...