Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

What is RPC?

Remote Procedure Call (RPC). Simply understood, a node requests services provided by another node. It is widely used in our daily development, especially in distributed systems.

What’s the difference between RPC and HTTP?

RPC requires that the client and server use the same protocol and version, while HTTP masks this part. If your server is developed in Go on Linux and your client is developed in Java on Windows, you should choose HTTP.

RPC is commonly used for microservices internal communication, and when you need middleware for request forwarding, you should choose HTTP.

When the number of requests for a service is high and frequent, you should choose RPC because HTTP packets are much larger than RPC for equivalent requests.

summary

  1. HTTP is flexible, cross-platform & language.
  2. HTTP is simpler than RPC.
  3. RPC is faster than HTTP.

An example is RPC

The service side

from xmlrpc.server import SimpleXMLRPCServer

# lcoal function
def sayHi(name) :
    print(f'Hi {name}! ')
    return f'Hi {name}! '

if __name__ == '__main__':
    # server instance
    server = SimpleXMLRPCServer(('127.0.0.1'.8889),allow_none=True)

    # register function to rpc server
    # after registered, local function can be call in rpc client
    server.register_function(sayHi)
    print(F 'server listening on 127.0.0.1:8889')

    # start rpc server
    server.serve_forever()
Copy the code

The client

from xmlrpc.client import ServerProxy

if __name__ == '__main__':

    # start rpc client
    with  ServerProxy('http://127.0.0.1:8889',allow_none=True) as client:

        # call server function sayHi
        res = client.sayHi('phyger')
        print(res)
Copy the code

As shown above, we can see that the client successfully called the server-side method and returned the expected result.

Brief analysis of RPC process

The RPC server is actually a SOCKET connection based on TPC, which exposes the service to a port of the host. The client establishes a connection with the server through this port, and then calls the RPC API to make the method of the server run on the server, while the server sends the return value to the client.

A more usable RPC library

Because XMLRPC comes with Python and doesn’t cross languages, You have come up with a more user-friendly RPC library: HProse (High-performance Remote Object Service Engine).

Hprose can be used across languages, currently supporting Java,.NET, Golang, PHP, C, Python and more than 20 other languages.

The service side

# from xmlrpc.server import SimpleXMLRPCServer

# # lcoal function
# def sayHi(name):
# print(f'Hi {name}! ')
# return f'Hi {name}! '

# if __name__ == '__main__':
# # server instance
# server = SimpleXMLRPCServer ((127.0.0.1, 8889), allow_none = True)

# # register function to rpc server
# # after registered, local function can be call in rpc client
# server.register_function(sayHi)
# print(f'server listening on 127.0.0.1:8889')

# # start rpc server
# server.serve_forever()

import hprose

def sayHi(name) :
    print(f'Hi,{name}! ')
    return f'Hi,{name}! '

def sayBye(name) :
    print(f'Bye,{name}! ')
    return f'Bye,{name}! '

def main() :
    # create rpc server instance
    server = hprose.HttpServer(host='localhost',port=8880)

    # add single function
    # server.addFunction(sayHi)

    # add more functions
    server.addFunctions([sayHi,sayBye])

    # start rpc server
    server.start()

if __name__ == '__main__':
    print('server listening on localhost:8880')
    main()
Copy the code

The client

import hprose

def main() :
    # start rpc client
    client = hprose.HttpClient('http://localhost:8880')

    # call server function sayHi & sayBye
    res1 = client.sayHi('Python Testing and Development ')
    res2 = client.sayBye('Python Testing and Development ')
    print(res1,'\n'+res2)

if __name__ == '__main__':
    main()
Copy the code

That’s all for today, thank you for reading, and we’ll see you next time.