Popular understanding of the RPC protocol

In the early stand-alone era, multiple processes were running on a computer, and everyone did their own things, and they didn’t communicate with each other. If process A needs a drawing function, and process B also needs a drawing function, the programmer must write a drawing function for both processes. Isn’t this a whole person? So IPC (Inter-process communication, mutual communication between processes running in a single machine) appeared. OK, now that A has the drawing function, B will call the drawing function on the A process, and the programmer can finally get lazy.

到了网络时代,大家的电脑都连起来了。 In the past, programs could only call processes on their own computer, can they call processes on other machines? So programmers extended IPC to the network, which is called RPC (Remote Procedure Call). Now not only processes on a single machine can communicate with each other, but processes on multiple machines can also communicate with each other.

要知道实现RPC很麻烦呀,什么多线程、什么Socket、什么I/O,都是让咱们普通程序员很头疼的事情。 So some great people developed RPC frameworks (for example, CORBA, RMI, Web Services, RESTful Web Services, etc.).

OK,现在可以定义RPC框架的概念了。 To put it simply, the RPC framework is a set of tools that allow programmers to call code on remote processes. With the RPC framework, we programmers are much easier, and finally can escape the sea of ​​multithreading, Socket, and I/O.

案例:

比如说,一个方法可能是这样定义的:
Employee getEmployeeByName(String fullName)
那么:

  • First of all, to solve the communication problem, the main thing is to establish a TCP connection between the client and the server. All the data exchanged by the remote procedure call is transmitted in this connection. The connection can be an on-demand connection, which is disconnected after the end of the call, or a long connection, where multiple remote procedure calls share the same connection.
  • Second, to solve the problem of addressing, that is, how does the application on the A server tell the underlying RPC framework, how to connect to the B server (such as the host or IP address) and a specific port, What is the name of the method so that the call can be completed. For example, RPC based on the Web service protocol stack needs to provide an endpoint URI, or search from the UDDI service. If it is called by RMI, an RMI Registry is also needed to register the address of the service.
  • Third, when the application on the A server initiates a remote procedure call, the parameters of the method need to be passed to the B server through the underlying network protocol such as TCP. Because the network protocol is based on binary, the parameters in the memory The value of must be serialized into a binary form, that is, serialized (Serialize) or marshal (marshal), and the serialized binary is sent to the B server through addressing and transmission.
  • Fourth, after the B server receives the request, it needs to deserialize the parameters (the reverse operation of serialization), restore the expression in the memory, and then find the corresponding method (part of the addressing) ) Make a local call and get the return value.
  • Fifth, the return value must be sent back to the application on server A, and sent through serialization. After server A receives it, it deserializes it and restores it to the expression in memory. Give it to the application on the A server

RPC( What is remote procedure call?

  • In short, RPC is to call another machine (server) from one machine (client) through parameter passing. ) On a function or method (collectively referred to as a service) and get the returned result.
  • RPC will hide the underlying communication details (no need to directly deal with Socket communication or Http communication)
  • RPC is a request response model. The client initiates a request, and the server returns a response (similar to how Http works)
  • RPC is used in the form of calling a remote function (or method) like calling a local function (or method).

The development history of remote procedure call

  • ONC RPC (Remote Procedure Call for Open Network Computing) , OSF RPC (Remote Procedure Call of the Open Software Foundation)
  • CORBA (Common Object Request Broker Architecture)
  • DCOM (Distributed Component Object Model), COM+
  • Java RMI
  • .NET Remoting
  • XML-RPC, SOAP, Web Service
  • PHPRPC, Hessian, JSON-RPC< /li>
  • Microsoft WCF, WebAPI
  • ZeroC Ice, Thrift, GRPC
  • Hprose

Early RPC

  • The first generation of RPC (ONC RPC, OSF RPC) does not support the transfer of objects.
  • CORBA is too complicated, different implementations are not compatible, and ordinary programmers can’t play it.
  • DCOM, COM+ cannot escape the palm of Windows.
  • RMI can only be played in Java.
  • .NET Remoting can only be played on the .NET platform.

XML-RPC, SOAP, WebService

  • Too much redundant data and too much processing speed slow.
  • RPC-style Web Service is not well cross-language, and Document-style Web Service is too difficult to use.
  • Web Service does not solve the real problem of users, it just turns one problem into another.
  • The specification of Web Service is too complicated, so that there is no really useful implementation outside of .NET and Java platforms, or even a usable implementation.
  • Cross-language and cross-platform is just a slogan for Web Service. Although many people are superstitious about this, in fact it has not really been implemented.

PHPRPC

  • Based on PHP’s built-in serialization format, in cross-language type mapping There are flaws.
  • The communication relies on the HTTP protocol, and there is no choice of other underlying communication methods.
  • The built-in encrypted transmission is both a feature and a disadvantage.
  • Although faster than XML-based RPC, it is not fast enough.

Hessian

  • The binary data format is completely unreadable.
  • The official only provides two and a half-language implementations (Java, ActionScript and the imperfect Python implementation), and third-party implementations in other languages ​​are mixed.
  • Not enough languages ​​are supported, and JavaScript on the web front end is completely ignored.
  • Although it is a dynamic RPC, the dynamics are still not good.
  • Although faster than XML-based RPC, it is not fast enough.

JSON-RPC

  • JSON is text readable and more concise than XML.
  • JSON is limited by a subset of the JavaScript language, and there are not enough data types that can be represented.
  • JSON format cannot express self-reference, mutual reference and circular reference in data.
  • Some languages ​​have multiple versions of implementation, but there is no uniform standard for type mapping, and there are compatibility problems.
  • Although JSON-RPC has specifications, it does not have a unified implementation. Respective implementations in different languages ​​have compatibility issues and cannot truly communicate with each other.

Microsoft WCF, WebAPI

  • They are Microsoft’s .NET platform for existing technologies The unified packaging on the above is an integration of technologies such as .NET Remoting, WebService, and REST-style services based on JSON, XML and other data formats.
  • Although it claims to be able to call these services outside the .NET platform, in fact, it is completely different from calling them within the .NET platform. It does not provide any tools that can be used in languages ​​of other platforms.

ZeroC Ice, Thrift, GRPC

  • The cross-language object-oriented return of the original RPC technology .
  • An intermediate language is still needed to write type and interface definitions.
  • You still need to use a code generator to translate the type and interface definitions written in the intermediate language into the client and server stubs of the programming language you are using.
  • You must write the service separately based on the generated server code, and you cannot publish the existing code directly as a service.
  • You have to use the generated client code to call the service, and there is no other more flexible way.
  • If your intermediate code is modified, you must repeat all the above steps at least once.

Hprose

  • No intrusive design, no need to define types separately, no need to write separately Services, existing codes can be directly published as services.
  • With rich data types and perfect cross-language type mapping, it supports self-reference, mutual reference and circular reference data.
  • Support many transmission methods, such as HTTP, TCP, Websocket, etc.
  • The client has more flexible calling methods, supports synchronous calls, asynchronous calls, dynamic parameters, variable parameters, reference parameter transfer, multiple result returns (Golang) and other language features. Hprose 2.0 even supports push.
  • With good scalability, various functional extensions such as encryption, compression, caching, and proxy can be achieved through filters and middleware.
  • Compatible and indiscriminate cross-language calls
  • Support more common languages ​​and platforms
  • Support browser-side cross-domain calls
  • No intermediate language, no learning cost
  • Excellent performance, easy to use

==================== ================================================== ===============
Author: iseeyou
Link: http://www.zhihu.com/question/25536695/answer/113449098
Source: Zhihu
The copyright belongs to the author, please contact the author for authorization for reprinting.

RPC is a communication method between systems. Commonly used communication methods between systems include http, webservice, rpc, etc. Generally speaking, RPC has higher performance than http and webservice. Common RPC Frameworks are: thrift, Finagle, dubbo, grpc, json-rpc, etc.

一个通用的网络RPC框架,它应该包括如下功能:

1.具有服务的分层设计,借鉴Future/Service/Filter概念
2.具有网络的分Layer design, distinguish protocol layer, data layer, transport layer, connection layer
3. Independent and adaptable codec layer, which can flexibly increase the support of HTTP, Memcache, Redis, MySQL/JDBC, Thrift and other protocols.
4. Incorporate years of experience in various remote calls to High availability into the implementation, such as load balancing, failover, multiple copy strategies, switch downgrades, etc.
5. Universal remote call implementation, using async mode to reduce the overhead of business services, and separating the concerns of remote call and data flow through the future.
6.具有状态查看及统计功能
7.当然,最终要的是,具备以下通用的远程容错处理能力,超时、重试、负载均衡、failover……

QiuRPC It is a small RPC framework implemented in JAVA. It has a total of 3K lines of code. It has been open sourced on github. The project address is: GitHub-i1see1you/QiuRPC: A simple RPC framework that implements the basic functions of RPC. Developers can also Custom extensions are available for everyone to study and discuss or use in small projects. Currently, QiuRPC has the following characteristics: 1. The server is based on annotations and automatically scans all RPC implementations at startup, with basic zero configuration. 2. The client implements the Filter mechanism, which can be self-explanatory. Define Filter 3. Netty-based Reactor IO multiplexing network model 4. The data layer provides the implementation of protobuff and hessian, which can extend the ISerializer interface to customize the implementation of others 5. Load balancing algorithm adopts the least active call number algorithm, and can extend the ILoadBlance interface Customize and implement other 6. The client supports synchronous or asynchronous invocation of services

Leave a Comment

Your email address will not be published.