Minimal RPC Framework Implementation __java

Source: Internet
Author: User
"The General directory >>>" | "project Source >>>" RPC Overview

RPC (remote Proceduce call remoting) is generally used to implement method calls between systems deployed on different machines, enabling programs to transmit past access to remote system resources over the network, just as access to local system resources. RPC Call Procedure


1, client clients Invoke method implementation, responsible for initiating RPC calls.

2, Clientstub/sereverstub can be considered as a proxy object, shielding RPC calls the process of complex network processing logic, so that RPC transparent, so that the call remote method to call the local method.

3. Server service provides remote service.

Stub Primary Action 
Serialization: Responsible for serialization of data serialization. 
Network transmission: Data sending and receiving.
Note: Serverstub is also called skeleton.
RPC Implementation Remote Service Interface
Package Mongo.rpc.service.api;

/**
 * Author: Wang Junshu
 date:2018-02-04 10:09 *
 BLOG:HTTP://BLOG.CSDN.NET/DERRANTCM *
 github:https:/ /github.com/wang-jun-chao
 * All Rights Reserved!!!
 *
/public interface Ihello {public
    string SayHello (string info);
}
Remote Service Interface implementation Class (Server)
Package Mongo.rpc.service.impl;

Import Mongo.rpc.service.api.IHello;

/**
 * Author: Wang Junshu
 date:2018-02-04 10:09 *
 BLOG:HTTP://BLOG.CSDN.NET/DERRANTCM *
 github:https:// Github.com/wang-jun-chao
 * All Rights Reserved!!!
 *

/public class Helloserviceimpl implements Ihello {public
    string SayHello (string info) {
        string result = "H Ello: "+ info;
        SYSTEM.OUT.PRINTLN (result);
        return result;
    }
3. Server proxy Implementation (skeleton)

Build a ServerSocket service to listen for requests from clients.

Receives the requested data. (method name and parameter)

Invokes the corresponding service using reflection, based on the requested data (method name and parameters).

The response data for the output service.

Package mongo.rpc.server;
Import Mongo.rpc.service.api.IHello;

Import Mongo.rpc.service.impl.HelloServiceImpl;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import Java.lang.reflect.Method;
Import Java.net.ServerSocket;

Import Java.net.Socket;  /** * Proxy server for requests sent from the executing proxy client * Author: Wang Junshu * date:2018-02-04 10:10 * BLOG:HTTP://BLOG.CSDN.NET/DERRANTCM * Github:
 Https://github.com/wang-jun-chao * All Rights Reserved!!!

    * * Public class Rpcproxyserver {private Ihello hello = new Helloserviceimpl (); /** * Launch Proxy Server * @param port/public void Publisherserver (int port) {Try (ServerSocket SS = N EW ServerSocket (Port) {while (true) {//executing a request sent by the proxy client and returning the request to a try (Socket 
                        Socket = Ss.accept ()) {try (ObjectInputStream ois = new ObjectInputStream (Socket.getinputstream ())) {
                        String method = Ois.readutf (); object[] ObJS = (object[]) ois.readobject ();
                        class<?>[] types = new Class[objs.length];
                        for (int i = 0; i < types.length i++) {Types[i] = Objs[i].getclass ();
                        Method M = HelloServiceImpl.class.getMethod (method, types);

                        Object obj = M.invoke (hello, OBJS); Try (objectoutputstream oos = new ObjectOutputStream (Socket.getoutputstream ())) {Oos.writeobj
                            ECT (obj);
                        Oos.flush ();
                A catch (Exception e) {e.printstacktrace ());
        A catch (Exception e) {e.printstacktrace ()); }
    }
}
RPC client proxy Implementation (CLIENTSTUB)

Build a socket to connect to the remote service.

Send data to the remote service. (method name and method parameter)

The data that receives the remote service response.

Package mongo.rpc.client;
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.Socket; /** * Proxy client, for obtaining invocation results from service * Author: Wang Junshu * date:2018-02-04 10:12 * BLOG:HTTP://BLOG.CSDN.NET/DERRANTCM
 ://github.com/wang-jun-chao * All Rights Reserved!!!
     * * public class Rpcproxyclient {/** * Create proxy Object * * @param clazz interface to proxy * @param host server address * @param ports provided by Port server * @param <T> Interface class * Proxy object for @return interface/public <T> T Proxyclien T (class<t> Clazz, String host, int port) {return (T) clazz.cast (Proxy.newproxyinstance (clazz.getclassloader
            (), New Class[]{clazz}, new Invocationhandler () {/** * Proxy method call, for obtaining the result of the call from the service side. @Override public Object Invoke (Object Proxy, Method method, object[] args) throws Throwable {try (socket socket = new socket (host, port)) {Try (ObjectOutputStream Oos
                        = New ObjectOutputStream (Socket.getoutputstream ())) {Oos.writeutf (Method.getname ());
                        Oos.writeobject (args);

                        Oos.flush (); Try (objectinputstream ois = new ObjectInputStream (Socket.getinputstream ())) {return ois.read
                        Object ();
    }
                    }
                }
            }
        }));
 }
}
Service-Side publishing services
Package mongo.rpc;

Import Mongo.rpc.server.RpcProxyServer;

/**
 * Author: Wang Junshu
 date:2018-02-04 10:13 *
 BLOG:HTTP://BLOG.CSDN.NET/DERRANTCM *
 github:https:// Github.com/wang-jun-chao
 * All Rights Reserved!!!
 *
/public class Rpcserver {
    //Publishing Service public
    static void Main (string[] args) {
        rpcproxyserver server = NE W Rpcproxyserver ();
        Server.publisherserver (9999);
    }

client calls (clients)
Package mongo.rpc;

Import mongo.rpc.client.RpcProxyClient;
Import Mongo.rpc.service.api.IHello;

/**
 * Author: Wang Junshu
 date:2018-02-04 10:14 *
 BLOG:HTTP://BLOG.CSDN.NET/DERRANTCM *
 github:https:// Github.com/wang-jun-chao
 * All Rights Reserved!!!
 *
/public class Rpcclient {
    //Invoke service public
    static void Main (string[] args) {
        rpcproxyclient rpcclient = new Rpcproxyclient ();

        Ihello Hello = rpcclient.proxyclient (Ihello.class, "localhost", 9999);
        String s = Hello.sayhello ("Welcome to RPC");
        System.out.println (s);
    }

Run Results

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.