Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the underlying principle of the RPC framework?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces you what is the underlying principle of the RPC framework, the content is very detailed, interested friends can refer to, hope to be helpful to you.

1. The concept of RPC framework

RPC (Remote Procedure Call)-remote procedure call, which calls different services through network communication, jointly supports a software system and is the cornerstone technology for the implementation of micro-services.

Using RPC can decouple the system, facilitate maintenance, and increase the ability of the system to handle requests.

The above is a simple software system structure, we split out the user system and order system as a service, let different sites to call.

You only need to introduce the interface packages of each service, and invoking RPC services in code is the same as calling local methods. I was surprised when I first came into contact with this method. I clearly called java language methods (java as an example, now the RPC framework generally supports multiple languages), how can remote services be called?

2. Principle analysis of RPC framework

Recently I wrote a simple RPC framework KRPC, principle analysis combined with the code, are the source code of the framework.

2.1 process Overview

As shown in the figure above, I summarize a RPC invocation process into five processes in the figure above, three on the left for the client process and two on the right for the server process.

The following is the parsing of each process

2.2 client invocation

When the service caller invokes the service, it generally initializes and obtains the server address user call through the configuration file / configuration center.

/ / user service interface public interface UserService {public User genericUser (Integer id,String name,Long phone);} / caller / / Service initialization KRPC.init ("D:\\ krpc\\ service\\ demo\\ conf\\ client.

It must be confusing to contact the RPC calling method at the beginning, isn't it an interface, calling directly should not have any effect, and I didn't introduce the implementation package.

With this doubt, let's move on to the next knowledge point, dynamic agent.

2.3 dynamic agent

Dynamic agent means as its name, it acts for you to do things for you, dynamic agent read this article "detailed explanation of the three agent modes in Java".

Above we don't talk about directly calling a method in an interface, and there is no implementation class calling with that interface, so how does the method work?

You can see that the service for the user service is created by the ProxyFactory agent project and is bound to a proxy processor in the ProxyFactory#create () method.

@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {/ / Construction request request Request request = new Request ();. Return RequestHandler.request (serviceName, request,returnClass);}

This class implements the InvocationHandler interface (the dynamic proxy technology provided by JDK), and each time the interface method is called, it is eventually handled by the handler.

This link usually gets some information about the method, such as the method name, the method parameter type, the method parameter value, and returns the object type.

At the same time, this link will provide serialization function, the general RPC network transmission using TCP (even if using HTTP) transmission, here these parameters should be encapsulated into our defined data interface for transmission.

2.4 Network transmission

After we deal with the method parameters, we will use to initiate a network request, and those that use tcp transmission will use socket communication for transmission, which is a synchronous blocking scheme used in my open source project to make requests, or we can use some non-blocking schemes to make requests, which will be more efficient.

On the underlying principles of the RPC framework is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report