In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the principle of java rpc framework". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the principle of java rpc framework"?
Basic concepts of RPC
RPC is the remote procedure call protocol, its function is the remote call between the client and the server, just like the local call, let the server service, functional uniqueness, load hot traffic.
Implementation of RPC Framework
RPC enables local applications to simply and efficiently invoke processes (services) in the server. It is mainly used in distributed systems. Such as the IPC component in Hadoop. But how do you implement a RPC framework?
Think from the following aspects, for reference only:
1. Communication model: suppose that the communication is between A machine and B machine, and there is a communication model between An and B, which is generally based on BIO or NIO; in Java.
two。 Process (service) location: using a given mode of communication, and determining the IP and port and method name to determine a specific process or method
3. Remote proxy object: the locally called 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 calling remote method calls.
4. Serialization, the network transmission of object names, method names, parameters and other object information needs to be converted into binary transmission, here may require different serialization technical solutions. Such as: thrift,protobuf,Arvo, etc.
Here is a simple native java socket to implement rpc calls to facilitate a deeper understanding of the principle of rpc
Service class interface
Package socketrpc;2 3 public interface IHello {4 String sayHello (String string); 5}
Service implementation class
1 package socketrpc.server; 2 3 import socketrpc.IHello; 4 5 public class HelloServiceImpl implements IHello {6 7 @ Override 8 public String sayHello (String string) {9 return "Hello:" .concat (string); 10} 11}
Client implementation
Package socketrpc.client;import socketrpc.IHello;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.net.InetSocketAddress;import java.net.Socket;public class RpcClientProxy implements InvocationHandler {private Class serviceInterface; private InetSocketAddress addr; public RpcClientProxy (Class serviceInterface, String ip,String port) {this.serviceInterface = serviceInterface; this.addr = new InetSocketAddress (ip, Integer.parseInt (port)) } public T getClientIntance () {return (T) Proxy.newProxyInstance (serviceInterface.getClassLoader (), new Class [] {serviceInterface}, this);} @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Socket socket = null; ObjectOutputStream output = null; ObjectInputStream input = null; try {/ / 2. Create a Socket client and connect to the remote service provider socket = new Socket (); socket.connect (addr); / / 3 according to the specified address. The interface class, method name, parameter list, etc. required for remote service invocation are encoded and sent to the service provider output = new ObjectOutputStream (socket.getOutputStream ()); output.writeUTF (serviceInterface.getName ()); output.writeUTF (method.getName ()); output.writeObject (method.getParameterTypes ()); output.writeObject (args); / / 4. Synchronous blocking waits for the server to return a reply, which returns input = new ObjectInputStream (socket.getInputStream ()); return input.readObject ();} finally {if (socket! = null) socket.close (); if (output! = null) output.close (); if (input! = null) input.close () } public static void main (String [] args) {RpcClientProxy client = new RpcClientProxy (IHello.class, "localhost", "6666"); IHello hello = (IHello) client.getClientIntance (); System.out.println (hello.sayHello ("socket rpc"));}}
Server-side implementation
Package socketrpc.server;import socketrpc.IHello;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.lang.reflect.Method;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;public class RpcServer {private static final HashMap [] parameterTypes = (Class []) input.readObject (); Object [] arguments = (Object []) input.readObject () Class serviceClass = serviceRegistry.get (serviceName); if (serviceClass = = null) {throw new ClassNotFoundException (serviceName + "not found");} Method method = serviceClass.getMethod (methodName, parameterTypes); Object result = method.invoke (serviceClass.newInstance (), arguments); output = new ObjectOutputStream (socket.getOutputStream ()) Output.writeObject (result);}} catch (Exception e) {e.printStackTrace ();} finally {if (output! = null) {try {output.close ();} catch (IOException e) {e.printStackTrace () }} if (input! = null) {try {input.close ();} catch (IOException e) {e.printStackTrace () }} if (socket! = null) {try {socket.close ();} catch (IOException e) {e.printStackTrace () }} public static void main (String [] args) throws IOException {new RpcServer (6666). Register (IHello.class,HelloServiceImpl.class). Run ();}}
After starting the server and client, the running result is as follows
Simple explanation of client code
Private Class serviceInterface; 2 private InetSocketAddress addr; 3 4 public RpcClientProxy (Class serviceInterface, String ip,String port) {5 this.serviceInterface = serviceInterface; 6 this.addr = new InetSocketAddress (ip, Integer.parseInt (port)); 7} 8 9 public T getClientIntance () {10 return (T) Proxy.newProxyInstance (serviceInterface.getClassLoader (), new Class [] {serviceInterface}, this); 11}
Pass in the interface class and ip port. When the getClientIntance method is called, the current interface is proxied. The actual calling method is
1 @ Override 2 public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {3 4 Socket socket = null; 5 ObjectOutputStream output = null; 6 ObjectInputStream input = null; 7 8 try {9 / / 2. Create a Socket client and connect to the remote service provider at the specified address: 10 socket = new Socket (); 11 socket.connect (addr); 12 13 / / 3. Encode the interface classes, method names, parameter lists, etc. required for remote service invocation and send them to the service provider 14 output = new ObjectOutputStream (socket.getOutputStream ()); 15 output.writeUTF (serviceInterface.getName ()); 16 output.writeUTF (method.getName ()); 17 output.writeObject (method.getParameterTypes ()); 18 output.writeObject (args) 19 20 / / 4. Synchronous blocking waits for the server to return a reply. After getting the reply, 21 input = new ObjectInputStream (socket.getInputStream ()); 22 return input.readObject (); 23} finally {24 if (socket! = null) socket.close (); 25 if (output! = null) output.close (); 26 if (input! = null) input.close (); 27} 28}
Follow these steps
Create a socket and make a three-way connection handshake with the remote [socket.connect (addr)].
Encapsulate socket output stream [ObjectOutputStream].
3: output class name, method name, parameter type and parameter value to server.
4: get the socket input stream and wait for server to return the result.
Simple explanation of server code
1 public RpcServer (int port) {2 this.port = port;3} 4 5 public RpcServer register (Class serviceInterface, Class impl) {6 serviceRegistry.put (serviceInterface.getName (), impl); 7 return this;8}
Initialize the server and register the service class with hashMap [simulate spring context]
1 public void run () throws IOException {2 3 ServerSocket server = new ServerSocket (); 4 server.bind (new InetSocketAddress (port)); 5 System.out.println ("start server"); 6 ObjectInputStream input = null; 7 ObjectOutputStream output = null; 8 Socket socket=null; 9 try {10 while (true) {11 socket= server.accept () 12 input = new ObjectInputStream (socket.getInputStream ()); 13 14 String serviceName = input.readUTF (); 15 String methodName = input.readUTF (); 16 System.out.println (methodName); 17 Class [] parameterTypes = (Class []) input.readObject (); 18 Object [] arguments = (Object []) input.readObject () 19 Class serviceClass = serviceRegistry.get (serviceName); 20 if (serviceClass = = null) {21 throw new ClassNotFoundException (serviceName + "not found"); 22} 23 Method method = serviceClass.getMethod (methodName, parameterTypes); 24 Object result = method.invoke (serviceClass.newInstance (), arguments) 25 output = new ObjectOutputStream (socket.getOutputStream ()); 26 output.writeObject (result); 27} 28} catch (Exception e) {29 e.printStackTrace (); 30} finally {31 if (output! = null) {32 try {33 output.close () 34} catch (IOException e) {35 e.printStackTrace (); 36} 37} 38 if (input! = null) {39 try {40 input.close (); 41} catch (IOException e) {42 e.printStackTrace () 43} 44} 45 if (socket! = null) {46 try {47 socket.close (); 48} catch (IOException e) {49 e.printStackTrace (); 50} 51} 52} 53 54}
The server executor does the following things:
1: bind the port, blocking waiting for the client to call [socket = server.accept ()].
Encapsulate the input stream socket.getInputStream ().
3: get the interface name, method name, parameter type, and parameter value from the input stream.
4: find the service class in hashmap during initialization.
5: reflection acquires the service implementation class method and invokes the service according to the request parameters.
Encapsulates the output stream ObjectOutputStream and returns the result.
So far, the RPC service for the entire simple socket implementation has been completed and can be optimized.
1: serialization is limited, native serialization can only serialize the service class that implements the [Serializable] interface, and when serializing complex objects, the content is huge and the efficiency is extremely low, so it needs an efficient serialization protocol to serialize parameter methods and other necessary requests for input parameters.
Due to the limited performance of 2:BIO, the socket server uses the default BIO to block the acquisition of input stream, which is inefficient, so it is necessary to use asynchronous non-blocking server schemes such as NIO, such as netty,mina and java nio.
3: in the large enterprise RPC solution, the persistent connection between the client and the server needs to be maintained all the time, otherwise the three-way handshake and four waves will have to be re-performed each time. Creating tcp connections frequently is a great loss to machine performance, and apache pool2 connection pooling can be used for socket connections.
4: server load, you need to consider service automatic discovery, so that the client can dynamically perceive the changes of the server without restarting, so as to achieve hot deployment. We can use the method of regular automatic polling, zookeeper and so on.
5: server service class execution exception, client awareness, etc.
At this point, I believe you have a deeper understanding of "what is the principle of the java rpc framework?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.