Thrift lua implementation and thriftlua implementation

Source: Internet
Author: User
Tags connection reset

Thrift lua implementation and thriftlua implementation

The system is being upgraded recently. The background data is differentiated by city. Worried about the stability of the new system and the accuracy of new data, some cities are planned to adopt new interfaces. There is no city information in the input parameters of the interface, and only the longitude and latitude coordinates are required. You need to call a thrift interface to obtain the city information based on the coordinates.

If you directly modify the code logic, the coupling between the new and old versions will occur. Not only do you need to modify the code logic when it is fully online, but you also need to add a test process, which is a little more costly. At this time, I thought that I could use nginx + lua to perform grayscale release on the interfaces of the new and old versions.

Steps:

1. Install thrift

2. Generate Customer Code

3. Compile lua to call the library required by thrift

4. implement client code

5. Test

  

1. Install thrift

Thrift was initially developed and used by facebook for RPC communication between different languages in the system. In fact, thrift has many similarities with webservice.

First, there is a file that defines the data type and interface, xxx. thrift (xxx. wsdl in webservic), and then generate the corresponding client/server code using the program.

Thrift official website http://thrift.apache.org/, you can download the latest thrift (http://thrift.apache.org/download ).

Thrift-0.9.3.tar.gz (as of) is the latest version ).

The installation steps are available on the official website at http://thrift.apache.org/tutorial/. the basic steps are as follows:

1 ./configure && make && make install

The installation process may encounter a lack of dependency packages, which is not a big problem. Just install the package.

2. Generate Customer Code

Thrift file content. The server provides location_match.thrift:

1/** 2 * File: location_service.thrift 3 */4/** input parameter data structure **/5 struct Location_point 6 {7 1: double x; 8 2: double y; 9} 10/** output parameter data structure **/11 struct Citycode_Response12 {13 1: string retCode; 14 2: i16 cityCode; 15} 16 17/** interface definition **/18 service LocationmatchService19 {20 Citycode_Response find_citycode (1: Location_point location_point) 21}

Generate the lua client code based on this file:

1 thrift --gen lua location_match.thrift

A directory will be generated in the current directory: gen-lua

There are three files:

Location_match_constants.lua

Location_match_LocationmatchService.lua: Interface Definition

Location_match_ttypes.lua: Entry/Exit parameter structure definition

This process is very similar to the process in which axis generates webservice code based on wsdl. The preceding three files do not need to be modified.

3. Compile lua to call the library required by thrift

Lua needs some library files to call the thrift service, some of which are the dynamic link library *. so file implemented by C language, and some are the lua function library *. lua. The source code files of these databases are included in the official Network Package (thrift-0.9.3.tar.gz). After decompression, the libraries of various languages are available in the lib directory. Lua is under lib/lua.

Here, you need to pay attention to a few issues: 1. the C language compilation on the official website will report an error (of course, it may be an individual phenomenon). The specific error is not remembered. In general, the error is reported when relink libluasocket. so is used. Modify the Makefile. am to adjust the sequence. For more information, see http://blog.csdn.net/superye1983/article/details/41090166. 2. To call the thrift service, you must pay attention to the two key transmission protocols and I/O methods (blocking/non-blocking). The transmission protocols include binary stream transmission, json string transmission, and compressed data transmission formats, in fact, the main provisions of the data in the transmission process format, the official website package only binary transmission protocol TBinaryProtocol. lua. 3. In the package on the official website, socke is implemented based on lua, while openresty is actually implemented based on luajit. The socket of luajit is ngx. socket. tcp (), which may cause some conflicts.

Here the use of http://www.cnblogs.com/voipman/p/5365248.html mentioned in this Article source code implementation, without the use of thrift official website code. Code: https://github.com/gityf/ngx_lua_thrift. There are clear installation instructions on the above. It is very simple to compile a dynamic link library with only one command to make linux.

Nginx used here is openresty. Download and install see http://openresty.org/cn.

4. implement client code

Test_cln.lua:

 1 require('TSocket')    2 require('TCompactProtocol') 3 require('TTransport')  4 require('location_match_LocationmatchService') 5 require('location_match_ttypes') 6 require('TFramedTransport') 7 module("test_cln",package.seeall)  8 function demoFunc() 9   local opt = {10         host='127.0.0.1',11         port=909012   }13   local socket = TSocket:new(opt)14   local ttable = {15      trans = socket16   }17   local transport = TFramedTransport:new(ttable)18   19   20   local protocol = TCompactProtocol:new{21         trans = transport22   }23 24   client = LocationmatchServiceClient:new{25         protocol = protocol26   }27   local location_point = Location_point:new{28         x=114.2901961,29         y=22.76033004,30   }31  socket:open()32  res = ""33  local ret = client:find_citycode(location_point)34  res= tostring(ret.cityCode)35  ngx.log(ngx.ERR,res..'   ~~~~~~~'..tostring(ret.cityCode))36   return res37 end

 

Implementation is similar to http://blog.csdn.net/superye1983/article/details/41090166. The difference is that the protocol uses the compression protocol TCompactProtocol, And the IO blocking method is non-blocking. Therefore, TFramedTransport is used because the server implements the non-blocking service, if the protocol or blocking method is not called, an I/O error will be reported. TTransportException: time out/TTransportException: closed/TTransportException: connection reset by peer.

Put the lua file generated in step 2, the dynamic link library compiled in step 3, and the lua file in the lualib directory. Test_cln.lua should also prevent this directory.

Nginx. conf:

1         location / {2             content_by_lua_file /usr/local/nginx/nginx/conf/luascript/test.lua; 3             root   html;4             index  index.html index.htm;5         }

 

Test. lua:

1 local cln = require "test_cln"2 ngx.say(cln.demoFunc());

 

5. Test
[root@hadoop-1 sbin]# curl http://127.0.0.1/1438

 

The city code is returned.

 

Conclusion: openresty (A nginx version with the luajit plug-in) is used to implement phased release of interfaces. lua calls thrift, installs thrift, and generates customer code, download the library that the open-source thrift-lua depends on. thrift requires protocol and io blocking. The client is consistent with the server.

 

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.