The article directories

  • preface
  • Network seven layer model
  • RPC architecture
      • Speaking of remote calls, it’s important to know * asynchronous synchronization *
        • Synchronous versus asynchronous invocation
  • HTTP
  • conclusion

preface

Here is a brief introduction to the concept of RPC. For a more in-depth understanding of RPC, you can see my previous blog [Custom RPC source code] Portal: Custom RPC source code implementation


Network seven layer model

Before I talk about the difference between RPC and HTTP, I think it’s important to understand OSI’s seven-layer network architecture model



Layer 1: application layer. Defines the interface for communicating and transmitting data in the network;

Layer 2: Presentation layer. Define data transmission format, encoding and decoding specifications in different systems;

The third layer: the session layer. Manage user sessions and control the establishment and interruption of logical connections between users.

The fourth layer: transport layer. Manages the end-to-end data transfer in the network;

Layer 5: network layer. Define how data is transmitted between network devices;

Layer 6: link layer. The packets of the network layer are encapsulated into data frames for easy transmission at the physical layer.

Layer 7: Physical layer. This layer is all about transferring binary data.

Key points: In practical application, there is no presentation layer and session layer in the five-layer protocol structure. I should say they merge with the application layer. We should focus on the application layer and the transport layer. Because HTTP is an application layer protocol and TCP is a transport layer protocol.

Here is a simple explanation to you based on each layer of load balancing, in fact, as we know nGINx is to do load balancing, but is based on which layer? Watch the following clip with this question

Layer 2 load: MAC address, VIP (virtual VIP) Virtual VIP is a constant, users access configured virtual VIP (that is, IP address) and then according to the load balancing route different real host IP, Generally speaking, virtual VIP has multiple IP addresses.) The MAC address of each machine is different and the IP address is the same

Layer 3 load: IP: provides VIPs externally. Each machine in the cluster uses a different IP address. Layer-4 load: load at the transport layer, including IP address and port number, and changing IP address or port address. Layer-7 load: load at the application layer, request URL, HTTP request packet, and host name

HTTP load balancing, or what we normally call “layer 7 load balancing,” works at layer 7, the application layer. TCP load balancing, known as “layer 4 load balancing”, works at the “network layer” and the “transport layer”. For example, LVS (Linux Virtual Server) and F5 (a hardware load balancer) are also layer 4 load balancers. Nginx is usually configured based on IP load, that is, layer 7 load, but later versions can also do TCP load

RPC architecture

Let’s start with the basic architecture of RPC services. We can clearly see that a complete RPC architecture contains four core components, namely the Client,Server,Client Stub and Server Stub. Let’s talk about the components: the Client, the caller of the service. A Server is a real service provider. Client stubs that store server address messages and package client request parameters into network messages that are sent to the server remotely over the network. The server stub receives the message sent from the client, unpacks the message, and invokes local methods.



RPC is mainly used in large enterprises, because large enterprises have many systems and complex business lines, and the efficiency advantage is very important. At this time, THE advantage of RPC is more obvious. This is done in real development, where projects are generally managed using Maven.

For example, we have an order processing system services, to declare all of its interface (here is the specific refers to the interface in Java), then the whole project packaged as a jar package, the server side to introduce the two party libraries, and then realize the corresponding function, the client side and you just need to introduce the second party libraries call.

Why do you do that? The main purpose is to reduce the size of the jar packages on the client side, because every time a package is released, too many JARS will always affect efficiency. It also decouples the client and server to improve the portability of the code.

Speaking of remote calls, something to understandAsynchronous synchronous

Synchronous versus asynchronous invocation

What is a synchronous call? What is an asynchronous call? A synchronous call is when the client waits for the call to complete and returns the result. An asynchronous call is one in which the client does not wait for the call to complete and return the result, but can still receive notification of the return result through callback functions, etc. If the client doesn’t care about the result, it can become a one-way call. This process is similar to the Callable and Runnable interfaces in Java. If we need to know the result of an asynchronous execution, we can use the Callable interface and obtain the result of an asynchronous execution through the Future class. If you don’t care about the result of the execution, you can just use the Runnable interface because it doesn’t return the result. Of course, callable is also ok, we don’t need to fetch the Future. (Future, if I remember correctly, is an internal while loop to check whether it is completed. If it is completed, it calls the callback method of the Future object to fill in the data. It is actually quite simple.

Generally speaking, RPC is fast, but due to the various underlying serialization languages, each system has to be compatible with a variety of languages, so there are also the underlying use of HTTP after all small and flexible cross-platform

HTTP

In fact, long ago, I defined the mode of enterprise development as HTTP interface development, which is often referred to as RESTful service interface. Indeed, in the case of few interfaces and less system-to-system interaction, it is a communication means often used to solve the early stage of information islands; The advantages are simplicity, directness and ease of development. Use the existing HTTP protocol for transport. We remember that when I was doing background development in the company during my undergraduate internship, I was mainly engaged in interface development, and I had to write a large interface document to strictly indicate what the input and output were. Make clear the request method of each interface, and the matters needing attention of request parameters, etc.

The interface may return a JSON string or an XML document. The returned information is then processed by the client so that development can proceed relatively quickly (the agreed serialization protocol). However, for large enterprises, when there are many internal subsystems and interfaces, the benefits of RPC framework will be shown. First of all, the long link does not need to shake hands three times like HTTP for each communication, which reduces the network overhead. Secondly, RPC frameworks generally have registries and rich monitoring management. Publishing, offline interfaces, dynamic extensions, etc., are unsentient, unified operations for callers (ZK, Eurka, NACos, etc.).

conclusion

There are many differences between RPC services and HTTP services. Generally speaking, RPC services are mainly targeted at large enterprises, while HTTP services are mainly targeted at small enterprises, because RPC is more efficient and HTTP service development iterations are faster. In short, the choice of framework is not determined by what is popular in the market, but by a complete evaluation of the entire project, so as to carefully compare the impact of the two development frameworks on the overall project, and finally decide what is best for the project. Make sure you don’t use RPC for every project just to use IT.