首页 > 其他分享 >230106_50_RPC底层原理

230106_50_RPC底层原理

时间:2023-01-06 20:34:15浏览次数:55  
标签:java bill 230106 50 RPC IUserService oos import com

  • 目前Stub只能拿到一个接口,IUserService 。如果新增接口后,需要重新修改,支持任意接口。具体优化如下:

  • Stub优化

package com.bill.rpc06;

import com.bill.rpc.common.IUserService;
import com.bill.rpc.common.User;

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;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/6 - 01 - 06 - 19:40
 * @Description: com.bill.rpc06
 * @version: 1.0
 */
public class Stub {
    public static Object getStub(final Class clazz){
        InvocationHandler h = new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Socket s = new Socket("127.0.0.1",8888);

                ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

                String className = clazz.getName();
                String methodName = method.getName();
                Class[] parametersTypes = method.getParameterTypes();

                oos.writeUTF(className);
                oos.writeUTF(methodName);
                oos.writeObject(parametersTypes);
                oos.writeObject(args);
                oos.flush();

                ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
                User user = (User) ois.readObject();

                oos.close();
                s.close();
                return user;
            }
        };
        Object o = Proxy.newProxyInstance(IUserService.class.getClassLoader(),new Class[] {IUserService.class},h);
        System.out.println(o.getClass().getName());
        System.out.println(o.getClass().getInterfaces()[0]);
        return (IUserService)o;

    }
}

  • client优化
package com.bill.rpc06;

import com.bill.rpc.common.IUserService;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/6 - 01 - 06 - 19:40
 * @Description: com.bill.rpc06
 * @version: 1.0
 */
public class Client {
    public static void main(String[] args) {
        // 需要什么接口,传入一个对应接口的参数,IUserService.class
        IUserService service = (IUserService) Stub.getStub(IUserService.class);
        System.out.println(service.findUser(123));
    }
}
  • server优化
package com.bill.rpc06;

import com.bill.rpc.common.IUserService;
import com.bill.rpc.common.User;
import com.bill.rpc01.UserServiceImpl;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Auther: wangchunwen
 * @Date: 2023/1/6 - 01 - 06 - 19:40
 * @Description: com.bill.rpc06
 * @version: 1.0
 */
public class Server {
    private static boolean running = true;

    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        ServerSocket ss = new ServerSocket(8888);
        while (running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();
    }

    private static void process (Socket s) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        ObjectInputStream ois = new ObjectInputStream(in);

        String className = ois.readUTF();
        String methodName = ois.readUTF();
        Class[] parameterTypes = (Class[]) ois.readObject();
        Object[] args = (Object[]) ois.readObject();

        Class clazz = null;
        // 从服务注册表找到具体的类
        clazz = UserServiceImpl.class;

        Method method = clazz.getMethod(methodName,parameterTypes);
        Object o = (Object) method.invoke(clazz.newInstance(),args);

        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(o);
        oos.flush();
    }
}

标签:java,bill,230106,50,RPC,IUserService,oos,import,com
From: https://www.cnblogs.com/wcwblog/p/17031532.html

相关文章