日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

深入浅出Netty之四 Client请求处理

發布時間:2025/3/19 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深入浅出Netty之四 Client请求处理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前2篇分析了echo server端的運行機制,本篇同樣以echo client為例,分析netty的nio客戶端的運行機制。 總體來說client端和server端的處理是類似的,NioWorker是重用的,也就意味著client和server的讀寫機制是一樣的,都是通過worker線程來管理的。所不同的是Boss線程,server端的boss線程一個bind端口起一個,主要負責接收新請求,而client端的boss線程是一個可配置的數組,一個connect端口分配一個,主要負責connect過程,如果connect成功則將channle注冊到worker線程中處理。在Client同樣有PipelineSink,叫做NioClientSocketPipelineSink,也是負責底層IO和pipeline之間的交互。 EchoClient代碼: Java代碼
  • // 初始化Bootstrap和NioClientSocketChannelFactory,這一步將啟動nioWorker線程,并初始化NioClientSocketPipelineSink,并將Boss線程創建
  • ClientBootstrap bootstrap = new ClientBootstrap(
  • new NioClientSocketChannelFactory(
  • Executors.newCachedThreadPool(),
  • Executors.newCachedThreadPool()));
  • // 用戶自定義的pipeline工廠
  • bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
  • public ChannelPipeline getPipeline() throws Exception {
  • return Channels.pipeline(
  • new EchoClientHandler(firstMessageSize));
  • }
  • });
  • // 異步創建連接
  • ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
  • //等待連接關閉
  • future.getChannel().getCloseFuture().awaitUninterruptibly();
  • // 關閉資源,線程池等
  • bootstrap.releaseExternalResources();
  • // 初始化Bootstrap和NioClientSocketChannelFactory,這一步將啟動nioWorker線程,并初始化NioClientSocketPipelineSink,并將Boss線程創建ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newCachedThreadPool()));// 用戶自定義的pipeline工廠bootstrap.setPipelineFactory(new ChannelPipelineFactory() {public ChannelPipeline getPipeline() throws Exception {return Channels.pipeline(new EchoClientHandler(firstMessageSize));}});// 異步創建連接ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));//等待連接關閉future.getChannel().getCloseFuture().awaitUninterruptibly();// 關閉資源,線程池等bootstrap.releaseExternalResources(); 具體connect過程: 一.創建client的channel Java代碼
  • public ChannelFuture connect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
  • ......
  • //拿用戶自定義的pipeline
  • ChannelPipeline pipeline;
  • try {
  • pipeline = getPipelineFactory().getPipeline();
  • } catch (Exception e) {
  • throw new ChannelPipelineException("Failed to initialize a pipeline.", e);
  • }
  • // 從ChannelFactory中,創建Channel,對于client來說factory是NioClientSocketChannelFactory,Channel是NioClientSocketChannel
  • Channel ch = getFactory().newChannel(pipeline);
  • // 通過channel連接
  • return ch.connect(remoteAddress);
  • }
  • public ChannelFuture connect(final SocketAddress remoteAddress, final SocketAddress localAddress) {......//拿用戶自定義的pipelineChannelPipeline pipeline;try {pipeline = getPipelineFactory().getPipeline();} catch (Exception e) {throw new ChannelPipelineException("Failed to initialize a pipeline.", e);}// 從ChannelFactory中,創建Channel,對于client來說factory是NioClientSocketChannelFactory,Channel是NioClientSocketChannelChannel ch = getFactory().newChannel(pipeline);// 通過channel連接return ch.connect(remoteAddress);} 二.創建channel時,分配worker Java代碼
  • public SocketChannel newChannel(ChannelPipeline pipeline) {
  • return new NioClientSocketChannel(this, pipeline, sink, sink.nextWorker());
  • }
  • public SocketChannel newChannel(ChannelPipeline pipeline) {return new NioClientSocketChannel(this, pipeline, sink, sink.nextWorker());} 三.創建內部的SocketChannel,觸發ChannelOpen事件,不過echo client的handler沒有對channelOpen事件做處理

    轉載于:https://blog.51cto.com/daheyuan/1140093

    總結

    以上是生活随笔為你收集整理的深入浅出Netty之四 Client请求处理的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。