RPC之thrift-简单demo

2019-07-26  本文已影响0人  SeanJX

Thrift简介

Apache thrift是一个软件框架,目的是提供可伸缩、跨语言的服务开发,连接code栈和code引擎来创建高效的,无缝连接的服务,无论在何种主流的开发语言之中。

Getting started
1.下载Apache thrift,downloada copy of Thrift.

  1. 编译安装Apache thrift 编译器-See the installing Thriftguide
  2. 编写一个a.thrift文件
    使用接口文件定义一个自己的thrift文件,根据这个文件我们可以使用thrift编译器生成clients和server端代码
    <pre class="cjk">thrift --gen <language> <Thrift filename></pre>
  3. 根据生成的文件可以开发rpc服务

具体demo参见附录

Thrift 网络栈

+-------------------------------------------+
| Server |
| (single-threaded, event-driven etc) |
+-------------------------------------------+
| Processor |
| (compiler generated) |
+-------------------------------------------+
| Protocol |
| (JSON, compact etc) |
+-------------------------------------------+
| Transport |
| (raw TCP, HTTP etc) |
+-------------------------------------------+

open
close
read
write
flush

ServerTransport有:

open
listen
accept
close

也支持其他传输协议:

file:文件读写
http: 网络

interface TProcessor {
bool process(TProtocol in, TProtocol out) throws TException
}

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)
上一篇 下一篇

猜你喜欢

热点阅读