一、RPC的简化版原理如下图(核心是代理机制)。

1.本地代理存根: Stub
2.本地序列化反序列化
3.网络通信
4.远程序列化反序列化
5.远程服务存根: Skeleton
6.调用实际业务服务
7.原路返回服务结果
8.返回给本地调用方
二、新建一个模块rpc-demo-consumer

主要的依赖是rpc-demo-api,即业务服务定义的api接口,rpc-core是核心依赖包。

三、启动方法
在启动方法中注入业务服务接口UserService和OrderService,使用的注解是@RpcConsumer,本文主要的功能就是如何实现这个注解。
在启动的时候会导入ConsumerConfig的配置类。
使用的ApplicationRunner来测试方法调用。

四、ConsumerConfig用来启动消费者服务。

五、ConsumerBootstrap实现了ApplicationContextAware接口,注入ApplicationContext,即Spring的应用上下文,获取bean。

在start()方法中,
1.获取所有的bean;
2.遍历每个bean,查找bean中是否有字段用了@RpcConsumer注解;
3.对被标记了的@RpcConsumer的字段进行遍历;
4.获取到该字段的类型,全限定名;
5.使用动态代理的方式创建消费者对象;
6.把代理对象赋值给空字段。

查找bean中的字段是否使用了@RpcConsumer注解,通过循环遍历的方式进行判断。不断向父类寻找,是因为bean本身可能被Spring进行了CGLIB增强,形成了一个代理类。

使用Proxy创建代理对象。

六、代理对象RpcInvocationHandler中定义了远程调用方法的逻辑。

在invoke()方法中定义具体实现:1.判断是否本地方法,就不需要走远程调用;
2.封装请求参数;
3.发起远程调用;
4.处理调用结果。

本地方法判断:即Object类中方法不需要被代理,不需要发起远程调用请求。

RpcRequest封装了远程调用消费者端请求的数据结构:接口全限定名称、方法名称、方法参数。

通过post方法发起远程调用,使用的是OkHttpClient。

RpcResponse定义了消费者端的响应体数据结构:状态、响应结果数据、异常对象。

处理调用结果:如果成功,就对调用结果数据进行反序列化;如果失败就封装异常信息。

七、对各种情况的基本测试:
1.类对象参数的远程调用;2.本地方法的远程调用;3.基本参数类型的远程调用;4. String类型参数的远程调用;5. 另一个业务范围(订单服务)的远程调用;6. 嵌套注入的远程调用;7. 异常测试

还有哪些问题?
- 默认使用的Java的动态代理,还有Spring的切面代理,CGLI增强,ByteBuddy的字节码
- 当前还是localhost请求,后续应该是走网络请求。
- 基本类型参数并不完整,如果是Double类型参数可能会报错。