What is RPC?

RPC is a framework or architecture whose main goal is to make remote service calls simple and transparent, as if they were local.

Baidu Encyclopedia explanation:

Remote Procedure Call (RPC) – A protocol for requesting services from Remote computer programs over a network without understanding the underlying network technology. The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans both the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprograms.

When is RPC used?

If we develop simple applications, the business processes are simple, the traffic is small, and RPC is not needed at all.

When the number of application visits and services increases, it is found that a single machine cannot bear it. In this case, we can divide the application into several unrelated applications based on different services (with clear division of service logic) and deploy them on different machines. In this case, RPC may not be needed.

With the increasing number of services and applications, applications are called in association with each other. Some functions cannot be easily separated, and RPC may be needed.

For example, when we develop the e-commerce system, we need to separate the user service, commodity service, coupon service, payment service, order service, logistics service, after-sales service and so on. All these services are called each other. In this case, it is better to use RPC for internal call, and each service can be independently deployed and launched.

That is to say, when our project is too large and needs decoupled services with strong scalability and flexible deployment, RPC will be used at this time, mainly to solve the invocation problem between services in distributed system.

Principle of RPC framework

RPC architecture mainly consists of three parts:

  • The service Registry is responsible for publishing local services as remote services, managing remote services, and making them available to service consumers.

  • A service provider (Server) that provides service interface definitions and service implementation classes.

  • A service consumer (Client) that invokes a remote service through a remote proxy object.

After startup, the service provider registers machine IP, port and the list of services provided in Registry.

The service consumer gets a list of service provider addresses from Registry at startup.

The service Registry enables load balancing and failover.

RPC call procedure

(1) The client invokes the service by local invocation;

(2) After receiving the call, the client stub is responsible for assembling methods and parameters into a message body capable of network transmission (serializing the message body object into binary);

(3) The client sends the message to the server through sockets;

(4) The server stub decodes the message after receiving it (deserialize the message object);

(5) The server stub invokes the local service according to the decoding result;

(6) The local service executes and returns the result to the server stub;

(7) The server stub packages the returned result into a message (serializes the result message object);

(8) The server sends the message to the client through sockets;

(9) The client stub receives the result message and decodes it (serializes the result message);

(10) The client gets the final result.

RPC encapsulates steps 2, 3, 4, 7, 8, and 9.

RPC advantages

  • Cross-language (C++, PHP, Java, Python…)

  • The protocol is private and has high security

  • High data transmission efficiency

  • Support for dynamic scaling

RPC shortcomings

It is difficult to develop a perfect RPC framework, which requires more professionals and is more difficult for beginners.

What does PHP RPC have?

  • Thrift:

  • http://thrift.apache.org/

  • GRPC:

    http://doc.oschina.net/grpc

  • Yar:

    https://www.php.net/manual/zh/book.yar.php

  • Swoole xml-rpc:

    https://wiki.swoole.com/wiki/page/683.html

  • Hprose:

    https://hprose.com/

summary

This article shares what I think RPC is like.

It mainly includes what scenarios to use RPC, the principle of RPC and the calling process, as well as the advantages and disadvantages of RPC and the RPC framework commonly used in PHP.

I will share with you when I examine a specific RPC.

Recommended reading

  • System – PHP interface signature verification

  • System explanation – PHP floating point high precision operation

  • System explanation – PHP cache technology

  • System explanation – PHP WEB security defense

  • System description – SSO single sign-on

This article is welcome to forward, forward please indicate the author and source, thank you!