自己实现一个简易RPC调用框架

最近在看dubbo,去创始人之一梁飞大神的博客搂了几眼,找到这个demo自己实现了一下,关于dubbo就不再介绍了,快速开始、框架设计、设计原则官网都有,有兴趣可以自己看Dubbo官网

1. 服务接口

package com.yjj.my.api;

/**
 * @Description:
 * @Author: yinjunjie
 * @CreateDate: 2018/8/7 13:16
 * @Version: 1.0
 */
public interface HelloService {
    String sayHello(String name);
}

2. 服务实现

package com.yjj.my.api;

/**
 * @Description:
 * @Author: yinjunjie
 * @CreateDate: 2018/8/7 13:17
 * @Version: 1.0
 */
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "hello "+name;
    }
}

3. RPC实现

package com.yjj.my.framework;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description:
 * @Author: yinjunjie
 * @CreateDate: 2018/8/7 10:43
 * @Version: 1.0
 */
public class RpcFramework {

    /**
     * 暴露服务
     * @param service
     * @param port
     * @throws IOException
     */
    public static void exportService(final Object service, int port) throws IOException {
        if (service == null)
            throw new IllegalArgumentException("service instance == null");
        if (port <= 0 || port > 65535)
            throw new IllegalArgumentException("Invalid port " + port);
        System.out.println("Export service " + service.getClass().getName() + " on port " + port);
        ServerSocket server = new ServerSocket(port);
        for(;;) {
            try {
                final Socket socket = server.accept();
                new Thread(() -> {
                    try {
                        try {
                            ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
                            try {
                                String methodName = input.readUTF();
                                Class[] parameterTypes = (Class[])input.readObject();
                                Object[] arguments = (Object[])input.readObject();
                                ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
                                try {
                                    Method method = service.getClass().getMethod(methodName, parameterTypes);
                                    Object result = method.invoke(service, arguments);
                                    output.writeObject(result);
                                } catch (Throwable t) {
                                    output.writeObject(t);
                                } finally {
                                    output.close();
                                }
                            } finally {
                                input.close();
                            }
                        } finally {
                            socket.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 引用服务
     * @param interfaceClass
     * @param host
     * @param port
     * @param 
     * @return
     */
    public static   T referService(final Class interfaceClass, final String host, final int port){
        if (interfaceClass == null)
            throw new IllegalArgumentException("Interface class == null");
        if (! interfaceClass.isInterface())
            throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");
        if (host == null || host.length() == 0)
            throw new IllegalArgumentException("Host == null!");
        if (port <= 0 || port > 65535)
            throw new IllegalArgumentException("Invalid port " + port);
        System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);
        return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] {interfaceClass}, (proxy, method, arguments) -> {
            Socket socket = new Socket(host, port);
            try {
                ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
                try {
                    output.writeUTF(method.getName());
                    output.writeObject(method.getParameterTypes());
                    output.writeObject(arguments);
                    ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
                    try {
                        Object result = input.readObject();
                        if (result instanceof Throwable) {
                            throw (Throwable) result;
                        }
                        return result;
                    } finally {
                        input.close();
                    }
                } finally {
                    output.close();
                }
            } finally {
                socket.close();
            }
        });
    }
}

4. 暴露服务

package com.yjj.my.provider;

import com.yjj.my.api.HelloService;
import com.yjj.my.api.HelloServiceImpl;
import com.yjj.my.framework.RpcFramework;

import java.io.IOException;

/**
 * @Description:
 * @Author: yinjunjie
 * @CreateDate: 2018/8/7 10:38
 * @Version: 1.0
 */
public class Provider {
    public static void main(String[] args) throws IOException {
        HelloService service = new HelloServiceImpl();
        RpcFramework.exportService(service, 1234);
    }
}

5. 引用服务

package com.yjj.my.consumer;

import com.yjj.my.api.HelloService;
import com.yjj.my.framework.RpcFramework;

/**
 * @Description:
 * @Author: yinjunjie
 * @CreateDate: 2018/8/7 10:36
 * @Version: 1.0
 */
public class Consumer {
    public static void main(String[] args) throws InterruptedException {
        //此处返回其实是一个代理对象
        HelloService service = RpcFramework.referService(HelloService.class, "127.0.0.1", 1234);
        for (int i = 0; i < Integer.MAX_VALUE; i ++) {
            //当执行目标方法时会执行代理对象的invoke方法,关于代理详见我另一篇文章https://www.jianshu.com/p/b685466b1001
            String hello = service.sayHello("World" + i);
            System.out.println(hello);
            Thread.sleep(1000);
        }
    }

}

github地址:https://github.com/YinJune/dubbo-demo.git

你可能感兴趣的:(自己实现一个简易RPC调用框架)