Sample code for Java to implement a simple RPC framework
1、 Introduction to RPC
RPC, fully known as remote procedure call, is a computer communication protocol. It allows remote services to be called as if they were local services. It can be implemented in different ways. Such as RMI (remote method call), Hessian, HTTP invoker, etc. In addition, RPC is language independent.
The most important thing that RPC framework does is to encapsulate the communication details between the caller and the callee. The client agent is responsible for returning value packets and other information to the method name parameters of the calling method, organizing them into messages according to the communication protocol and sending them to the server. The server parses the messages and executes the corresponding methods according to the information transmitted by the client, Then, the return value installation protocol is organized into a message and sent to the client, which is parsed by the client.
RPC schematic
As shown in the above figure, suppose that computer1 is calling the sayhi () method. For computer1, calling the sayhi () method is like calling the local method, and calling C > returns. However, it can be seen from the subsequent calls that computer1 calls the sayhi () method in computeR2. RPC shields the underlying implementation details, so that the caller does not need to pay attention to network communication, data transmission and other details.
2、 Implementation of RPC framework
The above introduces the core principle of RPC: RPC enables local applications to simply and efficiently call processes (services) in the server. It is mainly used in distributed systems, such as IPC components in Hadoop. But how to implement an RPC framework?
Consider from the following aspects for reference only:
1. Communication model: it is assumed that the communication is between machine a and machine B, and there is a communication model between machine a and machine B. in Java, it is generally based on bio or NiO;.
2. Process (service) positioning: determine the specific process or method by using the given communication mode and determining the IP, port and method name;
3. Remote proxy object: the locally invoked method (service) is actually the local proxy of the remote method, so a remote proxy object may be required. For Java, the remote proxy object can be implemented using Java's dynamic object, which encapsulates the call of the remote method;
4. Serialization: the network transmission of object information such as object name, method name and parameters needs to be converted into binary transmission. Different serialization technical schemes may be required here. Such as protobuf, ARVO, etc.
3、 RPC framework implemented in Java
1. Implementation technical scheme
The following uses the original scheme to implement the RPC framework, using socket communication, dynamic proxy and reflection, and Java Native serialization.
2. RPC framework architecture
RPC architecture is divided into three parts:
1) The service provider, running on the server side, provides service interface definitions and service implementation classes.
2) The service center, running on the server side, is responsible for publishing local services into remote services, managing remote services and providing them to service consumers.
3) The service consumer runs on the client and invokes the remote service through the remote proxy object.
3. Concrete implementation
The service provider interface definition and implementation code is as follows:
Helloservices interface implementation class:
The code of service center is as follows:
Service center implementation class:
Remote proxy object for client:
Finally, test class:
Operation results:
4、 Summary
RPC is essentially a message processing model. RPC shields the communication details between different hosts at the bottom and allows processes to call remote services like local services.
5、 Areas for improvement
The simple RPC framework implemented here is developed using the Java language, which is highly coupled with the Java language, and the socket used for communication is based on bio. The IO efficiency is not high. In addition, the Java Native serialization mechanism occupies too much memory and the operation efficiency is not high. The following methods can be considered for improvement.
1. RPC framework based on JSON data transmission can be adopted;
2. NiO can be used or netty can be directly used to replace bio;
3. Use open source serialization mechanisms, such as Hadoop Avro and Google protobuf;
4. Service registration can be managed by zookeeper, which can make the application more stable.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.