一个最简单的RPC框架实现包含暴露服务、引用服务、Socket通讯三部分。
RPC框架简易实现
public class RpcFramework {
public static void exportService(final Object service, int port) throws Exception {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
final Socket socket = serverSocket.accept();
new Thread(new Runnable() {
@Override
public void run() {
ObjectInputStream reader = null;
ObjectOutputStream writer = null;
try {
reader = new ObjectInputStream(socket.getInputStream());
String methodName = reader.readUTF();
Class[] argumentsType = (Class[]) reader.readObject();
Object[] arguments = (Object[]) reader.readObject();
Method method = service.getClass().getMethod(methodName, argumentsType);
Object result = method.invoke(service, arguments);
writer = new ObjectOutputStream(socket.getOutputStream());
writer.writeObject(result);
} catch (Exception e) {
if (null != writer) {
try {
writer.writeObject(e);
} catch (IOException e1) {
e1.printStackTrace();
}
}
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}
public static <T> T referenceService(Class<T> interfaceClass, final String host, final int port) {
return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket socket = new Socket(host, port);
ObjectOutputStream writer = null;
ObjectInputStream reader = null;
try {
writer = new ObjectOutputStream(socket.getOutputStream());
writer.writeUTF(method.getName());
writer.writeObject(method.getParameterTypes());
writer.writeObject(args);
reader = new ObjectInputStream(socket.getInputStream());
return reader.readObject();
} finally {
if (null != writer) {
writer.close();
}
if (null != reader) {
reader.close();
}
}
}
});
}
}
接口
public interface HelloService {
String sayHello(String name);
}
public interface ByeService {
String sayBye(String name);
}
接口实现
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return name + " say hello";
}
}
public class ByeServiceImpl implements ByeService {
@Override
public String sayBye(String name) {
return name +" bye bye";
}
}
服务提供方
public class Provider {
public static void main(String[] args) throws Exception {
final HelloService helloService = new HelloServiceImpl();
new Thread(new Runnable() {
@Override
public void run() {
try {
RpcFramework.exportService(helloService, 20880);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
final ByeService byeService = new ByeServiceImpl();
new Thread(new Runnable() {
@Override
public void run() {
try {
RpcFramework.exportService(byeService, 20881);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
服务消费方
public class Consumer {
public static void main(String[] args) {
HelloService helloService = RpcFramework.referenceService(HelloService.class, "127.0.0.1", 20880);
System.out.println(helloService.sayHello("tom"));
ByeService byeService = RpcFramework.referenceService(ByeService.class, "127.0.0.1", 20881);
System.out.println(byeService.sayBye("jack"));
}
}
输出
tom say hello
jack bye bye