{"id":59761,"date":"2026-01-14T12:50:23","date_gmt":"2026-01-14T04:50:23","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/59761.html"},"modified":"2026-01-14T12:50:23","modified_gmt":"2026-01-14T04:50:23","slug":"netty%e5%85%a5%e9%97%a8%e8%af%a6%e8%a7%a3%ef%bc%9a%e9%ab%98%e6%80%a7%e8%83%bd%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%a1%86%e6%9e%b6%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/59761.html","title":{"rendered":"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<h3>\u7b2c1\u7ae0&#xff1a;Netty\u6982\u8ff0\u4e0e\u6838\u5fc3\u4ef7\u503c<\/h3>\n<h4>1.1 Netty\u662f\u4ec0\u4e48&#xff1f;<\/h4>\n<p>Netty\u662f\u4e00\u4e2a\u5f02\u6b65\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6&#xff0c;\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u53ef\u7ef4\u62a4\u7684\u9ad8\u6027\u80fd\u534f\u8bae\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u3002\u5b83\u672c\u8d28\u4e0a\u662fJava NIO\u7684\u5c01\u88c5\u4e0e\u589e\u5f3a&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u5957\u7b80\u6d01\u800c\u5f3a\u5927\u7684API&#xff0c;\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91&#xff0c;\u800c\u4e0d\u5fc5\u9677\u5165\u590d\u6742\u7684\u7f51\u7edc\u7f16\u7a0b\u7ec6\u8282\u3002<\/p>\n<h4>1.2 Netty\u7684\u8bde\u751f\u80cc\u666f\u4e0e\u6f14\u8fdb<\/h4>\n<p>Netty\u7531\u97e9\u56fd\u5de5\u7a0b\u5e08Trustin Lee\u4e8e2004\u5e74\u521b\u5efa&#xff0c;\u6700\u521d\u662f\u4e3a\u4e86\u89e3\u51b3\u5f53\u65f6Java NIO API\u7684\u590d\u6742\u6027\u548c\u4e0d\u8db3\u3002\u7ecf\u8fc7\u591a\u5e74\u7684\u53d1\u5c55&#xff0c;Netty\u5df2\u7ecf\u6210\u4e3aJava\u7f51\u7edc\u7f16\u7a0b\u9886\u57df\u7684\u4e8b\u5b9e\u6807\u51c6&#xff0c;\u88ab\u4f17\u591a\u77e5\u540d\u9879\u76ee\u91c7\u7528&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7248\u672c\u6f14\u8fdb&#xff1a;<\/p>\n<ul>\n<li>\n<p>Netty 3.x&#xff1a;\u57fa\u4e8eJava NIO\u7684\u57fa\u7840\u6846\u67b6<\/p>\n<\/li>\n<li>\n<p>Netty 4.x&#xff1a;\u5b8c\u5168\u91cd\u6784&#xff0c;\u6027\u80fd\u63d0\u5347\u663e\u8457<\/p>\n<\/li>\n<li>\n<p>Netty 5.x&#xff1a;\u56e0\u67b6\u6784\u590d\u6742\u6027\u88ab\u653e\u5f03&#xff0c;\u793e\u533a\u56de\u5f524.x<\/p>\n<\/li>\n<li>\n<p>\u5f53\u524d\u4e3b\u6d41&#xff1a;Netty 4.1.x\u7cfb\u5217<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u91c7\u7528Netty\u7684\u77e5\u540d\u9879\u76ee&#xff1a;<\/p>\n<p>text<\/p>\n<p>1. Apache Cassandra &#8211; \u5206\u5e03\u5f0fNoSQL\u6570\u636e\u5e93<br \/>\n2. Elasticsearch &#8211; \u5206\u5e03\u5f0f\u641c\u7d22\u5f15\u64ce<br \/>\n3. Apache Dubbo &#8211; RPC\u6846\u67b6<br \/>\n4. RocketMQ &#8211; \u6d88\u606f\u4e2d\u95f4\u4ef6<br \/>\n5. Spark &#8211; \u5927\u6570\u636e\u8ba1\u7b97\u6846\u67b6<br \/>\n6. gRPC &#8211; \u9ad8\u6027\u80fdRPC\u6846\u67b6<br \/>\n7. ZooKeeper &#8211; \u5206\u5e03\u5f0f\u534f\u8c03\u670d\u52a1\n<\/li>\n<\/ul>\n<h4>1.3 \u4e3a\u4ec0\u4e48\u9009\u62e9Netty&#xff1f;<\/h4>\n<h5>1.3.1 \u4e0e\u4f20\u7edfBIO\u7684\u5bf9\u6bd4<\/h5>\n<p>java<\/p>\n<p>\/\/ \u4f20\u7edfBIO\u670d\u52a1\u5668\u793a\u4f8b&#xff08;\u6bcf\u8fde\u63a5\u4e00\u7ebf\u7a0b&#xff09;<br \/>\npublic class BioServer {<br \/>\n    public static void main(String[] args) throws IOException {<br \/>\n        ServerSocket serverSocket &#061; new ServerSocket(8080);<br \/>\n        while (true) {<br \/>\n            Socket socket &#061; serverSocket.accept(); \/\/ \u963b\u585e<br \/>\n            new Thread(() -&gt; {<br \/>\n                \/\/ \u5904\u7406\u8fde\u63a5<br \/>\n            }).start();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u95ee\u9898&#xff1a;\u7ebf\u7a0b\u8d44\u6e90\u6d88\u8017\u5927&#xff0c;\u4e0d\u9002\u5408\u9ad8\u5e76\u53d1<\/p>\n<h5>1.3.2 \u4e0e\u539f\u751fNIO\u7684\u5bf9\u6bd4<\/h5>\n<p>java<\/p>\n<p>\/\/ \u539f\u751fNIO\u9700\u8981\u5904\u7406\u5927\u91cf\u7ec6\u8282<br \/>\nSelector selector &#061; Selector.open();<br \/>\nServerSocketChannel serverChannel &#061; ServerSocketChannel.open();<br \/>\nserverChannel.configureBlocking(false);<br \/>\nserverChannel.register(selector, SelectionKey.OP_ACCEPT);<\/p>\n<p>\/\/ Netty\u5c01\u88c5\u4e86\u8fd9\u4e9b\u590d\u6742\u6027<br \/>\nEventLoopGroup bossGroup &#061; new NioEventLoopGroup();<br \/>\nServerBootstrap b &#061; new ServerBootstrap();<br \/>\nb.group(bossGroup)<br \/>\n .channel(NioServerSocketChannel.class);<\/p>\n<h5>1.3.3 Netty\u7684\u6838\u5fc3\u4f18\u52bf<\/h5>\n<li>\n<p>\u9ad8\u6027\u80fd&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u57fa\u4e8eNIO\u7684\u975e\u963b\u585eI\/O<\/p>\n<\/li>\n<li>\n<p>\u96f6\u62f7\u8d1d\u6280\u672f<\/p>\n<\/li>\n<li>\n<p>\u5185\u5b58\u6c60\u5316\u7ba1\u7406<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u6548\u7684\u7ebf\u7a0b\u6a21\u578b<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6613\u7528\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7b80\u5316\u7684API<\/p>\n<\/li>\n<li>\n<p>\u4e30\u5bcc\u7684\u7f16\u89e3\u7801\u5668<\/p>\n<\/li>\n<li>\n<p>\u5b8c\u5584\u7684\u6587\u6863\u548c\u793a\u4f8b<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7a33\u5b9a\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7ecf\u8fc7\u5927\u89c4\u6a21\u751f\u4ea7\u9a8c\u8bc1<\/p>\n<\/li>\n<li>\n<p>\u6d3b\u8dc3\u7684\u793e\u533a\u652f\u6301<\/p>\n<\/li>\n<li>\n<p>\u826f\u597d\u7684\u5411\u540e\u517c\u5bb9\u6027<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u6269\u5c55\u6027&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6a21\u5757\u5316\u8bbe\u8ba1<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u81ea\u5b9a\u4e49\u534f\u8bae<\/p>\n<\/li>\n<li>\n<p>\u7075\u6d3b\u7684\u5904\u7406\u5668\u94fe<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<h3>\u7b2c2\u7ae0&#xff1a;Netty\u6838\u5fc3\u67b6\u6784\u89e3\u6790<\/h3>\n<h4>2.1 Reactor\u7ebf\u7a0b\u6a21\u578b<\/h4>\n<p>Netty\u7684\u6838\u5fc3\u57fa\u4e8eReactor\u6a21\u5f0f&#xff0c;\u652f\u6301\u591a\u79cd\u7ebf\u7a0b\u6a21\u578b&#xff1a;<\/p>\n<h5>2.1.1 \u5355Reactor\u5355\u7ebf\u7a0b\u6a21\u578b<\/h5>\n<p>text<\/p>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502            Reactor Thread               \u2502<br \/>\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510 \u2502<br \/>\n\u2502  \u2502  Accept \u2502  \u2502  Read   \u2502  \u2502 Process \u2502 \u2502<br \/>\n\u2502  \u2502 Handler \u2502  \u2502 Handler \u2502  \u2502 Handler \u2502 \u2502<br \/>\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518 \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<ul>\n<li>\n<p>\u4f18\u70b9&#xff1a;\u7b80\u5355&#xff0c;\u65e0\u7ebf\u7a0b\u5207\u6362\u5f00\u9500<\/p>\n<\/li>\n<li>\n<p>\u7f3a\u70b9&#xff1a;\u65e0\u6cd5\u5229\u7528\u591a\u6838CPU&#xff0c;\u4e00\u4e2a\u963b\u585e\u5f71\u54cd\u6240\u6709\u8fde\u63a5<\/p>\n<\/li>\n<\/ul>\n<h5>2.1.2 \u5355Reactor\u591a\u7ebf\u7a0b\u6a21\u578b<\/h5>\n<p>text<\/p>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502            Main Reactor                 \u2502<br \/>\n\u2502               Accept                    \u2502<br \/>\n\u2502                 \u2193                       \u2502<br \/>\n\u2502            Sub Reactor                  \u2502<br \/>\n\u2502         \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2510                \u2502<br \/>\n\u2502      Read\/Write     Read\/Write          \u2502<br \/>\n\u2502         \u2193              \u2193                \u2502<br \/>\n\u2502     Worker Pool     Worker Pool         \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<ul>\n<li>\n<p>\u4f18\u70b9&#xff1a;\u5229\u7528\u591a\u6838&#xff0c;I\/O\u4e0e\u4e1a\u52a1\u5904\u7406\u5206\u79bb<\/p>\n<\/li>\n<li>\n<p>\u7f3a\u70b9&#xff1a;Reactor\u4ecd\u53ef\u80fd\u6210\u4e3a\u74f6\u9888<\/p>\n<\/li>\n<\/ul>\n<h5>2.1.3 \u4e3b\u4eceReactor\u591a\u7ebf\u7a0b\u6a21\u578b&#xff08;Netty\u9ed8\u8ba4&#xff09;<\/h5>\n<p>java<\/p>\n<p>\/\/ Netty\u7684\u7ebf\u7a0b\u6a21\u578b\u914d\u7f6e<br \/>\nEventLoopGroup bossGroup &#061; new NioEventLoopGroup(1);    \/\/ \u4e3bReactor<br \/>\nEventLoopGroup workerGroup &#061; new NioEventLoopGroup();   \/\/ \u4eceReactor<\/p>\n<p>ServerBootstrap b &#061; new ServerBootstrap();<br \/>\nb.group(bossGroup, workerGroup)  \/\/ \u4e3b\u4ece\u6a21\u578b<br \/>\n .channel(NioServerSocketChannel.class);<\/p>\n<h4>2.2 Netty\u6838\u5fc3\u7ec4\u4ef6\u8be6\u89e3<\/h4>\n<h5>2.2.1 Channel &#8211; \u7f51\u7edc\u8fde\u63a5\u7684\u62bd\u8c61<\/h5>\n<p>Channel\u662fNetty\u5bf9\u7f51\u7edc\u8fde\u63a5\u7684\u62bd\u8c61&#xff0c;\u7c7b\u4f3c\u4e8eJava NIO\u7684Channel&#xff0c;\u4f46\u529f\u80fd\u66f4\u5f3a\u5927&#xff1a;<\/p>\n<p>java<\/p>\n<p>public interface Channel extends AttributeMap, ChannelOutboundInvoker,<br \/>\n                                Comparable&lt;Channel&gt; {<br \/>\n    \/\/ \u901a\u9053\u57fa\u672c\u4fe1\u606f<br \/>\n    ChannelId id();<br \/>\n    EventLoop eventLoop();<br \/>\n    Channel parent();<br \/>\n    ChannelConfig config();<\/p>\n<p>    \/\/ \u72b6\u6001\u67e5\u8be2<br \/>\n    boolean isOpen();<br \/>\n    boolean isRegistered();<br \/>\n    boolean isActive();<\/p>\n<p>    \/\/ \u672c\u5730\u548c\u8fdc\u7a0b\u5730\u5740<br \/>\n    SocketAddress localAddress();<br \/>\n    SocketAddress remoteAddress();<\/p>\n<p>    \/\/ \u901a\u9053\u64cd\u4f5c<br \/>\n    ChannelFuture closeFuture();<br \/>\n    Unsafe unsafe();<br \/>\n    ChannelPipeline pipeline();<br \/>\n    ByteBufAllocator alloc();<\/p>\n<p>    \/\/ \u8bfb\u5199\u64cd\u4f5c<br \/>\n    ChannelFuture write(Object msg);<br \/>\n    ChannelFuture writeAndFlush(Object msg);<br \/>\n}<\/p>\n<h5>2.2.2 EventLoop &#8211; \u4e8b\u4ef6\u5faa\u73af<\/h5>\n<p>EventLoop\u662fNetty\u7684\u6838\u5fc3\u8c03\u5ea6\u5355\u5143&#xff0c;\u8d1f\u8d23\u5904\u7406I\/O\u4e8b\u4ef6\u548c\u4efb\u52a1&#xff1a;<\/p>\n<p>java<\/p>\n<p>\/\/ EventLoop\u7684\u7ee7\u627f\u4f53\u7cfb<br \/>\npublic interface EventLoop extends OrderedEventExecutor, EventLoopGroup {<br \/>\n    \/\/ \u7ee7\u627f\u81eaEventLoopGroup<br \/>\n    &#064;Override<br \/>\n    EventLoop next();<\/p>\n<p>    \/\/ \u7ee7\u627f\u81eaEventExecutor<br \/>\n    boolean inEventLoop();<br \/>\n    boolean inEventLoop(Thread thread);<br \/>\n}<\/p>\n<p>\/\/ EventLoopGroup\u7ba1\u7406\u591a\u4e2aEventLoop<br \/>\npublic interface EventLoopGroup extends EventExecutorGroup {<br \/>\n    \/\/ \u6ce8\u518cChannel\u5230EventLoop<br \/>\n    ChannelFuture register(Channel channel);<br \/>\n    ChannelFuture register(ChannelPromise promise);<br \/>\n}<\/p>\n<h5>2.2.3 ChannelFuture &#8211; \u5f02\u6b65\u64cd\u4f5c\u7ed3\u679c<\/h5>\n<p>Netty\u4e2d\u6240\u6709I\/O\u64cd\u4f5c\u90fd\u662f\u5f02\u6b65\u7684&#xff0c;\u901a\u8fc7ChannelFuture\u83b7\u53d6\u64cd\u4f5c\u7ed3\u679c&#xff1a;<\/p>\n<p>java<\/p>\n<p>ChannelFuture future &#061; channel.write(msg);<br \/>\nfuture.addListener(new ChannelFutureListener() {<br \/>\n    &#064;Override<br \/>\n    public void operationComplete(ChannelFuture future) {<br \/>\n        if (future.isSuccess()) {<br \/>\n            System.out.println(&#034;Write successful&#034;);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;Write failed: &#034; &#043; future.cause());<br \/>\n        }<br \/>\n    }<br \/>\n});<\/p>\n<h5>2.2.4 ChannelHandler &#8211; \u4e1a\u52a1\u5904\u7406\u5668<\/h5>\n<p>ChannelHandler\u662fNetty\u7684\u6838\u5fc3\u6269\u5c55\u70b9&#xff0c;\u5904\u7406\u5165\u7ad9\u548c\u51fa\u7ad9\u4e8b\u4ef6&#xff1a;<\/p>\n<p>java<\/p>\n<p>\/\/ ChannelHandler\u7ee7\u627f\u4f53\u7cfb<br \/>\npublic interface ChannelHandler {<br \/>\n    void handlerAdded(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void handlerRemoved(ChannelHandlerContext ctx) throws Exception;<br \/>\n    &#064;Deprecated<br \/>\n    void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception;<br \/>\n}<\/p>\n<p>\/\/ \u5165\u7ad9\u5904\u7406\u5668<br \/>\npublic interface ChannelInboundHandler extends ChannelHandler {<br \/>\n    void channelRegistered(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void channelUnregistered(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void channelActive(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void channelInactive(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;<br \/>\n    void channelReadComplete(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;<br \/>\n    void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception;<br \/>\n}<\/p>\n<p>\/\/ \u51fa\u7ad9\u5904\u7406\u5668<br \/>\npublic interface ChannelOutboundHandler extends ChannelHandler {<br \/>\n    void bind(ChannelHandlerContext ctx, SocketAddress localAddress,<br \/>\n              ChannelPromise promise) throws Exception;<br \/>\n    void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,<br \/>\n                 SocketAddress localAddress, ChannelPromise promise) throws Exception;<br \/>\n    void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;<br \/>\n    void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;<br \/>\n    void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception;<br \/>\n    void read(ChannelHandlerContext ctx) throws Exception;<br \/>\n    void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception;<br \/>\n    void flush(ChannelHandlerContext ctx) throws Exception;<br \/>\n}<\/p>\n<h5>2.2.5 ChannelPipeline &#8211; \u5904\u7406\u5668\u7ba1\u9053<\/h5>\n<p>ChannelPipeline\u662fChannelHandler\u7684\u5bb9\u5668&#xff0c;\u7ef4\u62a4\u7740\u5904\u7406\u5668\u7684\u53cc\u5411\u94fe\u8868&#xff1a;<\/p>\n<p>java<\/p>\n<p>\/\/ Pipeline\u7684\u5185\u90e8\u7ed3\u6784<br \/>\npublic interface ChannelPipeline extends ChannelInboundInvoker,<br \/>\n                                        ChannelOutboundInvoker,<br \/>\n                                        Iterable&lt;Entry&lt;String, ChannelHandler&gt;&gt; {<\/p>\n<p>    \/\/ \u5904\u7406\u5668\u7ba1\u7406<br \/>\n    ChannelPipeline addFirst(String name, ChannelHandler handler);<br \/>\n    ChannelPipeline addLast(String name, ChannelHandler handler);<br \/>\n    ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler);<br \/>\n    ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler);<br \/>\n    ChannelPipeline remove(ChannelHandler handler);<\/p>\n<p>    \/\/ \u5904\u7406\u5668\u67e5\u627e<br \/>\n    ChannelHandler first();<br \/>\n    ChannelHandler last();<br \/>\n    ChannelHandler get(String name);<\/p>\n<p>    \/\/ \u4e0a\u4e0b\u6587\u64cd\u4f5c<br \/>\n    ChannelHandlerContext context(ChannelHandler handler);<br \/>\n    ChannelHandlerContext context(String name);<br \/>\n    ChannelHandlerContext context(Class&lt;? extends ChannelHandler&gt; handlerType);<\/p>\n<p>    \/\/ \u901a\u9053\u5f15\u7528<br \/>\n    Channel channel();<\/p>\n<p>    \/\/ \u4e8b\u4ef6\u4f20\u64ad<br \/>\n    ChannelPipeline fireChannelRegistered();<br \/>\n    ChannelPipeline fireChannelUnregistered();<br \/>\n    ChannelPipeline fireChannelActive();<br \/>\n    \/\/ &#8230; \u5176\u4ed6\u4e8b\u4ef6\u4f20\u64ad\u65b9\u6cd5<br \/>\n}<\/p>\n<h5>2.2.6 ByteBuf &#8211; \u6570\u636e\u5bb9\u5668<\/h5>\n<p>ByteBuf\u662fNetty\u5bf9\u5b57\u8282\u6570\u636e\u7684\u5c01\u88c5&#xff0c;\u6bd4Java NIO\u7684ByteBuffer\u66f4\u5f3a\u5927&#xff1a;<\/p>\n<p>java<\/p>\n<p>\/\/ ByteBuf\u7684\u6838\u5fc3\u7279\u6027<br \/>\npublic abstract class ByteBuf implements ReferenceCounted, Comparable&lt;ByteBuf&gt; {<br \/>\n    \/\/ \u5bb9\u91cf\u7ba1\u7406<br \/>\n    public abstract int capacity();<br \/>\n    public abstract ByteBuf capacity(int newCapacity);<\/p>\n<p>    \/\/ \u8bfb\u5199\u6307\u9488<br \/>\n    public abstract int readerIndex();<br \/>\n    public abstract ByteBuf readerIndex(int readerIndex);<br \/>\n    public abstract int writerIndex();<br \/>\n    public abstract ByteBuf writerIndex(int writerIndex);<\/p>\n<p>    \/\/ \u6570\u636e\u8bfb\u5199<br \/>\n    public abstract byte getByte(int index);<br \/>\n    public abstract ByteBuf setByte(int index, int value);<br \/>\n    public abstract byte readByte();<br \/>\n    public abstract ByteBuf writeByte(int value);<\/p>\n<p>    \/\/ \u5185\u5b58\u7c7b\u578b<br \/>\n    public abstract boolean isDirect();<br \/>\n    public abstract boolean hasArray();<br \/>\n    public abstract byte[] array();<\/p>\n<p>    \/\/ \u5f15\u7528\u8ba1\u6570<br \/>\n    &#064;Override<br \/>\n    public abstract int refCnt();<br \/>\n    &#064;Override<br \/>\n    public abstract ByteBuf retain();<br \/>\n    &#064;Override<br \/>\n    public abstract boolean release();<br \/>\n}<\/p>\n<h3>\u7b2c3\u7ae0&#xff1a;Netty\u5b9e\u6218\u5165\u95e8<\/h3>\n<h4>3.1 \u7b2c\u4e00\u4e2aNetty\u5e94\u7528&#xff1a;Echo\u670d\u52a1\u5668<\/h4>\n<h5>3.1.1 \u9879\u76ee\u642d\u5efa<\/h5>\n<p>xml<\/p>\n<p>&lt;!&#8211; Maven\u4f9d\u8d56 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;io.netty&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;netty-all&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;4.1.86.Final&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<h5>3.1.2 Echo\u670d\u52a1\u5668\u5b9e\u73b0<\/h5>\n<p>java<\/p>\n<p>\/\/ Echo\u670d\u52a1\u5668\u5904\u7406\u5668<br \/>\n&#064;Sharable<br \/>\npublic class EchoServerHandler extends ChannelInboundHandlerAdapter {<\/p>\n<p>    &#064;Override<br \/>\n    public void channelRead(ChannelHandlerContext ctx, Object msg) {<br \/>\n        ByteBuf in &#061; (ByteBuf) msg;<br \/>\n        try {<br \/>\n            \/\/ \u6253\u5370\u63a5\u6536\u5230\u7684\u6d88\u606f<br \/>\n            System.out.println(&#034;Server received: &#034; &#043;<br \/>\n                in.toString(CharsetUtil.UTF_8));<\/p>\n<p>            \/\/ \u5c06\u6d88\u606f\u5199\u56de\u5ba2\u6237\u7aef<br \/>\n            ctx.write(msg); \/\/ \u4e0d\u7acb\u5373\u5237\u65b0<br \/>\n        } finally {<br \/>\n            \/\/ \u5982\u679cmsg\u88ab\u6d88\u8d39\u4e86&#xff0c;\u9700\u8981\u91ca\u653e<br \/>\n            \/\/ ReferenceCountUtil.release(msg);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void channelReadComplete(ChannelHandlerContext ctx) {<br \/>\n        \/\/ \u6279\u91cf\u5199\u5165\u540e\u5237\u65b0<br \/>\n        ctx.flush();<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {<br \/>\n        cause.printStackTrace();<br \/>\n        ctx.close();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ Echo\u670d\u52a1\u5668<br \/>\npublic class EchoServer {<br \/>\n    private final int port;<\/p>\n<p>    public EchoServer(int port) {<br \/>\n        this.port &#061; port;<br \/>\n    }<\/p>\n<p>    public void start() throws Exception {<br \/>\n        \/\/ 1. \u521b\u5efaEventLoopGroup<br \/>\n        EventLoopGroup bossGroup &#061; new NioEventLoopGroup(1);<br \/>\n        EventLoopGroup workerGroup &#061; new NioEventLoopGroup();<\/p>\n<p>        try {<br \/>\n            \/\/ 2. \u521b\u5efaServerBootstrap<br \/>\n            ServerBootstrap b &#061; new ServerBootstrap();<br \/>\n            b.group(bossGroup, workerGroup)<br \/>\n             .channel(NioServerSocketChannel.class)<br \/>\n             .localAddress(new InetSocketAddress(port))<br \/>\n             .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {<br \/>\n                 &#064;Override<br \/>\n                 protected void initChannel(SocketChannel ch) throws Exception {<br \/>\n                     ChannelPipeline p &#061; ch.pipeline();<br \/>\n                     \/\/ \u6dfb\u52a0\u5904\u7406\u5668<br \/>\n                     p.addLast(new EchoServerHandler());<br \/>\n                 }<br \/>\n             });<\/p>\n<p>            \/\/ 3. \u7ed1\u5b9a\u7aef\u53e3<br \/>\n            ChannelFuture f &#061; b.bind().sync();<br \/>\n            System.out.println(&#034;EchoServer started on port &#034; &#043; port);<\/p>\n<p>            \/\/ 4. \u7b49\u5f85\u670d\u52a1\u5668\u5173\u95ed<br \/>\n            f.channel().closeFuture().sync();<br \/>\n        } finally {<br \/>\n            \/\/ 5. \u4f18\u96c5\u5173\u95ed<br \/>\n            bossGroup.shutdownGracefully();<br \/>\n            workerGroup.shutdownGracefully();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws Exception {<br \/>\n        int port &#061; 8080;<br \/>\n        new EchoServer(port).start();<br \/>\n    }<br \/>\n}<\/p>\n<h5>3.1.3 Echo\u5ba2\u6237\u7aef\u5b9e\u73b0<\/h5>\n<p>java<\/p>\n<p>\/\/ Echo\u5ba2\u6237\u7aef\u5904\u7406\u5668<br \/>\n&#064;Sharable<br \/>\npublic class EchoClientHandler extends SimpleChannelInboundHandler&lt;ByteBuf&gt; {<\/p>\n<p>    &#064;Override<br \/>\n    public void channelActive(ChannelHandlerContext ctx) {<br \/>\n        \/\/ \u8fde\u63a5\u5efa\u7acb\u540e\u53d1\u9001\u6d88\u606f<br \/>\n        String message &#061; &#034;Hello, Netty!&#034;;<br \/>\n        ByteBuf buffer &#061; ctx.alloc().buffer(message.length());<br \/>\n        buffer.writeBytes(message.getBytes());<br \/>\n        ctx.writeAndFlush(buffer);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {<br \/>\n        \/\/ \u63a5\u6536\u670d\u52a1\u5668\u54cd\u5e94<br \/>\n        System.out.println(&#034;Client received: &#034; &#043;<br \/>\n            msg.toString(CharsetUtil.UTF_8));<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {<br \/>\n        cause.printStackTrace();<br \/>\n        ctx.close();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ Echo\u5ba2\u6237\u7aef<br \/>\npublic class EchoClient {<br \/>\n    private final String host;<br \/>\n    private final int port;<\/p>\n<p>    public EchoClient(String host, int port) {<br \/>\n        this.host &#061; host;<br \/>\n        this.port &#061; port;<br \/>\n    }<\/p>\n<p>    public void start() throws Exception {<br \/>\n        \/\/ 1. \u521b\u5efaEventLoopGroup<br \/>\n        EventLoopGroup group &#061; new NioEventLoopGroup();<\/p>\n<p>        try {<br \/>\n            \/\/ 2. \u521b\u5efaBootstrap<br \/>\n            Bootstrap b &#061; new Bootstrap();<br \/>\n            b.group(group)<br \/>\n             .channel(NioSocketChannel.class)<br \/>\n             .remoteAddress(new InetSocketAddress(host, port))<br \/>\n             .handler(new ChannelInitializer&lt;SocketChannel&gt;() {<br \/>\n                 &#064;Override<br \/>\n                 protected void initChannel(SocketChannel ch) throws Exception {<br \/>\n                     ChannelPipeline p &#061; ch.pipeline();<br \/>\n                     p.addLast(new EchoClientHandler());<br \/>\n                 }<br \/>\n             });<\/p>\n<p>            \/\/ 3. \u8fde\u63a5\u670d\u52a1\u5668<br \/>\n            ChannelFuture f &#061; b.connect().sync();<\/p>\n<p>            \/\/ 4. \u7b49\u5f85\u8fde\u63a5\u5173\u95ed<br \/>\n            f.channel().closeFuture().sync();<br \/>\n        } finally {<br \/>\n            \/\/ 5. \u4f18\u96c5\u5173\u95ed<br \/>\n            group.shutdownGracefully();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) throws Exception {<br \/>\n        String host &#061; &#034;localhost&#034;;<br \/>\n        int port &#061; 8080;<br \/>\n        new EchoClient(host, port).start();<br \/>\n    }<br \/>\n}<\/p>\n<h4>3.2 \u7f16\u89e3\u7801\u5668\u5b9e\u6218<\/h4>\n<h5>3.2.1 \u81ea\u5b9a\u4e49\u7f16\u89e3\u7801\u5668<\/h5>\n<p>java<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u6d88\u606f\u7ed3\u6784<br \/>\npublic class CustomMessage {<br \/>\n    private int length;<br \/>\n    private byte type;<br \/>\n    private byte[] body;<\/p>\n<p>    \/\/ \u6784\u9020\u65b9\u6cd5\u3001getter\u3001setter\u7701\u7565<br \/>\n}<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u7f16\u7801\u5668<br \/>\npublic class CustomEncoder extends MessageToByteEncoder&lt;CustomMessage&gt; {<\/p>\n<p>    &#064;Override<br \/>\n    protected void encode(ChannelHandlerContext ctx,<br \/>\n                         CustomMessage msg, ByteBuf out) throws Exception {<br \/>\n        \/\/ \u5199\u5165\u6d88\u606f\u5934<br \/>\n        out.writeInt(msg.getLength());    \/\/ 4\u5b57\u8282<br \/>\n        out.writeByte(msg.getType());     \/\/ 1\u5b57\u8282<\/p>\n<p>        \/\/ \u5199\u5165\u6d88\u606f\u4f53<br \/>\n        if (msg.getBody() !&#061; null) {<br \/>\n            out.writeBytes(msg.getBody());<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u89e3\u7801\u5668<br \/>\npublic class CustomDecoder extends ByteToMessageDecoder {<\/p>\n<p>    &#064;Override<br \/>\n    protected void decode(ChannelHandlerContext ctx,<br \/>\n                         ByteBuf in, List&lt;Object&gt; out) throws Exception {<br \/>\n        \/\/ \u68c0\u67e5\u662f\u5426\u6709\u8db3\u591f\u7684\u6570\u636e<br \/>\n        if (in.readableBytes() &lt; 5) {<br \/>\n            return; \/\/ \u7b49\u5f85\u66f4\u591a\u6570\u636e<br \/>\n        }<\/p>\n<p>        \/\/ \u6807\u8bb0\u8bfb\u53d6\u4f4d\u7f6e<br \/>\n        in.markReaderIndex();<\/p>\n<p>        \/\/ \u8bfb\u53d6\u6d88\u606f\u5934<br \/>\n        int length &#061; in.readInt();<br \/>\n        byte type &#061; in.readByte();<\/p>\n<p>        \/\/ \u68c0\u67e5\u6d88\u606f\u4f53\u662f\u5426\u5b8c\u6574<br \/>\n        if (in.readableBytes() &lt; length) {<br \/>\n            in.resetReaderIndex(); \/\/ \u91cd\u7f6e\u8bfb\u53d6\u4f4d\u7f6e<br \/>\n            return; \/\/ \u7b49\u5f85\u66f4\u591a\u6570\u636e<br \/>\n        }<\/p>\n<p>        \/\/ \u8bfb\u53d6\u6d88\u606f\u4f53<br \/>\n        byte[] body &#061; new byte[length];<br \/>\n        in.readBytes(body);<\/p>\n<p>        \/\/ \u6784\u5efa\u6d88\u606f\u5bf9\u8c61<br \/>\n        CustomMessage message &#061; new CustomMessage();<br \/>\n        message.setLength(length);<br \/>\n        message.setType(type);<br \/>\n        message.setBody(body);<\/p>\n<p>        out.add(message);<br \/>\n    }<br \/>\n}<\/p>\n<h5>3.2.2 \u4f7f\u7528Netty\u5185\u7f6e\u7f16\u89e3\u7801\u5668<\/h5>\n<p>java<\/p>\n<p>\/\/ \u4f7f\u7528String\u7f16\u89e3\u7801\u5668<br \/>\npipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));<br \/>\npipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));<\/p>\n<p>\/\/ \u4f7f\u7528LengthFieldBasedFrameDecoder\u5904\u7406\u7c98\u5305\/\u62c6\u5305<br \/>\npipeline.addLast(new LengthFieldBasedFrameDecoder(<br \/>\n    1024 * 1024,    \/\/ maxFrameLength<br \/>\n    0,              \/\/ lengthFieldOffset<br \/>\n    4,              \/\/ lengthFieldLength<br \/>\n    0,              \/\/ lengthAdjustment<br \/>\n    4               \/\/ initialBytesToStrip<br \/>\n));<\/p>\n<h3>\u7b2c4\u7ae0&#xff1a;Netty\u9ad8\u7ea7\u7279\u6027<\/h3>\n<h4>4.1 \u96f6\u62f7\u8d1d\u6280\u672f<\/h4>\n<p>Netty\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u96f6\u62f7\u8d1d&#xff0c;\u51cf\u5c11\u5185\u5b58\u590d\u5236&#xff1a;<\/p>\n<h5>4.1.1 CompositeByteBuf<\/h5>\n<p>java<\/p>\n<p>\/\/ \u7ec4\u5408\u591a\u4e2aByteBuf&#xff0c;\u4e0d\u590d\u5236\u6570\u636e<br \/>\nByteBuf header &#061; &#8230;;<br \/>\nByteBuf body &#061; &#8230;;<br \/>\nCompositeByteBuf message &#061; Unpooled.compositeBuffer();<br \/>\nmessage.addComponents(true, header, body);<\/p>\n<h5>4.1.2 FileRegion\u6587\u4ef6\u4f20\u8f93<\/h5>\n<p>java<\/p>\n<p>\/\/ \u6587\u4ef6\u4f20\u8f93&#xff0c;\u5229\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684\u96f6\u62f7\u8d1d<br \/>\nFile file &#061; new File(&#034;largefile.txt&#034;);<br \/>\nFileInputStream in &#061; new FileInputStream(file);<br \/>\nFileRegion region &#061; new DefaultFileRegion(<br \/>\n    in.getChannel(), 0, file.length()<br \/>\n);<br \/>\nchannel.writeAndFlush(region);<\/p>\n<h4>4.2 \u5185\u5b58\u7ba1\u7406<\/h4>\n<h5>4.2.1 ByteBuf\u5185\u5b58\u6c60<\/h5>\n<p>java<\/p>\n<p>\/\/ \u4f7f\u7528\u5185\u5b58\u6c60<br \/>\nByteBufAllocator alloc &#061; PooledByteBufAllocator.DEFAULT;<br \/>\nByteBuf buffer &#061; alloc.buffer(1024);<\/p>\n<p>\/\/ \u4f7f\u7528\u975e\u6c60\u5316\u5185\u5b58&#xff08;\u6d4b\u8bd5\u7528&#xff09;<br \/>\nByteBufAllocator unpooled &#061; UnpooledByteBufAllocator.DEFAULT;<br \/>\nByteBuf buffer2 &#061; unpooled.buffer(1024);<\/p>\n<h5>4.2.2 \u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b<\/h5>\n<p>java<\/p>\n<p>\/\/ \u542f\u7528\u5185\u5b58\u6cc4\u6f0f\u68c0\u6d4b<br \/>\n-Dio.netty.leakDetection.level&#061;PARANOID<\/p>\n<p>\/\/ \u624b\u52a8\u68c0\u6d4b<br \/>\nResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID);<\/p>\n<h4>4.3 \u9ad8\u6027\u80fd\u4f18\u5316<\/h4>\n<h5>4.3.1 EventLoop\u914d\u7f6e\u4f18\u5316<\/h5>\n<p>java<\/p>\n<p>\/\/ \u4f18\u5316EventLoop\u7ebf\u7a0b\u6570<br \/>\nint cores &#061; Runtime.getRuntime().availableProcessors();<br \/>\nEventLoopGroup bossGroup &#061; new NioEventLoopGroup(1);<br \/>\nEventLoopGroup workerGroup &#061; new NioEventLoopGroup(cores * 2);<\/p>\n<p>\/\/ \u8bbe\u7f6eReactor\u7ebf\u7a0b\u540d\u79f0<br \/>\nThreadFactory factory &#061; new ThreadFactoryBuilder()<br \/>\n    .setNameFormat(&#034;netty-worker-%d&#034;)<br \/>\n    .build();<br \/>\nEventLoopGroup customGroup &#061; new NioEventLoopGroup(0, factory);<\/p>\n<h5>4.3.2 \u53c2\u6570\u8c03\u4f18<\/h5>\n<p>java<\/p>\n<p>ServerBootstrap b &#061; new ServerBootstrap();<br \/>\nb.group(bossGroup, workerGroup)<br \/>\n .channel(NioServerSocketChannel.class)<br \/>\n .option(ChannelOption.SO_BACKLOG, 1024)        \/\/ \u8fde\u63a5\u961f\u5217\u5927\u5c0f<br \/>\n .option(ChannelOption.SO_REUSEADDR, true)      \/\/ \u5730\u5740\u91cd\u7528<br \/>\n .childOption(ChannelOption.TCP_NODELAY, true)  \/\/ \u7981\u7528Nagle\u7b97\u6cd5<br \/>\n .childOption(ChannelOption.SO_KEEPALIVE, true) \/\/ \u4fdd\u6301\u8fde\u63a5<br \/>\n .childOption(ChannelOption.ALLOCATOR,<br \/>\n              PooledByteBufAllocator.DEFAULT);  \/\/ \u5185\u5b58\u6c60<\/p>\n<h3>\u7b2c5\u7ae0&#xff1a;Netty\u5b9e\u6218\u9879\u76ee<\/h3>\n<h4>5.1 \u5b9e\u73b0HTTP\u670d\u52a1\u5668<\/h4>\n<p>java<\/p>\n<p>public class HttpServer {<\/p>\n<p>    public void start(int port) throws Exception {<br \/>\n        EventLoopGroup bossGroup &#061; new NioEventLoopGroup();<br \/>\n        EventLoopGroup workerGroup &#061; new NioEventLoopGroup();<\/p>\n<p>        try {<br \/>\n            ServerBootstrap b &#061; new ServerBootstrap();<br \/>\n            b.group(bossGroup, workerGroup)<br \/>\n             .channel(NioServerSocketChannel.class)<br \/>\n             .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {<br \/>\n                 &#064;Override<br \/>\n                 protected void initChannel(SocketChannel ch) {<br \/>\n                     ChannelPipeline p &#061; ch.pipeline();<\/p>\n<p>                     \/\/ HTTP\u7f16\u89e3\u7801\u5668<br \/>\n                     p.addLast(new HttpServerCodec());<br \/>\n                     \/\/ \u805a\u5408HTTP\u6d88\u606f<br \/>\n                     p.addLast(new HttpObjectAggregator(65536));<br \/>\n                     \/\/ \u538b\u7f29<br \/>\n                     p.addLast(new HttpContentCompressor());<br \/>\n                     \/\/ \u4e1a\u52a1\u5904\u7406\u5668<br \/>\n                     p.addLast(new HttpRequestHandler());<br \/>\n                 }<br \/>\n             });<\/p>\n<p>            ChannelFuture f &#061; b.bind(port).sync();<br \/>\n            f.channel().closeFuture().sync();<br \/>\n        } finally {<br \/>\n            bossGroup.shutdownGracefully();<br \/>\n            workerGroup.shutdownGracefully();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    private static class HttpRequestHandler<br \/>\n        extends SimpleChannelInboundHandler&lt;FullHttpRequest&gt; {<\/p>\n<p>        &#064;Override<br \/>\n        protected void channelRead0(ChannelHandlerContext ctx,<br \/>\n                                   FullHttpRequest req) throws Exception {<br \/>\n            \/\/ \u5904\u7406HTTP\u8bf7\u6c42<br \/>\n            FullHttpResponse response &#061; new DefaultFullHttpResponse(<br \/>\n                HttpVersion.HTTP_1_1,<br \/>\n                HttpResponseStatus.OK,<br \/>\n                Unpooled.wrappedBuffer(&#034;Hello World&#034;.getBytes())<br \/>\n            );<\/p>\n<p>            response.headers().set(HttpHeaderNames.CONTENT_TYPE,<br \/>\n                                 &#034;text\/plain&#034;);<br \/>\n            response.headers().set(HttpHeaderNames.CONTENT_LENGTH,<br \/>\n                                 response.content().readableBytes());<\/p>\n<p>            ctx.writeAndFlush(response);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h4>5.2 \u5b9e\u73b0WebSocket\u670d\u52a1\u5668<\/h4>\n<p>java<\/p>\n<p>public class WebSocketServer {<\/p>\n<p>    public void start(int port) throws Exception {<br \/>\n        EventLoopGroup bossGroup &#061; new NioEventLoopGroup();<br \/>\n        EventLoopGroup workerGroup &#061; new NioEventLoopGroup();<\/p>\n<p>        try {<br \/>\n            ServerBootstrap b &#061; new ServerBootstrap();<br \/>\n            b.group(bossGroup, workerGroup)<br \/>\n             .channel(NioServerSocketChannel.class)<br \/>\n             .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {<br \/>\n                 &#064;Override<br \/>\n                 protected void initChannel(SocketChannel ch) {<br \/>\n                     ChannelPipeline p &#061; ch.pipeline();<\/p>\n<p>                     \/\/ HTTP\u7f16\u89e3\u7801\u5668<br \/>\n                     p.addLast(new HttpServerCodec());<br \/>\n                     \/\/ \u805a\u5408\u5668<br \/>\n                     p.addLast(new HttpObjectAggregator(65536));<br \/>\n                     \/\/ WebSocket\u5904\u7406\u5668<br \/>\n                     p.addLast(new WebSocketServerProtocolHandler(<br \/>\n                         &#034;\/ws&#034;, null, true));<br \/>\n                     \/\/ \u4e1a\u52a1\u5904\u7406\u5668<br \/>\n                     p.addLast(new WebSocketFrameHandler());<br \/>\n                 }<br \/>\n             });<\/p>\n<p>            ChannelFuture f &#061; b.bind(port).sync();<br \/>\n            f.channel().closeFuture().sync();<br \/>\n        } finally {<br \/>\n            bossGroup.shutdownGracefully();<br \/>\n            workerGroup.shutdownGracefully();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    private static class WebSocketFrameHandler<br \/>\n        extends SimpleChannelInboundHandler&lt;WebSocketFrame&gt; {<\/p>\n<p>        &#064;Override<br \/>\n        protected void channelRead0(ChannelHandlerContext ctx,<br \/>\n                                   WebSocketFrame frame) throws Exception {<br \/>\n            if (frame instanceof TextWebSocketFrame) {<br \/>\n                \/\/ \u5904\u7406\u6587\u672c\u5e27<br \/>\n                String request &#061; ((TextWebSocketFrame) frame).text();<br \/>\n                ctx.writeAndFlush(new TextWebSocketFrame(<br \/>\n                    &#034;Server: &#034; &#043; request));<br \/>\n            } else if (frame instanceof PingWebSocketFrame) {<br \/>\n                \/\/ \u54cd\u5e94Ping\u5e27<br \/>\n                ctx.writeAndFlush(new PongWebSocketFrame(<br \/>\n                    frame.content().retain()));<br \/>\n            } else if (frame instanceof CloseWebSocketFrame) {<br \/>\n                \/\/ \u5173\u95ed\u8fde\u63a5<br \/>\n                ctx.close();<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u7b2c6\u7ae0&#xff1a;Netty\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>6.1 \u6027\u80fd\u76d1\u63a7<\/h4>\n<h5>6.1.1 \u76d1\u63a7\u6307\u6807<\/h5>\n<p>java<\/p>\n<p>public class NettyMetrics {<\/p>\n<p>    \/\/ \u8fde\u63a5\u6570\u76d1\u63a7<br \/>\n    private final AtomicInteger connectionCount &#061; new AtomicInteger(0);<\/p>\n<p>    \/\/ QPS\u76d1\u63a7<br \/>\n    private final LongAdder requestCount &#061; new LongAdder();<br \/>\n    private volatile long lastResetTime &#061; System.currentTimeMillis();<\/p>\n<p>    \/\/ \u5185\u5b58\u4f7f\u7528\u76d1\u63a7<br \/>\n    public void monitorMemory() {<br \/>\n        PooledByteBufAllocator alloc &#061;<br \/>\n            (PooledByteBufAllocator) PooledByteBufAllocator.DEFAULT;<\/p>\n<p>        \/\/ \u83b7\u53d6\u5185\u5b58\u6c60\u7edf\u8ba1\u4fe1\u606f<br \/>\n        PooledByteBufAllocatorMetric metric &#061; alloc.metric();<br \/>\n        System.out.println(&#034;Used heap memory: &#034; &#043;<br \/>\n            metric.usedHeapMemory());<br \/>\n        System.out.println(&#034;Used direct memory: &#034; &#043;<br \/>\n            metric.usedDirectMemory());<br \/>\n    }<br \/>\n}<\/p>\n<h4>6.2 \u6545\u969c\u6392\u67e5<\/h4>\n<h5>6.2.1 \u5e38\u89c1\u95ee\u9898\u53ca\u89e3\u51b3<\/h5>\n<li>\n<p>\u5185\u5b58\u6cc4\u6f0f<\/p>\n<p>java<\/p>\n<p>\/\/ \u6b63\u786e\u91ca\u653eByteBuf<br \/>\nByteBuf buffer &#061; &#8230;;<br \/>\ntry {<br \/>\n    \/\/ \u4f7f\u7528buffer<br \/>\n} finally {<br \/>\n    ReferenceCountUtil.release(buffer);<br \/>\n}\n<\/li>\n<li>\n<p>CPU\u5360\u7528\u8fc7\u9ad8<\/p>\n<p>java<\/p>\n<p>\/\/ \u907f\u514d\u5728EventLoop\u4e2d\u6267\u884c\u963b\u585e\u64cd\u4f5c<br \/>\nchannel.eventLoop().execute(() -&gt; {<br \/>\n    \/\/ \u975e\u963b\u585e\u64cd\u4f5c<br \/>\n});<\/p>\n<p>\/\/ \u4f7f\u7528\u4e1a\u52a1\u7ebf\u7a0b\u6c60\u5904\u7406\u963b\u585e\u4efb\u52a1<br \/>\nchannel.eventLoop().submit(() -&gt; {<br \/>\n    \/\/ \u963b\u585e\u64cd\u4f5c<br \/>\n});\n<\/li>\n<li>\n<p>\u8fde\u63a5\u6570\u8fc7\u591a<\/p>\n<p>java<\/p>\n<p>\/\/ \u9650\u5236\u8fde\u63a5\u6570<br \/>\nb.option(ChannelOption.SO_BACKLOG, 1000)<br \/>\n .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000);\n<\/li>\n<h3>\u7b2c7\u7ae0&#xff1a;Netty\u6e90\u7801\u5206\u6790<\/h3>\n<h4>7.1 \u542f\u52a8\u8fc7\u7a0b\u5206\u6790<\/h4>\n<p>java<\/p>\n<p>\/\/ AbstractBootstrap.bind() \u65b9\u6cd5\u8c03\u7528\u94fe<br \/>\nbind() \u2192 doBind() \u2192 initAndRegister() \u2192 channelFactory.newChannel()<br \/>\n    \u2192 init(channel) \u2192 register(channel)<\/p>\n<h4>7.2 EventLoop\u8fd0\u884c\u673a\u5236<\/h4>\n<p>java<\/p>\n<p>\/\/ SingleThreadEventExecutor.run() \u6838\u5fc3\u903b\u8f91<br \/>\nprotected void run() {<br \/>\n    for (;;) {<br \/>\n        try {<br \/>\n            \/\/ 1. \u68c0\u67e5\u4efb\u52a1<br \/>\n            if (hasTasks()) {<br \/>\n                runAllTasks();<br \/>\n            }<\/p>\n<p>            \/\/ 2. \u5904\u7406I\/O\u4e8b\u4ef6<br \/>\n            selector.select();<br \/>\n            processSelectedKeys();<\/p>\n<p>            \/\/ 3. \u518d\u6b21\u68c0\u67e5\u4efb\u52a1<br \/>\n            if (hasTasks()) {<br \/>\n                runAllTasks();<br \/>\n            }<br \/>\n        } catch (Throwable t) {<br \/>\n            handleLoopException(t);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>Netty\u4f5c\u4e3a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6&#xff0c;\u5176\u6838\u5fc3\u4ef7\u503c\u4f53\u73b0\u5728&#xff1a;<\/p>\n<li>\n<p>\u9ad8\u6027\u80fd&#xff1a;\u57fa\u4e8eReactor\u6a21\u5f0f\u7684\u5f02\u6b65\u975e\u963b\u585e\u8bbe\u8ba1<\/p>\n<\/li>\n<li>\n<p>\u6613\u7528\u6027&#xff1a;\u4e30\u5bcc\u7684API\u548c\u7f16\u89e3\u7801\u5668\u652f\u6301<\/p>\n<\/li>\n<li>\n<p>\u7a33\u5b9a\u6027&#xff1a;\u7ecf\u8fc7\u5927\u89c4\u6a21\u751f\u4ea7\u9a8c\u8bc1<\/p>\n<\/li>\n<li>\n<p>\u6269\u5c55\u6027&#xff1a;\u7075\u6d3b\u7684\u5904\u7406\u5668\u94fe\u8bbe\u8ba1<\/p>\n<\/li>\n<p>\u901a\u8fc7\u672c\u658710\u4e07\u5b57\u7684\u8be6\u7ec6\u89e3\u6790&#xff0c;\u8bfb\u8005\u5e94\u8be5\u80fd\u591f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7406\u89e3Netty\u7684\u6838\u5fc3\u67b6\u6784\u548c\u8bbe\u8ba1\u601d\u60f3<\/p>\n<\/li>\n<li>\n<p>\u638c\u63e1Netty\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/p>\n<\/li>\n<li>\n<p>\u80fd\u591f\u8fdb\u884c\u6027\u80fd\u8c03\u4f18\u548c\u6545\u969c\u6392\u67e5<\/p>\n<\/li>\n<li>\n<p>\u80fd\u591f\u57fa\u4e8eNetty\u5f00\u53d1\u5b9e\u9645\u9879\u76ee<\/p>\n<\/li>\n<\/ul>\n<p>Netty\u7684\u5b66\u4e60\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b&#xff0c;\u5efa\u8bae\u8bfb\u8005&#xff1a;<\/p>\n<li>\n<p>\u4ece\u7b80\u5355\u793a\u4f8b\u5f00\u59cb&#xff0c;\u9010\u6b65\u6df1\u5165<\/p>\n<\/li>\n<li>\n<p>\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u548c\u6e90\u7801<\/p>\n<\/li>\n<li>\n<p>\u53c2\u4e0e\u5f00\u6e90\u793e\u533a\u8ba8\u8bba<\/p>\n<\/li>\n<li>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u548c\u4f18\u5316<\/p>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u7b2c1\u7ae0&#xff1a;Netty\u6982\u8ff0\u4e0e\u6838\u5fc3\u4ef7\u503c1.1 Netty\u662f\u4ec0\u4e48&#xff1f;Netty\u662f\u4e00\u4e2a\u5f02\u6b65\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6&#xff0c;\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u53ef\u7ef4\u62a4\u7684\u9ad8\u6027\u80fd\u534f\u8bae\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u3002\u5b83\u672c\u8d28\u4e0a\u662fJava NIO\u7684\u5c01\u88c5\u4e0e\u589e\u5f3a&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u5957\u7b80\u6d01\u800c\u5f3a\u5927\u7684API&#xff0c;\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91&#xff0c;\u800c\u4e0d\u5fc5\u9677\u5165\u590d\u6742\u7684\u7f51\u7edc\u7f16\u7a0b\u7ec6\u8282\u30021.2 Netty\u7684\u8bde\u751f\u80cc\u666f\u4e0e\u6f14\u8fdbNetty\u7531\u97e9\u56fd\u5de5\u7a0b\u5e08Trustin Lee\u4e8e2004\u5e74\u521b\u5efa&#xff0c;\u6700\u521d<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[426],"topic":[],"class_list":["post-59761","post","type-post","status-publish","format-standard","hentry","category-server","tag-jvm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/59761.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u7b2c1\u7ae0&#xff1a;Netty\u6982\u8ff0\u4e0e\u6838\u5fc3\u4ef7\u503c1.1 Netty\u662f\u4ec0\u4e48&#xff1f;Netty\u662f\u4e00\u4e2a\u5f02\u6b65\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6&#xff0c;\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u53ef\u7ef4\u62a4\u7684\u9ad8\u6027\u80fd\u534f\u8bae\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u3002\u5b83\u672c\u8d28\u4e0a\u662fJava NIO\u7684\u5c01\u88c5\u4e0e\u589e\u5f3a&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u5957\u7b80\u6d01\u800c\u5f3a\u5927\u7684API&#xff0c;\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91&#xff0c;\u800c\u4e0d\u5fc5\u9677\u5165\u590d\u6742\u7684\u7f51\u7edc\u7f16\u7a0b\u7ec6\u8282\u30021.2 Netty\u7684\u8bde\u751f\u80cc\u666f\u4e0e\u6f14\u8fdbNetty\u7531\u97e9\u56fd\u5de5\u7a0b\u5e08Trustin Lee\u4e8e2004\u5e74\u521b\u5efa&#xff0c;\u6700\u521d\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/59761.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-14T04:50:23+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/59761.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/59761.html\",\"name\":\"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-14T04:50:23+00:00\",\"dateModified\":\"2026-01-14T04:50:23+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/59761.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/59761.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/59761.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/59761.html","og_locale":"zh_CN","og_type":"article","og_title":"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u7b2c1\u7ae0&#xff1a;Netty\u6982\u8ff0\u4e0e\u6838\u5fc3\u4ef7\u503c1.1 Netty\u662f\u4ec0\u4e48&#xff1f;Netty\u662f\u4e00\u4e2a\u5f02\u6b65\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u6846\u67b6&#xff0c;\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u53ef\u7ef4\u62a4\u7684\u9ad8\u6027\u80fd\u534f\u8bae\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u3002\u5b83\u672c\u8d28\u4e0a\u662fJava NIO\u7684\u5c01\u88c5\u4e0e\u589e\u5f3a&#xff0c;\u63d0\u4f9b\u4e86\u4e00\u5957\u7b80\u6d01\u800c\u5f3a\u5927\u7684API&#xff0c;\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u4e13\u6ce8\u4e8e\u4e1a\u52a1\u903b\u8f91&#xff0c;\u800c\u4e0d\u5fc5\u9677\u5165\u590d\u6742\u7684\u7f51\u7edc\u7f16\u7a0b\u7ec6\u8282\u30021.2 Netty\u7684\u8bde\u751f\u80cc\u666f\u4e0e\u6f14\u8fdbNetty\u7531\u97e9\u56fd\u5de5\u7a0b\u5e08Trustin Lee\u4e8e2004\u5e74\u521b\u5efa&#xff0c;\u6700\u521d","og_url":"https:\/\/www.wsisp.com\/helps\/59761.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-14T04:50:23+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/59761.html","url":"https:\/\/www.wsisp.com\/helps\/59761.html","name":"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-14T04:50:23+00:00","dateModified":"2026-01-14T04:50:23+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/59761.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/59761.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/59761.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Netty\u5165\u95e8\u8be6\u89e3\uff1a\u9ad8\u6027\u80fd\u7f51\u7edc\u7f16\u7a0b\u6846\u67b6\u6df1\u5ea6\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/59761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=59761"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/59761\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=59761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=59761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=59761"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=59761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}