7年前,梁飞公布了一个微型的rpc,这个rpc核心就是一个类,2个方法,但重点我们要探讨是细节的设计和质量一些问题
package com.rpc;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* Created by luozhonghua on 2018/5/30.
*/
public class RpcFramework { /**
* 暴露服务
*
* @param service 服务实现
* @param port 服务端口
* @throws Exception
*/
public static void export(final Object service, int port) throws Exception {
if (service == null)
throw new IllegalArgumentException("service instance == null");
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Export service " + service.getClass().getName() + " 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 methodName = input.readUTF();
Class<?>[] parameterTypes = (Class<?>[])input.readObject();
Object[] arguments = (Object[])input.readObject();
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
try {
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();
}
}
}
/**
* 引用服务
*
* @param <T> 接口泛型
* @param interfaceClass 接口类型
* @param host 服务器主机名
* @param port 服务器端口
* @return 远程服务
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception {
if (interfaceClass == null)
throw new IllegalArgumentException("Interface class == null");
if (! interfaceClass.isInterface())
throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");
if (host == null || host.length() == 0)
throw new IllegalArgumentException("Host == null!");
if (port <= 0 || port > 65535)
throw new IllegalArgumentException("Invalid port " + port);
System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);
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 output = new ObjectOutputStream(socket.getOutputStream());
try {
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();
}
}
});
}
}
这个微型rpc 相信绝大多少开发者一眼就看出基于jdk socket编写的2个方法,export 服务暴露(dubbo称提供者),refer 服务引用或说调用(dubbo称为消费者)
虽然很简单,实际上rpc理论上的思路基本对等了,但真正自己写出同样功能的代码,会像这个模板一样的考虑周全?
从大的方面来讲,这个2个方法很优雅
1、尽早失败(断言)设计
2、变量尽量不可变(Immutable Class)
写过多线程编码的人应该清楚,不变类有天然的并发共享优势,减少同步或复制,而且可以有效帮忙分析线程安全的范围,
就算是可变类,对于从构造函数传入的引用,在类中持有时,最好将字段final,以免被中途误修改引用,
不要以为这个字段是私有的,这个类的代码都是我自己写的,不会出现对这个字段的重新赋值,
要考虑的一个因素是,这个代码可能被其他人修改,他不知道你的这个弱约定,final就是一个不变契约
3、防御性异常规范
3.1、异常处理类不能有异议,是参与异常就用参数异常
3.2、场景多种异常用超类异常覆盖,并且不能让异常中断业务
demo: https://github.com/luozhonghua/dubbo-spiDemo
延伸阅读
Dubbo spi extension 源码原理--Dubbo源码系列解读(1)
Dubbo spi 设计模式&用例源码--Dubbo源码系列解读(2)
Dubbo spi extension 关系导图--Dubbo源码系列解读(3)
Dubbo spi extension & Schema--Dubbo源码系列解读(4)