Nginx uses Ngx_lua to access back-end Thrift-server implementation and introduction

Source: Internet
Author: User
Tags nginx server

Background

With the advent of openresty, the ability of Nginx to use LUA to solve some of the business is greatly improved, Ngx_lua can use Nginx-based event-driven IO model, and back-end storage, business and other systems to achieve non-blocking connection interaction.

How to use Ngx_lua to connect the backend Thrift-server?

Based on this demand, I did a bit of enhancement for Ngx_lua.

Enhanced Business Architecture Diagram

The front-end uses HTTP to provide services externally, calls the requested data to Ngx_lua logic, generates LUA code using the well-defined thrift IDL file, invokes the thrift service, and implements the business logic.

Open Source Implementation Github:https://github.com/gityf/ngx_lua_thrift

supported protocols Protocol
Support protocol: binary,compact and JSON.binary: TBinaryProtocol.luacompact: TCompactProtocol.luaJSON: TJsonProtocol.luaSupport transport: RawSocket,Buffered,Framed and Http.
supported sockets
使用nginx的tcp实现socket通信 socket: ngx.socket.tcp()
Installation steps
下载 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/cStart openresty nginx server.
Configuration of 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 Client-side code
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 = TJSO Nprotocolfactory:getprotocol (socket)--local protocol = TCOMPACTPROTOCOLFACTORY:GETPROTOCOL (socket) client = Rpcserv iceclient:new{protocol = protocol} local argstruct = argstruct:new{Argbyte =, argstring = "St R test string \ "\t\n\r\ ' \b\fvalue", argI16 = si, argI32 = 12.3, argI64 = 43.32, argdouble = 11.22, Argbo OL = True}--Open the Socket Socket:open () Pmap = {} Pmap.name = "namess" Pmap.pass = "Vpass" PiS Trmap = {} 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 Resend
A simple demo test
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.
Reference

Https://github.com/apache/thrift/tree/master/lib/lua

http://openresty.org/

Done.

Nginx uses Ngx_lua to access back-end Thrift-server implementation and introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.