<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 一、概述 Netty 是一個利用 Java 基于NIO(Nonblocking I/O,非阻塞IO,相對的,阻塞IO即BIO)的高級網絡的能力,隱藏其背后的復雜性而提供一個易于使用的 API 的客戶端/服務器框架; ![](https://img.kancloud.cn/85/52/8552db7ceabc450d9e0eb8db689155d6_592x348.png) 比起直接用NIO編程,用Netty要簡化很多; 1. **并發高** 2. **傳輸快** 3. **封裝好** ## 二、核心概念 ### **Channel** 數據傳輸流,與channel相關的概念有以下四個,上一張圖讓你了解netty里面的Channel; ![](https://img.kancloud.cn/52/50/525049c2d106a975c0e9bdfc7a96780b_751x319.png) * **Channel**,表示一個連接,可以理解為每一個請求,就是一個Channel; * **ChannelHandler**,核心處理業務就在這里,用于處理業務請求,分為ChannelInboundHandler輸入數據處理器和ChannelOutboundHandler輸出業務處理器; * **ChannelHandlerContext**,用于傳輸業務數據; * **ChannelPipeline**,用于保存處理過程需要用到的ChannelHandler和ChannelHandlerContext; >[danger] 交互流程; > 1. 事件傳遞給 ChannelPipeline 的第一個 ChannelHandler > 2. ChannelHandler 通過關聯的 ChannelHandlerContext 傳遞事件給 ChannelPipeline 中的 下一個; > 3. ChannelHandler 通過關聯的 ChannelHandlerContext 傳遞事件給 ChannelPipeline 中的 下一個,一直到最后一個處理完; ### **ByteBuf** ByteBuf是一個存儲字節的容器,最大特點就是**使用方便**,它既有自己的讀索引和寫索引,方便你對整段字節緩存進行讀寫,也支持get/set,方便你對其中每一個字節進行讀寫; ![](https://img.kancloud.cn/d2/20/d2204925603d5e4b8f113289d70511ac_558x181.png) ### **Codec** 包括Decoder和Encoder; Netty中的編碼/解碼器,通過他你能完成字節與pojo、pojo與pojo的相互轉換,從而達到自定義協議的目的,在Netty里面最有名的就是HttpRequestDecoder和HttpResponseEncoder了; 它們,實際上就是對應這**ChannelInboundHandler和ChannelOutboundHandler**,別用于在數據流進來的時候將字節碼轉換為消息對象和數據流出去的時候將消息對象轉換為字節碼; ## 三、快速入門 用一個簡單的HttpServer實現例子作為示范; 只需要兩個類,其中啟動類負責啟動(BootStrap)和main方法,另外是ChannelHandler,負責具體的業務邏輯; ``` public class NettySampleServer { private static Logger logger = LoggerFactory.getLogger(NettySampleServer.class); public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new NettySampleServerHandler()); } }); logger.info("server is ready......"); ChannelFuture channelFuture = bootstrap.bind(6666).sync(); channelFuture.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } ``` ``` public class NettySampleServerHandler extends ChannelInboundHandlerAdapter { private static Logger logger = LoggerFactory.getLogger(NettySampleServerHandler.class); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; logger.info("收到[{}]消息[{}]", ctx.channel().remoteAddress(), byteBuf.toString(CharsetUtil.UTF_8)); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(Unpooled.copiedBuffer("已收到", CharsetUtil.UTF_8)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close(); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看