当前位置:网站首页>RPC与thrift入门
RPC与thrift入门
2022-07-23 05:38:00 【我是女生,我不做程序媛】
RPC服务
RPC:Remote Procedure Call,远程过程调用。可类比http服务,简单理解都是一个节点请求另一个节点提供的服务。
区别在于RPC服务建立在TCP/IP协议上,而HTTP服务建立在HTTP协议上。一般来说,RPC服务主要针对大型企业,而HTTP服务主要是针对小企业,因为RPC效率更高,而HTTP服务开发迭代会更快。
thrift协议
thrift是接口定义语言和通讯协议,放在远程的接口,实现跨语言开发。
建立thrift服务的步骤:
- 服务端建立IDL文件(xx.thrift文件),定义接口(不实现函数)。这里可以选择Python、Java等多种语言。以Python为例:
namespace py example
#consist of 2 parts:struct and service
# struct:字段前需要index
struct Data {
1: string text
2: i32 id
}
# service:类似interface,只写函数声明,不写方法体
service format_data {
Data do_format(1:Data data),
}
终端输入thrift --gen py xx.thrift;–gen java xx.thrift,生成服务接口文件(gen-language)(如server端用java,client端用python,则生成gen- java和gen-py。
server端用java编写server函数,创建服务。import gen-java中的文件,并实现thrift声明的方法(这里只有用python的例子)
import sys
sys.path.append('./gen-py')
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from example.format_data import Processor
from example import ttypes
__HOST = 'localhost'
__PORT = 9000
class FormatDataHandler(object):
# 实现thrift中定义的函数(service)
def do_format(self, data):
print(data.text, data.id)
# do something
return ttypes.Data(data.text.upper(), data.id)
# 启动服务
if __name__ == '__main__':
handler = FormatDataHandler()
processor = Processor(handler)
transport = TSocket.TServerSocket(__HOST,__PORT)
# 传输方式,使用buffer
tfactory = TTransport.TBufferedTransportFactory()
# 传输的数据类型:二进制
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
# 创建一个thrift 服务
rpcServer = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print('Starting the rpc server at', __HOST, ':', __PORT)
rpcServer.serve()
print('done')
- client端用python编写client函数,消费服务。import gen-py中的文件,并调用server定义的方法
import sys
sys.path.append('./gen-py')
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from example.format_data import Client
from example.format_data import Data
__HOST = 'localhost'
__PORT = 9000
tsocket = TSocket.TSocket(__HOST, __PORT)
transport = TTransport.TBufferedTransport(tsocket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Client(protocol)
data=Data('hello world!', 123)
transport.open()
print('client-requests')
res = client.do_format(data)
print('server-answer',res)
transport.close()
- 运行server→运行client(需要同时运行)

边栏推荐
猜你喜欢

开发必备之Idea使用

达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程

Flask蓝图

Dictionary creation and copying

Flask blueprint

With only 5000 lines of code, AI renders 100 million landscape paintings on v853

Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?

使用聚类分析 构建信用卡高风险客户识别模型

Data Lake: viewing data lake from data warehouse

【系统问题】.NET Framework 3.5 安装错误
随机推荐
Data Lake: introduction to Apache iceberg
MySQL statement queries all child nodes of a level node
框架介绍mvt
使用聚类分析 构建信用卡高风险客户识别模型
JWT header进行编码过程
Pytorch (V) -- pytorch advanced training skills
对比redis的RDB、AOF模式的优缺点
Activiti工作流使用之流程结构介绍
超链接去掉下划线代码
TS type gymnastics intermediate type gymnastics challenge closing battle
[information system project manager] Chapter VI recheck schedule management knowledge structure
对NLP中transformer里面decoder的理解
Deploy storageclass trample record
Briefly describe the features and application scenarios of redis
USCD行人异常数据集使用指南 | 快速下载
Project deployment (simplified version)
9. Ray tracing
Cadence learning path (VIII) PCB placement components
面试必备之数据库专题
项目流程总结