Early in the morning on the weekend, I was reading the news in front of the computer, suddenly my girlfriend shouted: Wow, hangzhou under heavy snow, come and see. I ignored her, so she ran to pull me.

Hangzhou after a snow

RPC stands for Remote Procedure Call. Is a computer communication protocol.

Why are remote calls needed

In the article how to explain what is distributed to my girlfriend, I introduced that in order to improve the service capacity of the restaurant, the restaurant developed from a chef who was in charge of all things into a chef, a vegetable cutter, a vegetable preparation chef and other roles.

When there was only one chef in the restaurant, when the chef wanted to make a delicious scrambled egg with tomatoes, he had to wash the tomatoes, cut the tomatoes, beat the eggs, and stir the vegetables by himself. The whole process can be done without anyone else. This is the old centralized application, where a single computer can do everything.

Cook -> Wash -> Chop -> Stir fryCopy the code

With the development of the hotel, the division of labor needs to be clear, so that professional people are responsible for professional things. So it’s no longer just the cook who’s involved in the whole process. You need to have multiple roles, with a chef preparing the tomatoes and eggs, a chef chopping the vegetables, and a chef simply frying the vegetables.

However, as the division of labor becomes clearer, the process of making scrambled tomatoes and eggs is no longer a one-man affair. This process requires collaboration. Before cooking, the chef needs to inform the chef of preparation and chopping before cooking.

Make scrambled egg with tomato {prepare vegetable -> wash vegetable -> Chop vegetable chef -> Stir fry}Copy the code

In this case, the cook relies on many outsiders to help with the cooking. And when he notifies the preparer to help him wash the vegetables, notifies the chopping chef to help him cut the vegetables, this process is a remote procedure call.

In most cases, the waiter goes directly to the kitchen to place the order, and then a person in the back kitchen distributes the menu to the preparer, the cutter and the chef.

The process is much like a computer system. Today’s large web sites are distributed. An ordering process, for example, may involve multiple systems such as logistics, payment, inventory, red envelope, etc., which are deployed on different machines by different teams. In order to realize the order process, you need to use remote call.

Order {inventory -> reduce inventory payment -> Deduct red envelope -> Red envelope for logistics -> Generate logistics information}Copy the code

What exactly is a remote procedure call

RPC is A process on computer A that calls A process on another computer B, where the calling process on computer A is suspended and the called process on computer B begins execution. When the value is returned to A, A continues execution. The caller can pass the information to the called by using parameters, and then get the information from the result returned. This process is transparent to the developer.

As in the case of the back kitchen, the waiter passes the menu to the back kitchen, the chef tells the prepairer and the dishwasher to get to work, and then he waits for them to finish. After the preparer and washer have finished their work, the cook starts to fry the dishes. This process is transparent to the waiter, who does not need to care about how the food is cooked in the back.

Since each service is deployed on different machines, remote calls between services will inevitably involve network communication. Service consumers need to write a pile of code related to network communication for each service invocation, which is not only complex but also prone to errors.

It would be much more productive if there were a way to invoke a remote service as if we were invoking a local service, while making the caller transparent about the details of network communication, such as when a service consumer executes orderService.buy(“HHKB keyboard “), essentially invoking a remote service. This approach is essentially RPC. The tool that provides this functionality is called the RPC framework.

There are three main roles in the RPC framework: Provider, Consumer, and Registry. As shown below:

Server: The service provider that exposes the service. Client: service consumer that invokes the remote service. Registry: A Registry where services are registered and discovered.

Service providers and service consumers are better understood, it is the kitchen wash division and chef. The cook is the service consumer, the dishwasher is the service provider. Chefs depend on the services of dishwashers.

What is a service registry?

In fact, this is easier to understand. For big restaurants, you might have a lot of chefs and a lot of dishwashers. When a chef wants a dishwasher to help him wash dishes, he doesn’t go directly to a dishwasher. Instead, he calls an intermediary, who may be the leader of the dishwasher’s team or a person who specializes in coordinating the kitchen. He knows how many dishwashers there are in the kitchen, and which dishwashers showed up for work today (service registration is required). Moreover, it can dynamically allocate tasks (load balancing) according to the busyness of individual dishwashers.

This middleman is the service registry.

After startup, the service provider will register the machine IP, port and the service list to the registry. Service consumers obtain the address list of service providers from the registry when they start up, which can realize soft load balancing and Failover.

The techniques needed to implement RPC

A mature RPC framework needs to consider a lot of issues, here only introduces the implementation of a remote call needs to use the basic technology, interested friends can find some open source RPC framework code.

A dynamic proxy

Generating the Client stub and server stub requires the Java dynamic proxy technology. You can use the native dynamic proxy mechanism of JDK and some open source bytecode tool frameworks such as CgLib and Javassist.

serialization

To be able to transfer and receive Java objects over the network, we need to serialize and deserialize them.

Java’s native serialization mechanism can be used, but it is very inefficient. Some open source, mature serialization techniques such as Protobuf, Thrift, Hessian, Kryo, Msgpack are recommended

NIO

At present, many RPC frameworks are directly based on NetTY, such as ALIBABA’s HSF, Dubbo and Hadoop Avro. It is recommended to use NetTY as the underlying communication framework.

Service Registry

Optional technologies: Redis, Zookeeper, Consul, Etcd

Reference data: https://www.jianshu.com/p/dbfac2b876b1

Open source RPC framework

Dubbo

Dubbo is an open source Java high-performance service framework developed by Alibaba, which enables applications to realize the output and input functions of services through high-performance RPC, and can be seamlessly integrated with the Spring framework. It is currently in the Apache incubator.

Motan

Motan is an open source Java RPC framework of Sina Weibo. Open source in May 2016. Motan is already widely used on the microblogging platform, making nearly 100 billion calls a day to hundreds of services.

gRPC

GRPC is a high-performance, universal open source RPC framework developed by Google. It is mainly developed for mobile applications and designed based on THE HTTP/2 Protocol standard. It is developed based on the Protocol Buffers (ProtoBuf) serialization Protocol and supports many development languages. It is not distributed per se, so further development is required to implement the functionality of the framework above.

thrift

Thrift is a cross-language high performance service framework of Apache, which has been widely used.