• RPC与HTTP


    RPC(Remote Procedure Call)即远程过程调用,它允许像调用本地服务一样调用远程服务

    HTTP是一种基于TCP的网络传输协议

    RPC并没有规定数据传输格式,这个格式可以任意指定,不同的RPC协议,数据格式不一定相同,RPC方式需要在API层面进行封装,限制了开发的语言环境

    从速度来看,RPC要比HTTP更快,虽然底层都是TCP,但是HTTP协议的信息往往比较臃肿

    难度来看,RPC实现较为复杂,http相对比较简单,

    灵活性来看,如果需要更加灵活,跨语言、跨平台,显然HTTP更合适

    JAVA实现的RPC

    实现的思路是将本地方法调用转换成采用动态代理方式,在代理中建立socke连接,将接口名称,方法名称,参数类型,参数值传到服务端,服务端拿到这些数据后,利用反射调用实例方法,并将结果传回到客户端

    public interface HelloService {
        String sayHi(String name, int number);
    }
    public class HelloServiceImpl implements HelloService {
        public String sayHi(String name , int number) {
            return "Hi, " + name + " , "+number;
        }
    }
    public class RPCClient<T> {
        @SuppressWarnings("unchecked")
        public static <T> T getRemoteProxyObj(final Class<?> serviceInterface, final InetSocketAddress addr) {
            //1.将本地的接口调用转换成JDK的动态代理,在动态代理中实现接口的远程调用
            return (T) Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[]{serviceInterface},
                    new InvocationHandler() {
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            Socket socket = null;
                            ObjectOutputStream output = null;
                            ObjectInputStream input = null;
                            try {
                                // 2.创建Socket客户端,根据指定地址连接远程服务提供者
                                socket = new Socket();
                                socket.connect(addr);
     
                                // 3.将远程服务调用所需的接口类、方法名、参数列表等编码后发送给服务提供者
                                output = new ObjectOutputStream(socket.getOutputStream());
                                output.writeUTF(serviceInterface.getName()); //接口名
                                output.writeUTF(method.getName());  //方法名称
                                output.writeObject(method.getParameterTypes()); //参数类型
                                output.writeObject(args); //参数列表
     
                                // 4.同步阻塞等待服务器返回应答,获取应答后返回
                                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 interface Server {
        public void stop();
        public void start() throws IOException;
        public void register(Class serviceInterface, Class impl);
        public boolean isRunning();
        public int getPort();
    }
    public class ServiceCenter implements Server {
        private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
     
        private static final HashMap<String, Class> serviceRegistry = new HashMap<String, Class>();
     
        private static boolean isRunning = false;
     
        private static int port;
     
        public ServiceCenter(int port) {
            this.port = port;
        }
     
        public void stop() {
            isRunning = false;
            executor.shutdown();
        }
     
        public void start() throws IOException {
            ServerSocket server = new ServerSocket();
            server.bind(new InetSocketAddress(port));
            System.out.println("start server");
            try {
                while (true) {
                    // 1.监听客户端的TCP连接,接到TCP连接后将其封装成task,由线程池执行
                    executor.execute(new ServiceTask(server.accept()));
                }
            } finally {
                server.close();
            }
        }
     
        public void register(Class serviceInterface, Class impl) {
            serviceRegistry.put(serviceInterface.getName(), impl);
        }
     
        public boolean isRunning() {
            return isRunning;
        }
     
        public int getPort() {
            return port;
        }
     
        private static class ServiceTask implements Runnable {
            Socket clent = null;
     
            public ServiceTask(Socket client) {
                this.clent = client;
            }
     
            public void run() {
                ObjectInputStream input = null;
                ObjectOutputStream output = null;
                try {
                    // 2.将客户端发送字节流反序列化成对象,反射调用服务实现者,获取执行结果
                    input = new ObjectInputStream(clent.getInputStream());
                    String serviceName = input.readUTF();
                    String methodName = input.readUTF();
                    Class<?>[] 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);
     
                    // 3.将执行结果反序列化,通过socket发送给客户端
                    output = new ObjectOutputStream(clent.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 (clent != null) {
                        try {
                            clent.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
     
            }
        }
    }
    public class Test {
     
        public static void main(String[] args) throws IOException {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        Server serviceServer = new ServiceCenter(8088);
                        serviceServer.register(HelloService.class, HelloServiceImpl.class);
                        serviceServer.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            HelloService service = RPCClient.getRemoteProxyObj(HelloService.class, new InetSocketAddress("localhost", 8088));
            System.out.println(service.sayHi("test",232));
        }
    }
  • 相关阅读:
    清理计算机硬盘
    DIY-组装
    go函数类型的使用
    go同步互斥锁
    Go读写文件
    go mod
    go html
    channel
    arp和rarp协议
    自己实现的反射
  • 原文地址:https://www.cnblogs.com/moris5013/p/10944004.html
Copyright © 2020-2023  润新知