A, RPC Introduction
RPC, called the Remote Procedure Call, Remote Procedure Call that is, it is a computer communications protocol. It allows local services like calling the same remote service call. It can have different implementations. Such as RMI (Remote Method Invocation), Hessian, Http invoker and so on. In addition, RPC is language-independent.
RPC schematic
As shown above, it is assumed the sayHi Computer1 call () method, for purposes of call Computer1 the sayHi () method of the same method as local calls, call -> return. Subsequent calls can be seen but the call is sayHi Computer2 Computer1 in () method, the RPC shields the underlying implementation details, let the caller without paying attention to the communication network, data transmission and other details.
Second, implement an RPC framework
The above describes the core principles of RPC: RPC enables local applications simple and efficient server process (service) calls. It is mainly used in distributed systems. The IPC component in Hadoop. But how to achieve a RPC framework it?
Reflections from the following aspects, for reference:
1. Communication Model: A to B machines and machine model assumes that communication with a communication between A and B, typically in the Java-based or BIO NIO ;.
2. Process (service) Location: using a given communication mode, and determines the IP and port and method for determining the name of the specific process or method;
3. Remote proxy object: Method (service) is actually a local proxy remote method, and therefore may require a remote proxy object for Java, the Java remote proxy object can be used to achieve dynamic object that encapsulates the call to a remote method invocation of the local transfer;
4. The sequence of the object name, method name, parameters required for network transmission target information is converted into binary transmission, there may need different technical solutions serialization. Such as: protobuf, Arvo and so on.
Three, Java RPC framework to achieve
1, the technical solution
Comparative implement an RPC frame following the original program, using Socket communication, dynamic proxies sequence reflective native and Java.
2, RPC framework architecture
RPC architecture is divided into three parts:
1) service provider, run on the server to provide services and service implementation class interface definition.
2) service center, running on the server side is responsible for local services publish to a remote service, remote management services, services provided to consumers.
3) consumer service, running on the client, call the remote service through the remote proxy object.
3, the specific implementation
The service provider defines and implements the interface code is as follows:
1 2 3 4 5 |
|
HelloServices interface categories:
1 2 3 4 5 6 7 |
|
Service code implementation, the code is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
Service implementation class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
Remote client proxy object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Finally, as a test class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
operation result:
1 2 3 |
|
IV Summary
RPC is the essence of the message processing model, shielding RPC communication between different hosts underlying details so remote procedure call service are like local services.
Five areas for improvement
RPC simple framework to achieve here is to use the Java language development, highly coupled with the Java language, and Socket communication used is based on BIO achieved, IO is not efficient, as well as native Java serialization mechanism to account for too much memory, the operating efficiency It is not high. Improvement may be considered from the following methods.
- It can be based RPC JSON data frame transmission;
- NIO can be used directly or alternatively Netty BIO implemented;
- The use of open source serialization mechanism, such as Hadoop Avro and Google protobuf and so on;
- Zookeeper service registry can be used to manage, can make application more stable.
Transfer: https://www.cnblogs.com/codingexperience/p/5930752.html