Implement an RPC framework by yourself

Source: Internet
Author: User
RPC Overview
Remote Call protocol. How useful it is, and how common it is. Approximate process: 1. Call the client handle and transmit parameters. 2. messages such as encapsulation parameters are transmitted to the remote host in the network transmission format. 3. obtain the message from the server handle and parse the parameters. 4. execute the code to be called in the server segment and return the result to the server handle. the server handle encapsulates the returned results and transmits them to the client over the network. 6. client parsing and other visible issues include communication methods, handle implementation, and message encapsulation and resolution (serialization and deserialization)
RPC communication

Use SOCKET directly to use TCP/IP or UDP protocol, such as RMI

Take the HTTP protocol, Hessian. The advantage is that you do not need to enable the firewall again.

In the following example, Java Socket is used. In this way, we can see more basic content.


Dynamic RPC proxy

In the handle section, we can use a dynamic proxy to call the remote method when calling the client method.


RPC serialization and deserialization

There are also many types of serialization mechanisms, such as the built-in serialization mechanism of Java, and classes that implement the serializable interface can be serialized.

For the principle of Java serialization, see the following blog:

Http://www.java3z.com/cwbwebhome/article/article8/862.html

It is the consistent style of processing binary data in Java. It defines the flag to indicate the length of the content that follows, and then the specific content.


Java deserialization.

The best way is to check the source code of objectinputstream.

The general process is:

1. readobject0 entry

2. Read and parse the data in a segment. Generally, the class readclass is first read, and the class interface of this class is read.

3. readordinaryobject uses newinstance to create an instance

4. Use the DESC. setobjfieldvalues content in defaultreadfields to assign values to fields of this instance.

5. This is a cyclical process, knowing to restore all the content of the object.


There can also be other methods. For example, the WebService technology uses the SOAP protocol for transmission, and uses a method similar to the XML format to describe the message content (so the message content will be bloated)


Hessian is serialized in binary mode. It uses its own rules, which can be implemented in other languages and is a simple and efficient protocol.

The link to the details is as follows: http://hessian.caucho.com/doc/hessian-serialization.html


In the following example, objectinputstream is used for serialization.


Self-built RPC framework Server

Start the socket and create a processing thread for each request:

Public class startup {public static final int Port = 8080; public static void main (string [] ARGs) {exportrpc ();} /*** export RPC interface */Private Static void exportrpc () {try {serversocket Ss = new serversocket (9001); While (true) {socket S = ss. accept (); New rpcthread (s ). start () ;}} catch (ioexception e) {e. printstacktrace ();}}}

Then define the message content to be transmitted, including the content that the client wants to tell the server, including at least, the called interface name, The called method name, and method parameters. The object is as follows:

public class RpcObject implements Serializable{private static final long serialVersionUID = 1L;private Class c;private String methodName;private Object[] args;public RpcObject() {}public RpcObject(Class c, String methodName, Object[] args) {this.c = c;this.methodName = methodName;this.args = args;}public Class getC() {return c;}public void setC(Class c) {this.c = c;}public String getMethodName() {return methodName;}public void setMethodName(String methodName) {this.methodName = methodName;}public Object[] getArgs() {return args;}public void setArgs(Object[] args) {this.args = args;}}

The processing thread on the server will go out of the parameter object, and then get the specific instance object and call the method to be called through reflection.

Public class rpcthread extends thread {private socket s; Public rpcthread (socket s) {This. S = s ;}@ overridepublic void run () {objectinputstream is = NULL; objectoutputstream OS = NULL; try {is = new objectinputstream (S. getinputstream (); // obtain the remote call parameter, including the Interface Name, call method, and method parameter rpcobject = (rpcobject) is. readobject (); // construct the implementation class of the interface, and then call object o = GetObject (rpcobject. GETC (); object REO = executemethod (O, rpcobject. getmethodname (), rpcobject. getargs (); // output returned OS = new objectoutputstream (S. getoutputstream (); OS. writeobject (REO); OS. flush ();} catch (ioexception e) {e. printstacktrace ();} catch (classnotfoundexception e) {e. printstacktrace ();} finally {try {is. close (); OS. close ();} catch (ioexception e) {e. printstacktrace () ;}}/ *** use the reflection technology to execute the method, return the returned value * @ Param o * @ Param methodname * @ Param ARGs * @ return */private object executemethod (Object o, string methodname, object [] ARGs) {object objr = NULL; class [] cs = new class [args. length]; for (INT I = 0; I <args. length; I ++) {object Arg = ARGs [I]; CS [I] = Arg. getclass ();} Try {method M = O. getclass (). getmethod (methodname, CS); objr = m. invoke (O, argS);} catch (securityexception e) {e. printstacktrace ();} catch (nosuchmethodexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace ();} catch (invocationtargetexception e) {e. printstacktrace ();} return objr;}/*** obtain the instance * @ Param C * @ return */private object GetObject (Class C) {object o = NULL Based on the Interface Name; try {<span style = "white-space: pre"> </span> O = confmonitor. conf. get (C. getname ()). newinstance (); <span style = "white-space: pre"> </span>} catch (instantiationexception e) {<span style = "white-space: pre "> </span> E. printstacktrace (); <span style = "white-space: pre"> </span>} catch (illegalaccessexception e) {<span style = "white-space: pre "> </span> E. printstacktrace (); <span style = "white-space: pre"> </span>} return o ;}}
The server configuration file mainly refers to the implementation class corresponding to the configuration interface:

/*** Simulate the configuration. In the actual framework, most of them are configured using XML. For example, Hessian is configured on the web. * @ author cdwangzijian */public class confmonitor {public static Map <string, class> conf = new hashmap <string, class> () in the servlet attribute of XML (); static {Conf. put ("com. prince. RPC. service. ihello ", helloimpl. class );}}

Client

With dynamic proxy, every method call is changed to remote call.

public class ProxyFactory {public static <T> T create(Class<T> c, String ip, int port) { InvocationHandler handler = new RpcProxy(ip, port, c);return (T) Proxy.newProxyInstance(c.getClassLoader(),                new Class[] {c },                handler);}}

The proxy encapsulates remote parameters, transmits the called interface, method name, and parameter to the remote device, and obtains the returned value.

/*** Client interface proxy ** when the client interface method is called, The method name and method parameters are used as parameters. * Send it to the remote service for execution, and then obtain the returned value * @ author cdwangzijian **/public class rpcproxy implements invocationhandler, serializable {private string IP; private int port; private class C; private Static final long serialversionuid = 1l; Public rpcproxy (string IP, int port, Class C) {This. IP = IP; this. port = port; this. C = C;}/*** dynamic proxy class. when calling the interface method, convert it to calling this method */@ overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {object o = NULL; // used as the return value // use socket to call the remote service socket S = new socket (IP, Port ); // assemble it into an object that retains the class, method name, and parameters to be called, and then serialize it and send it to remote rpcobject = new rpcobject (C, method. getname (), argS); objectoutputstream OS = NULL; objectinputstream is = NULL; try {OS = new objectoutputstream (S. getoutputstream (); OS. writeobject (rpcobject); OS. flush (); // obtain the returned result is = new objectinputstream (S. getinputstream (); O = is. readobject ();} catch (exception e) {e. printstacktrace ();} finally {OS. close (); is. close () ;}return o ;}}
The last part is the call part to obtain the proxy class.

public class RpcClient {public static void main(String[] args) {String ip = "localhost";int port = 9001;IHello hello = ProxyFactory.create(IHello.class, ip, port);System.out.println(hello.sayHello("wzj"));}}

The following code is used as the test interface and implementation class:

public interface IHello {String sayHello(String name);}

public class HelloImpl implements IHello {@Overridepublic String sayHello(String name) {return "hello:" + name;}}

The download path of the source code is as follows:

Http://download.csdn.net/detail/three_man/8059871


Implement an RPC framework by yourself

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.