Warm tips: the following content is personal understanding, if there is an error please correct! Thank you

What is RPC?

  • Remote Procedure Call Protocol (RPC) :
    • We have a producer server and a consumer server, with different applications A and B deployed. When we want to call a function or method provided by the producer server’s application through the consumer server, because these applications are not in the same memory space, they cannot be called directly, so we need to transfer data requests through the network. For example, when we write a program on our own machine, others cannot call it directly. At this time, we need to call it through remote service. RPC came into being!

    Microservice architecture is a way of developing a single application as a set of small services. (So we know that the average small business doesn’t use microservices.)

    • Encyclopedia official Answers

      RPC is a protocol for requesting services from a remote computer program over a network without requiring knowledge of the underlying network technology. The RPC protocol assumes the existence of some 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. RPC makes it easier to develop applications including network distributed multiprograms.

RPC uses client/server mode. The requester is a client and the service provider is a server. First, the client calling process sends an invocation message with process parameters to the server process and then waits for the reply message. On the server side, the process stays asleep until the call message arrives. When a call message arrives, the server gets the process parameters, calculates the result, sends the reply message, and waits for the next call message. Finally, the client calling process receives the reply message, gets the process result, and the call execution continues.

What problem does RPC solve?

Technical problems: 1. Communication problems: Data is transmitted between the client and server through TCP. 2. Addressing problem: How to invoke applications between servers, and how to find the IP address and port of the corresponding application.

Advantages: 1. Decoupling: Each system is separated and deployed on different servers. 2. Avoid duplication of development: To avoid duplication of development of a system, you can call 3 directly when necessary. Convenient maintenance

What is the specific workflow of RPC? (Purely personal understanding, to be corrected by the big guy)

  • RPC needs to have three parties: producers, consumers, and a central administration. The producer is the service provider, the consumer is the service request, and the central management center (hereinafter referred to as the central center) acts as an intermediary, which can be understood as our housing intermediary.
  • When producers want to use microservices, they need to register their identities with the central government first, including IP, port number, classes or methods and other information, which need to be provided to the central government for records. At the same time, the central government generates a key to uniquely identify the producer according to its own encryption rules.
  • Similarly, when consumers want to use micro-services, they also need to register their identity with the central government first, including IP, port number, access method and other information, which need to be provided to the central government for records. At the same time, the central government will provide consumers with certain permissions based on the information provided by consumers, and determine which producers the consumers can call according to the permissions, and generate keys accordingly.
  • After registering, consumers can obtain IP, port number, key and other information of the corresponding producer by obtaining the permission, and store these information in the local cache.
  • When consumers request, consumers will be read from the local cache data information effectively, such as carrying the corresponding key producers, via HTTP, TCP protocols for data transmission (transmission here with the help of flow operation, serialization and deserialization technology, do not do temporarily, may go right into the baidu), producer receives the request, reflex agents, The key is first checked for correctness, and then the actual request processing is performed.
  • At this point, a normal simple RPC process is finished.

But lest you be surprised, RPC still has some guaranteed jobs

  • Heartbeat mechanism: in order to prevent the producer server from breaking down due to heart attack or being in a bad mood one day, we have forced the producer to send a heartbeat to the central government every few seconds (it can be designed according to the specific business) to prove that the producer is healthy. If a producer didn’t send a heartbeat, the central can forgive the little naughty, but didn’t send a heartbeat, if three consecutive central will take the initiative to mark the producers have abnormal, synchronous updating information to consumers in the cache, and check the producers server is common illness, and trigger the alarm mechanism to remind is program of change the bug apes for its treatment.
  • Synchronization mechanism: When a producer server adds, reduces, or changes servers, the central government synchronizes producer information to the consumer cache.
  • Feedback and inspection mechanism: Due to the heartbeat mechanism and synchronization mechanism without immediacy, so most of the time, producers server problems, tend to be consumers first found that when consumers connect producers, a connection is not successful, try reconnection, many times after the connection is not successful, will look for other producers server information available from the local cache for data requests, At the same time, the producer server information that fails to connect will be reported to the central government, indicating that the producer server is lazy and not working. Upon receiving the feedback, the central authority immediately checks the producer server for the cause of the exception and tries to trigger the alarm mechanism.

Such a general RPC idea is out, the text is not easy to understand, please refer to the picture below:

What aspects of RPC can improve performance optimization?

1. Cache: There are many kinds of cache technologies. The process cache includes the JDK’s own ConcurrentHashMap cache and the rich Ehcache cache. Distributed caches include Redis and Tair. Choosing the right cache based on our business needs can greatly improve performance.

2. Operation of data transmission stream: As a high-performance NIO communication framework, Netty has been found in many RPC frameworks. We also use it as a communication server. 3. Serialization and deserialization:

Serialization is the process of converting an object’s state information into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent store. Later, you can recreate the object by reading or deserializing its state from the store.

At present, Internet companies widely use Protobuf, Thrift, Avro and other mature serialization solutions to build RPC frameworks, which are time-tested solutions. Of course, the specific serialization needs to be carried out according to our business. 4. Communication protocol: different communication protocols have different transmission performance under different data structures and different data volumes, which should be determined according to the actual situation. I have an idea to design a communication protocol imitating Tomcat, which may improve performance in some aspect, but it has not been implemented yet.

Recommend a few good blogs:

  • How to design and use caching elegantly?
  • How to use Netty to implement RPC
  • Java serialization tool comparison
  • Comparison of several communication protocols in RPC

Just entered nuggets, please pay attention to!