Thrift rpc mounting system is installed Centos 7
Methods unsuccessful :( not find a reason, but still have to record)
Dependent libraries yum install automake libtool Flex Bison pkgconfig gcc -c ++ the Boost-devel libevent-devel zlib-devel Python-devel Ruby- devel download thrift package wget HTTP: // mirror.bit.edu.cn/apache/thrift/0.12. 0 / Thrift-0.12.0.tar.gz the tar -vzxf thrift- 0.12 . 0 .tar.gz . / Configure --prefix = / usr / local will get an error (G ++: error: / usr / the lib64 / libboost_unit_test_framework. a: No such file or directory)
The error (g ++: error: /usr/lib64/libboost_unit_test_framework.a: No such file or directory) not resolved, nor on the results of online search, so decisive another way.
Successful methods:
System update yum - the y-Update if you can skip the following steps: install the tool platform yum -y groupinstall " Development Tools " to install wget tool yum install - the y-wget installed automatically configure wget HTTP: // ftp.gnu.org/gnu/ autoconf / autoconf-2.69.tar.gz the tar xvf autoconf- 2.69 .tar.gz CD autoconf - 2.69 . / Configure --prefix = / usr the make the sudo the make the install CD .. installed automatically compile wget HTTP: // ftp.gnu. ORG / GNU / automake / automake-1.14.tar.gz the tar xvf automake- 1.14.tar.gz cd automake - 1.14 . / the configure --prefix = / usr the make sudo the make install cd .. update Bison wget HTTP: // ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz xvf bison- the tar 2.5 . . 1 .tar.gz CD Bison - 2.5 . . 1 . / Configure --prefix = / usr the make the sudo the make the install CD .. installation dependent libraries c yum -Y-devel zlib the install the libevent-devel openssl- devel installed boost version > = 1.53 wget HTTP: //sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz tar xvf boost_1_53_0.tar.gz cd boost_1_53_0 ./bootstrap.sh sudo ./b2 install 下载thrift git clone https://git-wip-us.apache.org/repos/asf/thrift.git cd thrift ./bootstrap.sh ./configure --with-lua=no make
Written in python and client service
First understand python server
Implement the server side mainly in the following five areas:
①Handler
server-side business logic. This is business code, such as the similarity of two strings is calculated
②Processor
transferred from the frame to the business logic Thrift. So is the RPC call, the client should send parameters to the server, and all this package of Thrift up by Processor will receive the "data" transferred to the business logic to deal with
③Protocol
serialization and de-serialization of data. Provided by the client is "string", and the data transmission is one byte, and therefore will use serialization and deserialization.
④Transport
data transmission transport layer.
⑤TServer
type of end of service. The manner in which server to handle client requests, for example, a Client request to create a new thread of it? Or the use of thread pool? ...... refer to: Socket blocking communication of programming
TSimpleServer - using standard single-threaded server blocking the I / O
TThreadPoolServer - multithreaded server using a standard blocking the I / O
TNonblockingServer - multithreaded server nonblocking type I / O # this is not very understanding not yet get
Explanation:
The thfit write python client and the server was trying to record sql mysql moved to deploy on the server so that the front end of the service only need to send back-end services on it
The following was written demo:
Intermediate code file sqlrecord.thrift:
service SqlRecord { string sql_record(1:string data) }
For sqlrecord.thrift compile:
thrift -r --gen py sqlrecord.thrift
Gen-py generating an intermediate code file i.e. the client and server connection
The server code is as follows:
import sys sys.path.append('gen-py') from sqlrecord import SqlRecord from sqlrecord.ttypes import * from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from sqlRecord import sqlThread # sql 记录的代码 import json def producer(consumer): consumer.send(None) def outer(func): def inner(*args): flag = func(*args) consumer.send(args[1]) return flag return inner return outer def consumer(): sql = '' while 1: rec = yield sql if not rec: break if not isinstance(rec,str): BREAK sql_rec = json.loads (REC) # The following are the recording operation can be replaced by other sql the try : sqlThread (sql_rec) the except Exception AS E: Print (E) Continue Print ( " successful insert " ) class SqlHandler (): @producer (Consumer = Consumer ()) DEF sql_record (Self, Data): return " OK " class ThritfServer (): DEF the __init__ (Self, Host = ' 127.0.0.1 ', port=9090, cls = None, handler = None): self._host = host self._port = port self._cls = cls self._handler = handler self._server = self.new() def new(self): if not hasattr(self._cls,'Processor'): return processor = self._cls.Processor(self._handler) transport = TSocket.TServerSocket(host=self._host,port=self._port) tfactory = TTransport.TBufferedTransportFactory() pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) return server def run(self): self._server.serve() if __name__ == '__main__': handler = SqlHandler() server = ThritfServer(cls=SqlRecord,handler=handler) server.run()
Client code is as follows:
import sys sys.path.append('gen-py') from sqlrecord import SqlRecord from sqlrecord.ttypes import * from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol import json import threading # sqlrecord.thrift 文件中定义的方法名 _thrift_methods = ['sql_record'] class ThriftClient(): def __init__(self,host='127.0.0.1',port=9090,cls=None): self._host = host self._port = port self._cls = cls self._transport = None self._client = None self.connect() def connect(self): if not hasattr(self._cls,'Client'): return socket = TSocket.TSocket(self._host,self._port) self._transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(self._transport) self._client = self._cls.Client(protocol) self._transport.open() def send_data(self,data): if not isinstance(data,dict): return if not hasattr(self._client,_thrift_methods[0]): return send = getattr(self._client,_thrift_methods[0]) result = send(json.dumps(data)) print(result) def __del__(self): self._transport.close() if __name__ == "__main__": tc = ThriftClient(cls=SqlRecord) data = {'table': 'test', 'address': 'http://127.0.0.1/?1111.html', 'results': [[3, 8, 9, 9, 9, 4]], 'duration': 1129, 'average_duration': 564} tc.send_data(data)
github Great God for the upgrade link thrift: https://github.com/Thriftpy/thriftpy
The official document links of thrift: http://thrift.apache.org/