Implementation of RPC framework-RPC principle and implementation from zero-start

Source: Internet
Author: User
Tags serialization stub zookeeper
RPC Overview

RPC (remote Procedure call), which allows a computer to invoke a program on another computer to get results, does not require additional programming in the code, as it does on a local call.

Now the scale of Internet application is getting bigger and larger, the ability of single computer is limited, it needs to be done with the help of scalable computer cluster, distributed application can use RPC to complete the call between machines. RPC Framework Principle

There are three main roles in the RPC framework: Provider, consumer, and registry. As shown in the following illustration:

Node role Description:
* Server: The service provider that exposes the service.
* Client: The service consumer that invokes the remote service.
* Registry: Registration Center for service Registration and discovery. RPC Call Process

RPC Basic Flowchart:

A complete RPC call process (synchronous invocation, asynchronous) is as follows:
1 The service consumer (client) invokes the service in a local invocation mode;
2 after receiving the call, the client stub is responsible for assembling the method and parameters into a message body capable of transmitting the network;
3 The client stub finds the service address and sends the message to the service side;
4) the server stub after receiving the message to decode;
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 results into a message and sends it to the consumer side;
8 The client stub receives the message and decodes it;
9) The service consumer obtains the final result.

The goal of the RPC framework is to 2~8 these steps and let the user be transparent about these details. Service Registration & Discovery


The service provider initiates the initiative to register with the registry the machine IP, port and the list of services provided;
The service consumer starts to obtain the service provider address list to the registry, can realize the soft load equalization and the failover; the technology used

1. Dynamic Agent
To generate the client stub and server stub requires Java dynamic Proxy technology , we can use the JDK native dynamic proxy mechanism, we can use some open source bytecode tool framework such as: Cglib, Javassist and so on.

2. Serialization of
In order to transmit and receive Java objects over the network, we need to serialize and deserialize them.
* Serialization: The process of converting Java objects into byte[], which is the process of encoding;
* Deserialization: The process of converting byte[to Java objects;

Java native serialization mechanisms can be used, but the efficiency is very low, and some open source, mature serialization techniques are recommended, such as Protobuf, Thrift, Hessian, Kryo, Msgpack

About the serialization tool performance comparisons can be referred to:jvm-serializers

3. NIO
At present, many RPC frameworks are directly based on the IO communication framework of Netty, such as Alibaba's HSF, Dubbo,hadoop Avro, and it is recommended to use Netty as the underlying communication framework.

4. Service Registration Center
Optional technology:
* Redis
* Zookeeper
* Consul
* ETCD Programming Practice

I have implemented a simple, efficient RPC framework Mango:https://github.com/tifg/mango based on Netty4 + zookeeper + Protostuff + Spring, and you are welcome to Fork/star. Open Source Excellent RPC framework Alibaba Dubbo:https://github.com/alibaba/dubbo Sina Weibo motan:https://github.com/weibocom/ Motan grpc:https://github.com/grpc/grpc rpcx:https://github.com/smallnest/rpcx Apache thrift:https:// thrift.apache.org/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.