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 ...
随机推荐
- cg数据类型
Cg 支持7 种基本的数据类型:1. float,32 位浮点数据,一个符号位.浮点数据类型被所有的profile 支持(但是DirectX8 pixel profiles 在一些操作中降低了浮点数的 ...
- get方式和set方式提交时乱码
request.setChracterEncoding("utf-8");只对form表单的post提交方式有效. 当使用get提交的时候:
- C#体检套餐项目
使用泛型集合写的一个小项目 1.要实现新建体检套餐,并且如果已经有了该体检套餐就不能再次新建, 2.要实现套餐列表动态更新,没添加一个体检套餐,在套餐列表里就自动添加一项; 3.向当前套餐类表里添加检 ...
- 测试docker不同主机间容器互相访问
测试服务器(centos6.5):192.168.16.70 网卡:eth0192.168.16.74 网卡:eth0第一步,分别在2台机器执行: yum install docker service ...
- oracle日常——修改用户密码
修改密码时,先进入sqlplus的sql命令状态(键入用户名密码之后),命令如下: alter user cnp2 identified by cnp3; --即将用户cnp2的密码修改为cnp3 格 ...
- Gulp:自动化构建工具
一.介绍: gulp是一个基于流的构建工具,可以自动执行指定的任务,简洁且高效 二.优点: 开发环境下,想要能够按模块组织代码,监听实时变化 css/js预编译,postcss等方案,浏览器前缀自动补 ...
- jquery中ajax用return来返回值无效
jquery中,ajax返回值,有三种写法,只有其中一种是成功的 /** * async:false,同步调用 * 返回1:2 * 失败 * 分析:ajax内部是一个或多个定义的函数,ajax中ret ...
- 小猪cms ClassifyAction.class.php
<?php /** *语音回复 **/ class ClassifyAction extends UserAction{ public $fid; public function _initia ...
- sql查询,不在某一范围问题的新思路
新思路: A为学生表 B为中间表(学生和课程的) C为课程表 新的思路是用left join,(right join应该也可以) 查询没有选课的学生 ... C left join B on A.si ...
- rt3070 驱动异常问题
新买的 Wi-Fi 网卡插在板子上,不能用.dmesg 查看,看到下面的内容. [ 9.501098] phy0 -> rt2x00lib_request_firmware: Error - F ...