RPC framework communication principle

1. What is RPC

RPC: (Romote Procedure Call) remote procedure call;
the role of the RPC framework: make calling remote methods just like calling local methods

The purpose of remote call: data exchange

2. Open source RPC framework

There are mainly the following open source RPC frameworks bound to the language platform.

  • Dubbo: The earliest open source RPC framework in China, developed by Alibaba and open sourced at the end of 2011, only supports the Java language.

  • Motan: The RPC framework used internally by Weibo was open sourced in 2016 and only supports the Java language.

  • Tars: The RPC framework used internally by Tencent was open sourced in 2017 and only supports the C++ language.

  • Spring Cloud: The foreign open source RPC framework of Pivotal in 2014, which only supports Java language

The cross-language platform open source RPC framework mainly has the following types.

  • gRPC: Google's open source cross-language RPC framework in 2015, supporting multiple languages.
  • Thrift: It was originally a cross-language RPC framework for internal systems developed by Facebook. In 2007, it contributed to the Apache Foundation and became one of the
    Apache open source projects, supporting multiple languages.
  • hprose: A new lightweight cross-language cross-platform object-oriented high-performance remote dynamic communication middleware licensed by MIT. It supports many languages: nodeJs, C++, .NET, Java, Delphi, Objective-C, ActionScript, JavaScript, ASP, PHP,
    Python, Ruby, Perl, Golang.

3. Communication between services

Without the RPC framework mentioned above, how do we communicate between services?
Suppose we are two Java applications. Java uses Socket/ServerSocket to communicate. Sockets can build a listener at the Java level. This listener can be based on TCP or UDP.
As early as our tomcat6 and before, it was also based on socket, an implementation of NIO.

Insert picture description here

As shown in the figure: suppose App1 wants to call the sayHello method of App2, how to call it?
We cannot directly new HelloClass, because the class HelloClass is not in the virtual machine App1, so...

Students who have used Dubbo know that we only need to add an @Reference annotation when calling remotely.

@Reference
private HelloClass helloClass;

So this helloClass must be a proxy class, why add a proxy class, because it wants to proxy our request, so we must use dynamic proxy technology, this request is our remote communication request, through this proxy class proxy to our sayHello, and then it needs to transmit a data.

What data need to be passed?
These are necessary (protocol + ip + port + uri), in addition to:
1. Which class is
called 2. Which method is called
3. The parameter passed
4. The type of the parameter

After App2 gets the data, it can be initialized and called by reflection.

So how is the data transmitted over the network? Can be serialized in java.

After the call, the result is returned to App1, and the remote communication is completed.

4. Technical points of RPC communication

Serialization : xml/json/java native serialization/protobuf/avro/kryo/protostuff/hessian2 (transformed serialization)

Differences: performance (serialized performance), serialized message size, cross-platform, cross-language | open source, community activity, technical support

Communication (BIO/NIO): High-performance communication framework: Netty4 (mina) (Java NIO (new IO))

Dynamic proxy : Proxy interface in java/javasisst (Dubbo)

Reflection : java reflection

5. RPC communication steps

  1. Assemble data message
    Interface called, method called, method parameter, parameter type

  2. Establish network communication to communicate
    through sockets.

  3. Initiate remote transmission,
    output and input data

Guess you like

Origin blog.csdn.net/RookiexiaoMu_a/article/details/106374241