1. Introduction

Hello, I’m Angol!

Remote Procedure Call (RPC) is an inter-process communication mode. It adopts the server/client mode and is a request and response model

Among them, the server is responsible for providing the service program, responding to the request to do the specific implementation logic, and the client is responsible for the request invocation

Mainstream RPC frameworks include:

  • Ali’s Dubbo

  • Facebook 的 Thrift

  • Google 的 gRpc

  • Motan of Sina Weibo

  • Golang Ecology RPCX

Among them, gRpc and Thrift are cross-language RPC service frameworks, and Thrift has higher performance

This article uses Thrift as an example to talk about processes that use RPC in Python

2. Thriftpy2 is introduced

Thrift is an interface description language and binary communication protocol that can be used to define and create cross-language services, enabling efficient and transparent communication between clients and servers in different languages

Thriftpy2 is a secondary encapsulation on top of Thrift, making it easier and faster to write RPCS

Project address: github.com/Thriftpy/th…

First, we install the dependency packages in a virtual environment

# Install pip3 install thriftPy2Copy the code

Then, if you are on Windows, it is recommended to install the Thrift plug-in in Pycharm

PS: This plugin makes it easy to write Thrift communication files

Download address: plugins.jetbrains.com/plugin/7331…

3. Practice

First, write the Thrift communication files as required

For example, the file defines two methods

  • The function ping takes no arguments

  • Log on to the login

    Contains two parameters: username and password

# foo.thrift

service PingPong{
    string ping(),
    string login(
        1: string username,
        2: string password
    )
}
Copy the code

Then, write the server side code

Write the concrete implementation logic according to the methods defined in the Thrift communication file

Create a service object, specify the bound IP address and port number, start the service, and listen for messages

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): """ def ping(self): """ return: """ return "pong" def login(self, username, password): """ login :param username: username: param password: password: return: """ print(' obtain the parameter from the client,username :',username: ",password: ",password) return 'Login successful! Server = make_server(pingpong_thrift.PingPong, Dispatcher(), '192.168.40.217', 9000) # start service and listen to server.serve()Copy the code

Next, write the client code

Here, based on the IP address and port number provided by the server, the client connection object is created and the methods defined in the communication file are invoked

PS: If the client executes remotely, the Thrift communication file needs to be placed in the sibling directory for execution

Py import thriftpy2 from thriftPy2. RPC import make_client # Read the communication configuration file pingpong_THRIFT = Thriftpy2. Load ("foo.thrift", module_name="pingpong_thrift") # make_client = make_client(pingpong_thrift. Print (client.ping()) print(client.login('root', 'PWD '))Copy the code

Finally, run the server and client code separately

Using WireShark, you can analyze packets to discover the communication mode and data transmission process between the server and client

WireShark can be used in this article:

Reptile encounter Socket, don’t panic, the liver is finished!

4. The last

In enterprise projects, THE characteristics of HTTP is simple, easy to develop, easy to use, is the mainstream data transmission protocol

Compared with HTTP or H2, RPC’s main advantages are reflected in high security, low performance consumption, high transmission efficiency, and convenient service governance. Therefore, we can choose a reasonable data communication mode according to the actual project requirements

If you think the article is good, please like, share, leave a message, because this will be my strongest power to continue to output more high-quality articles!