暴露服务
接口
public interface RpcService {
String getMsg(int param1, int param2);
}
实现
public class RpcServiceImpl implements RpcService {
@Override
public String getMsg(int param1, int param2) {
return param2 + " - " + param1 + " = " + (param2 - param1);
}
}
服务
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
public class ExposureServer {
private static final int PORT = 12345;
public static void main(String[] args) throws Exception {
RpcService service = new RpcServiceImpl();
new ExposureServer().exposure(service, PORT);
}
public void exposure(Object object, int port) throws Exception {
System.out.println("暴露接口实现:" + object.getClass().getName());
ServerSocket server = new ServerSocket(port);
while (true) {
Socket socket = server.accept();
new TaskHandler (socket, object).start();
}
}
class TaskHandler extends Thread {
private Socket socket;
private Object object;
private TaskHandler (Socket socket, Object object) {
this.socket = socket;
this.object = object;
}
@Override
public void run() {
try {
try {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
try {
String methodName = in.readUTF();
Class<?>[] parameterTypes = (Class<?>[]) in.readObject();
Object[] arguments = (Object[]) in.readObject();
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
try {
Method method = object.getClass().getMethod(methodName, parameterTypes);
Object result = method.invoke(object, arguments);
out.writeObject(result);
} catch (Throwable t) {
out.writeObject(t);
} finally {
out.close();
}
} finally {
in.close();
}
} finally {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
引用服务
接口
public interface RpcService {
String getMsg(int param1, int param2);
}
服务
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 ReferenceServer {
private static final String HOST = "127.0.0.1";
private static final int PORT = 12345;
public static void main(String[] args) {
RpcService service = reference(RpcService.class, HOST, PORT);
System.out.println(service.getMsg(1, 2));
}
@SuppressWarnings("unchecked")
private static <T> T reference(final Class<T> interfaceClass, final String host, final int port) {
System.out.println("获取远程实现:" + interfaceClass.getName());
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[]{interfaceClass}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
Socket socket = new Socket(host, port);
try {
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
try {
out.writeUTF(method.getName());
out.writeObject(method.getParameterTypes());
out.writeObject(arguments);
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
try {
Object result = in.readObject();
if (result instanceof Throwable) {
throw (Throwable) result;
}
return result;
} finally {
in.close();
}
} finally {
out.close();
}
} finally {
socket.close();
}
}
});
}
}