Netty 实战篇:为自研 RPC 框架加入异步调用与 Future 支持

我们在上篇实现了一个轻量级 RPC 框架,现在要进一步优化 ------ 加入异步响应支持,让 RPC 通信变得真正高效、非阻塞、支持并发。


一、为什么需要异步调用?

上篇的 RPC 框架是"同步阻塞"的:

  • 每次发送请求后,必须等待服务端响应后才能返回结果

  • 对于高并发请求,会导致线程阻塞、资源浪费、吞吐低下

理想方案:客户端请求后立即返回一个 Future 对象,等响应来了再异步设置结果,主线程不用阻塞。


二、核心机制:RequestId + ChannelFuture + Map 缓存

我们引入一个全局 ConcurrentHashMap<Long, CompletableFuture<RpcResponse>>:

java 复制代码
public class RpcResponseRegistry {
    public static final Map<Long, CompletableFuture<RpcResponse>> futures = new ConcurrentHashMap<>();
}

每次发送请求时:

  • 生成唯一 requestId

  • 创建一个 CompletableFuture 存入 RpcResponseRegistry

  • 异步监听服务端响应,当收到响应时设置结果


三、发送请求(非阻塞)

java 复制代码
public class NettyClient {
    private static AtomicLong REQUEST_ID_GEN = new AtomicLong();

    public static CompletableFuture<RpcResponse> send(RpcRequest request, Channel channel) {
        long requestId = REQUEST_ID_GEN.incrementAndGet();
        request.setRequestId(requestId);

        CompletableFuture<RpcResponse> future = new CompletableFuture<>();
        RpcResponseRegistry.futures.put(requestId, future);

        channel.writeAndFlush(request); // 不阻塞等待
        return future;
    }
}

四、接收响应并完成 Future

服务端处理完成后写出响应:

java 复制代码
ctx.writeAndFlush(response);

客户端接收时通过 requestId 找到对应的 future 并完成它:

java 复制代码
public class RpcClientHandler extends SimpleChannelInboundHandler<RpcResponse> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponse response) {
        long requestId = response.getRequestId();
        CompletableFuture<RpcResponse> future = RpcResponseRegistry.futures.remove(requestId);
        if (future != null) {
            future.complete(response);
        }
    }
}

五、客户端调用示例

java 复制代码
// 获取动态代理
HelloService proxy = proxyFactory.getProxy(HelloService.class);

// 发起异步调用
CompletableFuture<RpcResponse> future = NettyClient.send(request, channel);
future.thenAccept(response -> {
    System.out.println("异步结果:" + response.getResult());
});

// 主线程可以干其他事情
System.out.println("我可以继续做别的事情了!");

六、增强:自动类型转换 + 异常处理

你可以在 Future 结果返回时:

  • 自动封装返回值类型(比如String,List<T>)

  • 自动处理异常并打印日志

java 复制代码
future.whenComplete((resp, ex) -> {
    if (ex != null) {
        System.err.println("RPC 异常: " + ex.getMessage());
    } else {
        System.out.println("返回结果: " + resp.getResult());
    }
});

七、总结

通过本篇的异步机制改造,我们的 Netty RPC 框架已经拥有了:

✅ 异步非阻塞调用

✅ CompletableFuture 响应映射

✅ 并发安全的请求响应管理

✅ 更高的吞吐能力和性能提升

相关推荐
Derek_Smart42 分钟前
基于Netty与Spring Integration的高并发工业物联网网关架构设计与实现
spring boot·物联网·netty
迢迢星万里灬21 小时前
Java求职者面试指南:微服务技术与源码原理深度解析
java·spring cloud·微服务·dubbo·netty·分布式系统·面试指南
Y_3_73 天前
Netty实战:从核心组件到多协议实现(超详细注释,udp,tcp,websocket,http完整demo)
linux·运维·后端·ubuntu·netty
安徽杰杰10 天前
能源即服务:智慧移动充电桩的供给模式创新
netty
安徽杰杰11 天前
新基建浪潮下:中国新能源汽车充电桩智慧化建设与管理实践
netty
迢迢星万里灬11 天前
Java求职者面试:微服务技术与源码原理深度解析
java·spring cloud·微服务·dubbo·netty·分布式系统
触角云科技12 天前
掌上充电站:基于APP/小程序的新能源汽车智慧充电管理
netty
安徽杰杰12 天前
智慧充电:新能源汽车智慧充电桩的发展前景受哪些因素影响?
netty
漫步者TZ17 天前
【Netty系列】解决TCP粘包和拆包:LengthFieldBasedFrameDecoder
java·网络协议·tcp/ip·netty
安徽杰杰17 天前
智慧赋能:移动充电桩的能源供给革命与便捷服务升级
netty