Thrift简介
Apache thrift是一个软件框架,目的是提供可伸缩、跨语言的服务开发,连接code栈和code引擎来创建高效的,无缝连接的服务,无论在何种主流的开发语言之中。
Getting started
1.下载Apache thrift,downloada copy of Thrift.
- 编译安装Apache thrift 编译器-See the installing Thriftguide
- 编写一个a.thrift文件
使用接口文件定义一个自己的thrift文件,根据这个文件我们可以使用thrift编译器生成clients和server端代码
<pre class="cjk">thrift --gen <language> <Thrift filename>
</pre> - 根据生成的文件可以开发rpc服务
具体demo参见附录
Thrift 网络栈
+-------------------------------------------+
| Server |
| (single-threaded, event-driven etc) |
+-------------------------------------------+
| Processor |
| (compiler generated) |
+-------------------------------------------+
| Protocol |
| (JSON, compact etc) |
+-------------------------------------------+
| Transport |
| (raw TCP, HTTP etc) |
+-------------------------------------------+
- Transport
传输层为R/W网络提供简单的抽象,是得thrift和底层传输层解耦。
transport接口函数主要有如下:
open
close
read
write
flush
ServerTransport有:
open
listen
accept
close
也支持其他传输协议:
file:文件读写
http: 网络
- Protocol
协议定义了一种内存数据结构和读写格式之间的映射机制,可以将数据字节流编码或者解码:
binary: Fairly simple binary encoding -- the length and type of a field are encoded as bytes followed by the actual value of the field.
compact: Described in THRIFT-110
json - Processor
处理器具有读写数据的能力
interface TProcessor {
bool process(TProtocol in, TProtocol out) throws TException
}
- Server
A Server pulls together all of the various features described above:
Create a transport
Create input/output protocols for the transport
Create a processor based on the input/output protocols
Wait for incoming connections and hand them off to the processor
附录:
https://thrift.apache.org/docs/
一个简单的thrift服务demo(python 2 )
testService.thrift
service TestService {
/*print hello world*/
string say(1: required string something),
/*sum func*/
i64 calc(1:required i64 larg, 2:required i64 rarg),
}
test_client.py
# coding=utf-8
from testService import TestService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def main():
transport = TSocket.TSocket("127.0.0.1", 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TestService.Client(protocol)
transport.open()
print client.say("hahaha")
print client.calc(100, 90)
transport.close()
if __name__ == "__main__":
main()
test_server.py
# coding=utf-8
import sys
import logging
from testService import TestService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
logger = logging.getLogger(name="test_server")
class TestServiceHandler:
def say(self, something):
logging.info(something)
return "hello world, %s" % something
def calc(self, larg, rarg):
return larg + rarg
if __name__ == "__main__":
logging.basicConfig(stream=sys.stdout, level=logging.INFO,
format='%(asctime)s %(levelno)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
try:
handler = TestServiceHandler()
processor = TestService.Processor(handler)
transport = TSocket.TServerSocket(host="127.0.0.1", port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()\
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print "server starting..."
server.serve()
print "server done."
except Exception as e:
logging.exception(e)