1. Preface
RPC, The whole journey is Remote Procedure Call, It's a way of communicating between processes , It uses 「 Server side / The client 」 Pattern , It's a request response model
among , The server is responsible for providing service programs 、 Respond to the request and do the specific implementation logic , The client is responsible for calling
Mainstream RPC The framework contains :
among ,gRpc and Thrift It's Cross lingual RPC Service Framework , also Thrift Higher performance compared to
This article uses Thrift For example , Chat Python Use in RPC The process of
2. thriftpy2 Introduce
Thrift Is an interface description language and binary communication protocol , It can be used to define and create cross language services , Make clients in different languages 、 Efficient and transparent communication between servers
thriftpy2 Is in Thrift On the basis of the second package , Write with it RPC More convenient and quick
Project address :https://github.com/Thriftpy/t...
First , We install dependency packages in a virtual environment
# Installation dependency pip3 install thriftpy2
then , If it is Windows, It is suggested that Pycharm Install in thrift plug-in unit
PS: The plug-in can facilitate us to write Thrift Communication files
Download address :https://plugins.jetbrains.com...
3. Let's fight
First , Write on demand Thrift Communication files
such as , This document defines 2 A way
Contains two parameters :username、password
# foo.thrift service PingPong{ string ping(), string login( 1: string username, 2: string password ) }
then , Write server code
according to Thrift Methods defined in the communication file , Write specific implementation logic
Create a service object , Specifies the bound ip Address and port number , Turn on the service and listen for messages
# rcp_server.py import thriftpy2 from thriftpy2.rpc import make_server # Read the communication configuration file pingpong_thrift = thriftpy2.load("foo.thrift", module_name="pingpong_thrift") class Dispatcher(object): """ According to the method defined in the communication configuration file , Rewrite the implementation """ def ping(self): """ Ping once :return: """ return "pong" def login(self, username, password): """ Sign in :param username: user name :param password: password :return: """ print(' Get the parameters from the client , user name :',username,", password :",password) return ' Login successful !' # Create services , Specify the local ip Address and monitor port number server = make_server(pingpong_thrift.PingPong, Dispatcher(), '192.168.40.217', 9000) # Turn on the service and monitor server.serve()
next , Write client code
here , According to the server ip Address 、 Port number , Create a client connection object , Call the method defined in the communication file
PS: If the client is executing remotely , Need to put Thrift The communication file is placed in the same level directory for execution
# rcp_client.py import thriftpy2 from thriftpy2.rpc import make_client # Read the communication configuration file pingpong_thrift = thriftpy2.load("foo.thrift", module_name="pingpong_thrift") # Create client client = make_client(pingpong_thrift.PingPong, '192.168.40.217', 9000) # Call the method defined in the communication file ( Actually call the method on the server side ) print(client.ping()) print(client.login('root', 'pwd'))
Last , Run the code of server and client respectively
Use WireShark Carry out grab bag analysis , It can discover the communication mode and data transmission process between server and client
4. Last
In enterprise projects , frequently-used HTTP It's characterized by simplicity 、 Development convenience , Easy to get started 、 It's the mainstream data transfer protocol
comparison HTTP or H2,RPC The main advantage of is high safety 、 Low performance consumption 、 High transmission efficiency 、 Service management is convenient , So we can choose a reasonable data communication mode according to the actual needs of the project