Netty学习之实战RPC框架
先附上GitHub地址:https://github.com/kosamino/netty-root/tree/master/ym-netty-rpc
RPC的实现方式是本地通过远程代理对象调用远程服务。在互联网应用场景中,单体服务极度缺乏弹性伸缩能力,在大规模开发团队中也不便于开发管理。所以往往会把服务根据模块进行垂直拆分,也就是我们说的SOA服务化。服务拆分后系统跟系统直接的业务交互往往依赖于RPC框架进行通讯。
通常RPC的服务端会提供对应的接口jar包,客户端通过rpc框架功能拿到对应接口的代理实例,整个调用过程数据的包装和通讯都是透明的。
调用流程
首先先来分析下RPC流程是怎样的,如下图:
我们包含三部分,用户、Netty客户端,Netty服务端:
- 用户发起调用;
- Netty客户端包装请求;
- 客户端对请求进行序列化(对象转ByteBuf);
- 序列化后发送消息到服务端;
- 服务端会对请求进行反序列化解码成具体对象;
- 服务端根据客户端发送的请求解析并准备返回结果;
- 服务端对返回结果序列化为ByteBuf;
- 客户端收到返回信息;
- 客户端对返回信息反列化得到Object信息;
- 客户端把结果返回给用户调用方,完成整个请求。
包含技术
如上所示,就是整个RPC框架的简单流程,在这个流程中需要使用哪些技术呢?
- 动态代理:通过java Proxy技术拿到代理对象,invocationHandler实现数据协议包装和通讯。
- 序列化、反序列化
- 网络通讯:基于netty的客户端和服务端进行通讯可以获得很好的IO性能
- 反射:根据客户端请求参数通过反射技术实现服务端对应实例的方法调用
接下来我们就部分技术的使用进行代码片段分析。
动态代理
//todo 代理对象
QueryStudentClient client = (QueryStudentClient)rpcProxyFactory.factoryRemoteInvoker("localhost",8080,QueryStudentClient.class);
public class RpcProxyFactory<T> {
public T factoryRemoteInvoker(String host, int port, Class interfaces){
//动态代理
return (T) Proxy.newProxyInstance(interfaces.getClassLoader(),new Class[]{interfaces},
new RemoteInvocationHandler(host,port,interfaces));
}
}
public class RemoteInvocationHandler implements InvocationHandler {
private String host;
private int port;
private Class interfaces;
public RemoteInvocationHandler(String host, int port, Class interfaces) {
this.host = host;
this.port = port;
this.interfaces = interfaces;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//todo 封装消息
RpcContext rpcContext=new RpcContext();
rpcContext.setClassName(interfaces.getName());
rpcContext.setMethodName(method.getName());
rpcContext.setTypes(method.getParameterTypes());
rpcContext.setParams(args);
try {
//通讯
NettyClient client=new NettyClient(host,port);
client.connect();
return client.sendData(rpcContext);
}catch (Exception e){
}
return null;
}
}
序列化、反序列化
@Override
protected void initChannel(SocketChannel sc) throws Exception {
handler = new NettyClientHandler(latch);
HessianEncode hessionEncodeHandler=new HessianEncode();
HessianDecode hessionDecodeHandler= new HessianDecode();
LengthFieldPrepender fieldEncoder=new LengthFieldPrepender(2);
// LengthFieldBasedFrameDecoder fieldDecoder = new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2);
// 出站
sc.pipeline().addLast(fieldEncoder);
sc.pipeline().addLast(hessionEncodeHandler);
//入站 LengthFieldBasedFrameDecoder多线程下不安全,因此使用new
sc.pipeline().addLast(new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2));
sc.pipeline().addLast(hessionDecodeHandler);
sc.pipeline().addLast(handler);
}
可以看到在pipeline先后添加了:基于消息头的长度设置的粘包半包处理handler、序列化工具、反序列化工具,此处序列化使用的是Hessian。
反射技术
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
RpcContext model=(RpcContext)msg;
Class clazz=null;
if(Registry.map.containsKey(model.getClassName())){
clazz=Registry.map.get(model.getClassName());
}
Object result=null;
try {
Method method=clazz.getMethod(model.getMethodName(),model.getTypes());
result=method.invoke(clazz.newInstance(),model.getParams());
}catch (Exception e){
e.printStackTrace();
}
ctx.channel().writeAndFlush(result);
}
可以看到服务端根据客户端传来的类名,去Registry的map中获取已注册的类,然后根据返回类型、方法名、参数进行反射调用。
Netty异步调用线程协作问题
使用netty实现客户端发送需要注意的点:
通过Netty的channel调用写数据writeAndFlush 写的事件以及收到响应之后的channelRead事件都是会异步执行,所以需要注意线程协作的问题。可以使用countdowlacth来实现主线程等待channelread执行完之后才去获取收到的响应对象。
/**
* 客户端发送数据方法
* @param rpcRequest
* @return
* @throws InterruptedException
*/
public Object sendData(RpcContext rpcRequest) throws InterruptedException {
ChannelFuture cf = this.getChannelFuture();//单例模式获取ChannelFuture对象
if (cf.channel() != null && cf.channel().isActive()) {
latch=new CountDownLatch(1);
clientInitializer.reLatch(latch);
cf.channel().writeAndFlush(rpcRequest);
latch.await();
}
return clientInitializer.getServerResult();
}
}
// 客户端从服务端读取数据完成
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
result=msg;
System.out.println("返回数据读取完毕");
latch.countDown();
}
由此实现了线程协作,否则调用结果无法得到返回。
参考: |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程使用 AI 从 0 到 1 写了个小工具
· 快收藏!一个技巧从此不再搞混缓存穿透和缓存击穿
· AI 插件第二弹,更强更好用
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇