Thrift簡介
Apache thrift是一個軟件框架,目的是提供可伸縮、跨語言的服務(wù)開發(fā)来吩,連接code棧和code引擎來創(chuàng)建高效的,無縫連接的服務(wù)蔽莱,無論在何種主流的開發(fā)語言之中弟疆。
Getting started
1.下載Apache thrift,downloada copy of Thrift.
- 編譯安裝Apache thrift 編譯器-See the installing Thriftguide
- 編寫一個a.thrift文件
使用接口文件定義一個自己的thrift文件盗冷,根據(jù)這個文件我們可以使用thrift編譯器生成clients和server端代碼
<pre class="cjk">thrift --gen <language> <Thrift filename>
</pre> - 根據(jù)生成的文件可以開發(fā)rpc服務(wù)
具體demo參見附錄
Thrift 網(wǎng)絡(luò)棧
+-------------------------------------------+
| Server |
| (single-threaded, event-driven etc) |
+-------------------------------------------+
| Processor |
| (compiler generated) |
+-------------------------------------------+
| Protocol |
| (JSON, compact etc) |
+-------------------------------------------+
| Transport |
| (raw TCP, HTTP etc) |
+-------------------------------------------+
- Transport
傳輸層為R/W網(wǎng)絡(luò)提供簡單的抽象怠苔,是得thrift和底層傳輸層解耦。
transport接口函數(shù)主要有如下:
open
close
read
write
flush
ServerTransport有:
open
listen
accept
close
也支持其他傳輸協(xié)議:
file:文件讀寫
http: 網(wǎng)絡(luò)
- Protocol
協(xié)議定義了一種內(nèi)存數(shù)據(jù)結(jié)構(gòu)和讀寫格式之間的映射機制仪糖,可以將數(shù)據(jù)字節(jié)流編碼或者解碼:
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
處理器具有讀寫數(shù)據(jù)的能力
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服務(wù)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)