preface

Before a summary

In the last article we briefly touched on the master election and code implementation, some of the modules on the official website, and how they helped with the topic of previous articles. After all, you can’t just eat what the official website gives you, so you have to rely on yourself to find more information.

In the master election code, the key is to execute the master election through a thread, we have to keep listening to our master election, after the master is robbed, we need to block the thread, that is, the thread responsible for robbing the master. However, if the main thread is blocked, the whole program will stop running, so we use a child thread to grab the master for us, even if the master is not captured and blocked, the whole program can continue to execute.

The order of browsing the official website helps us to understand the framework to a large extent, but we also need to pay a lot of attention to the knowledge of extended classes, such as PaxOS, under the premise of a solid foundation. For this kind of knowledge, even if the official website cannot provide a perfect learning plan, it also gives us directions. In fact, in many cases, we are not weak in understanding, but suffering from too many things and have no way to start. When knowing the existence of these things to look up a lot of big guys to share, I believe it is not difficult to learn.

Previous link

High concurrency from scratch (part 1) – Basic concepts of Zookeeper

High concurrency from zero (2) – Zookeeper implementation of distributed lock

High concurrency from zero (3) : Establishment of Zookeeper cluster and leader election

High concurrency from zero (4) – Distributed queue of Zookeeper

High concurrency from zero (5) – Configuration center application of Zookeeper

High concurrency from scratch (6) – Zookeeper’s Master election

Content 1: RPC theory

1. What is RPC

(1) introduction of RPC

Remote Procedure Call Protocol (RPC)- Remote Procedure Call Protocol. Requests for services from remote computer programs over the network without knowledge of the protocols of the underlying network technology. It assumes the existence of a transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans both the transport layer and the application layer

For example, application 1 calls the service of application 2 over the network, regardless of the protocol of application 2. This is the simplest example.

② Why remote procedure call protocol?

By process, I mean business processing, computational tasks, or, more bluntly, programs, as if calling local methods

The difference between local call and long-distance call can be understood in the context of long-distance relationship, where everyone cohabitates and lives together, and remote call is like long-distance relationship, where you have to either come by car or come by car if you want to meet, and it’s also said that long-distance relationships generally have no good results. The network is needed in the middle of a remote call, so we can see

Because you're communicating over a network, the response is orders of magnitude slower and less reliable than it is locallyCopy the code

C/S mode of RPC

RPC adopts client-server structure and is realized through request-Response message mode. If the program of the server is updated, the client side also needs synchronous update to be used normally

④ The three processes of RPC

1. Communication agreement: Just like contacting friends in America in China, we will have various ways to contact them, such as making phone calls or going to America by ourselves. Addressing: We need to know how to contact, for example, we need to know the phone number to make a phone call, we need to know where the United States is to go by ourselves, that is, we need to know a specific address. 3. Data serialization: The function of serialization is also very simple. For example, we have already dialed the foreigner's phone or gone to him, but we speak Chinese and he speaks English, so we can't understand each other, so we can't communicateCopy the code

After all three procedures are done, RPC can work properly

⑤ Why do we use RPC

Microservices, the rise of distributed system architecture, to enable service reuse or interactive invocation between systems

⑥ Differences between RPC and other protocols

The difference between RPC and RMI, RMI remote method call is a concrete implementation of RPC in the OOP domain. That is, RPC is the parent and RMI is the child, and RMI can only be called in Java

WebService and restful interface calls are ACTUALLY RPC, but their message organization and message protocol are different

⑥ Comparison with MQ usage scenarios:

RPC is like making a phone call that requires a quick response (yes or no), whereas MQ is like sending a wechat QQ message that passes without waiting for a reply

The architectural differences are: MQ has an intermediate node Queue as the message storage point. RPC is called synchronously. For instances that need to wait for results to be returned, RPC processing is more natural. But it can't temporarily store requests like MQ, where the pressure piles up with the service Provider, and MQ can defer requests and let the handler process them at his or her own pace. But since messages are temporarily queued, the reliability of the system is affected by this queue, which is asynchronous and unidirectional. Sending messages is designed not to wait for message processing to complete, so MQ becomes unusable if there is a feature that needs to be designed for synchronous returnCopy the code

Therefore, if you are in a hurry and need to wait for the return result, or want to be convenient and simple to use, RPC is better. Its use is based on interface, similar to local call, and asynchronous programming is relatively complicated, such as gRPC. MQ is used when you don’t want the sender to be limited by the speed of the processor. As the business grows, the processing speed on the processing side is not enough to make synchronous calls to asynchronous messages

2. Procedure and protocol of RPC

① The flow of RPC

1. The client stub calls the client stub (just like calling a local method) and passes in parameters. 2. The Client Stub groups the parameters into messages and sends messages to the server through the system call 3. The local operating system of the client sends messages from the client machine to the server machine. 4. The server operating system sends the received data packets to the Client stub. 5. The result of process execution responds to the client with the same step in the opposite directionCopy the code

Stub: A stub in distributed computing is a piece of code that transforms parameters passed between Client and server during a remote procedure call

(2) Problems needing attention in the whole process

1. Development problem 2 of the Client stub and Server Stub. How to group parameters into messages and how to ungroup parameters 3. How to send messages 4. How to express process results and how to handle exceptions 5. How to implement secure access controlCopy the code

(3) Core concept terms

Client and Server, calls calls, replies, Services, programs, procedures, version, Marshalling and unmarshalling

About services, Programs, procedures, version

A network service consists of one or more remote assemblies, and a remote program implements one or more remote procedures. Procedures and procedure parameters, and results are defined in the program protocol specification, and a server may support multiple versions of remote programs to accommodate program protocol changes

(4) the RPC protocol

During RPC calls, parameters need to be grouped into messages for sending, and the receiver needs to ungroup the messages as parameters. Procedure results also need to be marshalled and unmarshalled. What constitutes a message and how it is represented constitute a message protocol. The message protocol used in RPC calls is called RPC protocol

RPC is the thing to be done, RPC protocol defines the format of the request response message, on top of TCP we can choose or customize the message protocol to complete our RPC interaction, at this time we can choose HTTP or HTTPS this general standard protocol, can also define their own message protocol according to their own needs (more)

RPC framework

RPC framework encapsulates the parameter marshalling and unmarshalling, the RPC program development framework of the bottom network communication, so that we can directly on its basis only need to focus on process code writing

Common RPC frameworks in the Java field: webService, CXF of Apache,Axis2, JAX-WS of Java, Dubbo of micro services, Spring Cloud, Apache Thrift, ICE, GRPC of Google, etc

① Service exposure of RPC framework

The remote provider needs to provide information related to service invocation in some form, including but not limited to service interface definition, data structure, or intermediate service definition file, such as Thrift IDL file, webService WSDL file, service caller needs to go through a certain path or information related to remote service invocation. In a nutshell, you need to tell someone how to call the service, right

② Remote proxy object of RPC framework

Proxy processing technology: the service used by the service caller is actually a local proxy for the remote service, which is implemented through dynamic proxy

Java provides at least two ways to provide dynamic code generation, one is JDK dynamic proxy, and the other is bytecode generation. Dynamic proxy is more convenient to use than bytecode generation, but the performance of dynamic proxy is worse than bytecode generation, and bytecode generation is much worse in code readability. So we typically sacrifice some performance to gain code readability and maintainability

③ Communication of RPC framework

RPC framework communication is independent of the specific protocol. It can be based on HTTP or TCP. WebService is RPC based on HTTP protocol, which has better cross-platform performance, but not as good as RPC based on TCP protocol

NIO does not necessarily have higher performance than BIO. NIO is designed to support high concurrency. NIO is non-blocking and is suitable for high concurrency scenarios with small data volumes

④ Serialization of RPC framework

Two aspects will directly affect the performance of RPC, one is the transmission mode, the other is serialization

Finally

This article is full of theory, we briefly went over what RPC is, the three processes, why we need it, its features and application scenarios, RPC flow and protocol definition and some small knowledge of its framework. And just to make it easier to explain in the next code

High Concurrency from scratch (8) – a simple implementation of RPC framework