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);
}
}
}