手写rpc(代理模式)

github下载地址:https://github.com/Gefuxing/rpc.git

service:

package com.gefuxing.rpc.rpc04;

import com.gefuxing.rpc.service.Impl.SchoolServiceImpl;
import lombok.extern.log4j.Log4j2;

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


@Log4j2
public class RpcServiceRpc04 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        ServerSocket socket = new ServerSocket(9998);
        while (true) {
            Socket accept = socket.accept();
            process(accept);
            accept.close();
        }
    }

    private static void process(Socket s) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        InputStream inputStream = s.getInputStream();
        OutputStream outputStream = s.getOutputStream();
        ObjectInputStream dis = new ObjectInputStream(inputStream);
        ObjectOutputStream dos = new ObjectOutputStream(outputStream);


        String className = dis.readUTF();
        String name = dis.readUTF();
        Class<?>[] parameterTypes = (Class<?>[]) dis.readObject();
        Object[] args= (Object[]) dis.readObject();

        Class clazz = null;

        clazz= SchoolServiceImpl.class;


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

        dos.writeObject(invoke);
        log.info(invoke.toString());
        dos.flush();
    }


}

代理类:Stud:

package com.gefuxing.rpc.rpc04;

import java.io.InputStream;
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;


public class Stud04 {
    public static Object getStud(Class clazz) {
        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Socket s = new Socket("127.0.0.1", 9998);

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

                String className = clazz.getName();
                String name1 = method.getName();
                Class<?>[] parameterTypes = method.getParameterTypes();
                dos.writeUTF(className);
                dos.writeUTF(name1);
                dos.writeObject(parameterTypes);
                dos.writeObject(args);
                dos.flush();


                InputStream inputStream = s.getInputStream();
                ObjectInputStream dis = new ObjectInputStream(inputStream);
                Object o = dis.readObject();
                return o;
            }
        };
        Object o = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, handler);
        return o;
    }
}

client:

package com.gefuxing.rpc.rpc04;

import com.gefuxing.rpc.model.School;
import com.gefuxing.rpc.service.SchoolService;


import java.io.IOException;
import java.util.Scanner;

public class RpcClient04 {
    public static void main(String[] args) throws IOException {
        while (true) {
            SchoolService schoolService = (SchoolService) Stud04.getStud(SchoolService.class);
            Scanner input = new Scanner(System.in);
            int id = input.nextInt();
            School bySchool = schoolService.findBySchool(id);

            System.out.println("rpc04:" + bySchool);

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值