暴露服务:
package com.saiarea; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket; public class RpcService {
public static void export(int port) throws Exception {
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Export service on port " + port);
ServerSocket server = new ServerSocket(port);
for(;;) {
try {
final Socket socket = server.accept();
new Thread(new Runnable() {
@Override
public void run() {
try {
try {
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
String className = input.readUTF();
String methodName = input.readUTF();
Class<?>[] parameterTypes = (Class<?>[])input.readObject();
Object[] arguments = (Object[])input.readObject();
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
Object service = Class.forName(className+"Impl").newInstance();
Method method = service.getClass().getMethod(methodName, parameterTypes);
Object result = method.invoke(service, arguments);
output.writeObject(result);
} catch (Throwable t) {
output.writeObject(t);
} finally {
output.close();
}
} finally {
input.close();
}
} finally {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
} public static void main(String[] args) throws Exception{
Test test = new TestImpl();
RpcService.export(8081);
}
}
引用服务
package com.saiarea; 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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; public class RpcRequestService {
static String host = "";
static int port = 0; public static <T> T proxyFactory(final Class<T> myClass) throws Exception {
List<Class> interfacesList = new ArrayList(myClass.getInterfaces().length + 1);
interfacesList.addAll(Arrays.asList(myClass.getInterfaces()));
interfacesList.add(myClass);
Class[] classes = new Class[myClass.getInterfaces().length + 1];
interfacesList.toArray(classes);
Object proxy = Proxy.newProxyInstance(RpcRequestService.class.getClassLoader(), classes, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
Socket socket = new Socket(host, port);
try {
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
output.writeUTF(myClass.getName());
output.writeUTF(method.getName());
output.writeObject(method.getParameterTypes());
output.writeObject(arguments);
ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
try {
Object result = input.readObject();
if (result instanceof Throwable) {
throw (Throwable) result;
}
return result;
} finally {
input.close();
}
} finally {
output.close();
}
} finally {
socket.close();
}
}
});
System.out.println("test:" + proxy);
return (T) proxy;
} public static void main(String[] args) throws Exception {
host = "127.0.0.1";
port = 8081;
Test service = proxyFactory(Test.class);
Long hello = service.testRpc("测试动态加载代理");
System.out.println("我收到了," + hello);
}
}