Claude 变成 7×24 值班员工了,但你算过它的“工资“吗?
2026/6/26 5:19:09
上一篇【第52篇】Netty秒杀系统——高并发网络接入层实战
下一篇【第54篇】Netty在Elasticsearch中的应用——分布式搜索引擎的网络通信
+------------------+ +------------------+ | Consumer | | Provider | | (NettyClient) | | (NettyServer) | +------------------+ +------------------+ | Transport层 | | Transport层 | | Exchange层 | | Exchange层 | | Protocol层 | | Protocol层 | +------------------+ +------------------+ | | +----------------------+ | Dubbo协议(基于Netty)Dubbo RPC协议帧: +------+------+------+------+------+------+------+------+ | 魔数(0xdabb,2B) | 标志位(1B) | 状态(1B) | ID(8B) | +------+------+------+------+------+------+------+------+ | 数据长度(4B) | +------+------+------+------+------+------+------+------+ | 序列化数据(body, variable) | +------+------+------+------+------+------+------+------+标志位含义:
// Dubbo 3.x中的NettyServerpublicclassNettyServerextendsAbstractServer{protectedvoiddoOpen(){bootstrap=newServerBootstrap();bootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childOption(ChannelOption.TCP_NODELAY,true).childOption(ChannelOption.SO_KEEPALIVE,true).childHandler(newChannelInitializer<SocketChannel>(){protectedvoidinitChannel(SocketChannelch){// Dubbo编解码器ch.pipeline().addLast("decoder",newDubboCountCodec());ch.pipeline().addLast("encoder",newDubboCountCodec());// Dubbo业务处理器ch.pipeline().addLast("handler",newNettyServerHandler());}});ChannelFuturef=bootstrap.bind(getBindAddress());}}// DubboCodec = 长度字段编解码 + Dubbo协议publicclassDubboCodecextendsExchangeCodec{protectedvoidencodeRequestData(Channelchannel,Objectoutput,ByteBufbuffer){// 序列化RPC调用数据:dubbo版本、服务名、方法名、参数等RpcInvocationinv=(RpcInvocation)output;ObjectOutputout=CodecSupport.getSerialization(channel.getUrl()).serialize(channel.getUrl(),buffer);out.writeUTF(inv.getAttachment(DUBBO_VERSION_KEY));out.writeUTF(inv.getAttachment(PATH_KEY));// 服务名out.writeUTF(inv.getAttachment(VERSION_KEY));out.writeUTF(inv.getMethodName());// 方法名out.writeUTF(inv.getParameterTypesDesc());// 参数类型// 写入参数值for(Objectarg:inv.getArguments()){out.writeObject(arg);}}}// 客户端发送心跳publicclassHeartbeatTimerTaskimplementsRunnable{publicvoidrun(){if(System.currentTimeMillis()-lastReadTime>heartbeat){// 发送心跳请求Requestreq=newRequest();req.setVersion(Version.getProtocolVersion());req.setTwoWay(true);req.setEvent(Request.HEARTBEAT_EVENT);channel.send(req);}}}// 服务端响应心跳if(request.isHeartbeat()){Responseresponse=newResponse();response.setEvent(Response.HEARTBEAT_EVENT);channel.send(response);return;}| 组件 | 作用 |
|---|---|
NettyServer/NettyClient | Transport层实现 |
DubboCodec | Dubbo协议编解码 |
ExchangeHandler | 请求-响应模型 |
| 心跳机制 | 60秒空闲检测 |
上一篇【第52篇】Netty秒杀系统——高并发网络接入层实战
下一篇【第54篇】Netty在Elasticsearch中的应用——分布式搜索引擎的网络通信