根据RPC框架原理,手写一个mini版RPC
2018-09-30 本文已影响0人
swaiter
我的个人博客地址:http://swaiter.github.io
根据RPC框架原理,手写一个mini版RPC
在实际后台服务开发中,RPC框架具有很大的优势,其中当前dubbo已经受到大家的关注和认可,现在开始进行手动码一个简单的RPC框架。
项目背景
假如项目背景为:
订单服务(开发者A负责)需要调用商品服务(开发者B负责),那么开发者B会和A约定调用API,以接口的形式提供给A。通常都是B把API上传到Maven私服,然后B开始写API的实现,A只需要引入API依赖进行开发即可。
screenshot home动手实现RPC
首先项目框架如下图
screenshot home订单查询(service模块)
直接调用rpc方法,获取productService实现对象,这个对象通过动态代理的方式获取,如果想了解动态代理和反射知识点,可以参考我的另外两个开源的源码地址
我的git地址 https://github.com/SWaiter/proxy.git
我的git地址 https://github.com/SWaiter/reflect.git
rpc的实现在api模块中
public class Main {
public static void main(String[] args) {
IProductService productService = (IProductService) com.yoozoo.api.Main.rpc(IProductService.class);
Product product = productService.queryById(1025);
System.out.println(product);
}
}
商品服务(api模块)
1、首先是bean
public class Product implements Serializable{
private long id;
private String name;
private double price;
//省去set、get 、tostring 方法
}
2、定义查询接口
public interface IProductService {
public Product queryById(long id);
}
3、接口实现
public class ProductService implements IProductService{
@Override
public Product queryById(long id) {
Product product = new Product();
product.setId(id);
product.setName("wangshan");
product.setPrice(12211.2125);
return product;
}
}
4、rpc方法实现
public static Object rpc(final Class iProductServiceClass) {
return Proxy.newProxyInstance(iProductServiceClass.getClassLoader(), new Class[]{iProductServiceClass}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Socket socket = new Socket("127.0.0.1", 19088);
String apiClassName = iProductServiceClass.getName();
String methodName = method.getName();
Class[] parameterTypes = method.getParameterTypes();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeUTF(apiClassName);
objectOutputStream.writeUTF(methodName);
objectOutputStream.writeObject(parameterTypes);
objectOutputStream.writeObject(args);
objectOutputStream.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
Object o = objectInputStream.readObject();
objectInputStream.close();
objectOutputStream.close();
socket.close();
return o;
}
});
}
5、rpc启动
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(19088);
while (true) {
Socket socket = serverSocket.accept();
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
String apiClassName = objectInputStream.readUTF();
String methodName = objectInputStream.readUTF();
Class[] parameterTypes = (Class[]) objectInputStream.readObject();
Object[] args4method = (Object[]) objectInputStream.readObject();
Class clazz = null;
if (apiClassName.equals(IProductService.class.getName())) {
clazz = ProductService.class;
}
Method method = clazz.getMethod(methodName, parameterTypes);
Object invoke = method.invoke(clazz.newInstance(), args4method);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeObject(invoke);
objectOutputStream.flush();
objectInputStream.close();
objectOutputStream.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
启动
1、首先启动api中的service中的Main.main方法
2、然后启动service中的Main.main方法
3、就可以获取服务对象
com.intellij.rt.execution.application.AppMain com.yoozoo.service.Main
Product{id=1025, name='wangshan', price=12211.2125}
Process finished with exit code 0
总结
就这一个简单的rpc框架就完成了。有什么问题可以QQ联系交流。(787324413)