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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Let's do our own full blown HTTP server with Netty--转载

發布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Let's do our own full blown HTTP server with Netty--转载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://adolgarev.blogspot.com/2013/12/lets-do-our-own-full-blown-http-server.html

Sometimes servlets just doesn't fit you, sometimes you need to support some protocols except HTTP, sometimes you need something really fast. Allow me to show you the Netty that can suit for these needs.?

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. (http://netty.io/)

Netty has everything one needs for HTTP, thus web server on Netty is like a low hanging fruit.
First of all you need to understand what pipeline is, see?Interface ChannelPipeline.?Pipeline is like processing line where various?ChannelHandlers convert input bytes into output. Pipeline corresponds one to one to connection, thus?ChannelHandlers in our case will convert HTTP Request into HTTP Response, handlers will be responsible for such auxiliary things like parsing incoming packets and assembling outcoming and also call business logic to handle requests and produce responses.
Full source is available.

There are 2 types of handlers.?

  • ChannelInboundHandlers?that process incoming data.

  • ChannelOutboundHandlers?that produce data to be sent.

  • See?How an event flows in a pipeline.
    In our case we'll create following inbound handlers

    --(ByteBuf)-->?HttpRequestDecoder?--(HttpObject)-->?HttpObjectAggregator?--(FullHttpRequest)-->?RequestDecoder*?--(Request*)-->?FormPayloadDecoder*?--(FullDecodedRequest*)-->?DefaultHandler*?--(Response*)-->?DefaultExceptionHandler*

    ?

    In braces you see messages being passed between handlers. With * marked our custom classes, all others are from Netty.?

    • HttpRequestDecoder is responsible for low level parsing and converts raw bytes into HttpObject that correspond to HTTP request header and body chunks.

    • HttpObjectAggregator composes from these objects one single FullHttpRequest per HTTP request. It can be configured with?maxContentLength to prevent too big requests.

    • RequestDecoder simply wraps FullHttpRequest into custom Request and adds auxiliary parameters namely orderNumber. HTTP client can send multiple requests in one connection and it expects responses in the same order. We process those requests in parallel and for instance second one can be processed before first. Thus we need to assembly responses in the right order before sending them out. For this purpose we assign index number to each incoming Request in our pipeline.

    Example. Imagine to process request we just sleep specified amount of seconds.?

    telnet?localhost?9999 Trying?::1... Connected?to?localhost. Escape?character?is?'^]'. GET?/?duration=5?HTTP/1.1?<--?sleep?for?5?seconds User-Agent:?curl/7.33.0 Host:?localhost:9999 Accept:?*/*GET?/?duration=3?HTTP/1.1?<--?sleep?for?3?seconds User-Agent:?curl/7.33.0 Host:?localhost:9999 Accept:?*/*HTTP/1.1?200?OK?<--?despite?second?request?processed?first?we?get?both?responses?in?the?right?order?after?5?seconds Content-Type:?application/json Content-Length:?18 Set-Cookie:?JSESSIOINID=e9ma1foeretnh19u4demqta7tr Connection:?keep-alive"Slept?for?5?secs"HTTP/1.1?200?OK Content-Type:?application/json Content-Length:?18 Set-Cookie:?JSESSIOINID=8ti5pbfc0dmd4r09i6or005r6b Connection:?keep-alive"Slept?for?3?secs"

    ?

    • FormPayloadDecoder implements incoming parameters parsing. It uses?QueryStringDecoder and?HttpPostRequestDecoder from netty to parse application/x-www-form-urlencoded and multipart/form-data encoded GET and POST data. It produces?FullDecodedRequest that differs from?Request by containing?Values - simple key-value mapping for parameter names and values. You can easily replace?FormPayloadDecoder with any other handler that converts Request into?FullDecodedRequest, for instance, parse JSON encoded data in POST body.

    Sidenote. 'Why we cannot just update Request object like Request.setValues() and pass it further?' you say. There is a good reason to keep messages being passed immutable in multithreaded application (even though a connection is bound to particular thread in netty). Even more, type gives you an information about what you actually have.?FullDecodedRequest clearly says that parameters were parsed, in case we make Request object mutable and set there Values one cannot say at any given point in time whether Request already contains Values or not.?DefaultHandler in its turn requires?FullDecodedRequest as input, in other words it says give me fully parsed request object with parameters. In case of mutable Request this restriction is not clear.?

    • DefaultHandler uses a separate set of threads to handle business logic. It uses netty?EventExecutorGroup?that is like?ExecutorService?in Java SE.?EventExecutorGroup provides methods to submit tasks and returns futures to observe (in contrast to Java SE?Future?one can?add listeners to netty Future).?Provider below is a callable that given input parameters produces some output object.

    protected?void?channelRead0(final?ChannelHandlerContext?ctx,final?FullDecodedRequest?decodedRequest)?throws?Exception?{Callable<??extends?Object>?callable?=?new?Provider(decodedRequest.getPath(),?decodedRequest.getValues());final?Future<??extends?Object>?future?=?executor.submit(callable);future.addListener(new?GenericFutureListener<Future<Object>>()?{@Overridepublic?void?operationComplete(Future<Object>?future)throws?Exception?{if?(future.isSuccess())?{ctx.writeAndFlush(new?Response(decodedRequest.getRequest(),future.get()));}?else?{ctx.fireExceptionCaught(future.cause());}}}); }

    ?

    • DefaultExceptionHandler is a catch all handler that receives all exceptions from any handler above, makes pretty output from java stack trace and closes connection.


    Now, given a result object, one need to produce output in HTTP way. For this purpose following outbound handlers are used.?

    --(Response*)-->?JacksonJsonResponseEncoder*?--(FullEncodedResponse*)-->?ResponseEncoder*?--(FullHttpResponse)-->?HttpResponseEncoder?--(ByteBuf)-->

    ?

    • Response contains result and reference to request. JacksonJsonResponseEncoder as you may guess uses Jackson library to convert response to JSON, produces FullEncodedResponse that contains FullHttpResponse with appropriate Content-Type and Content-Length set. You can replace?JacksonJsonResponseEncoder with any other way to encode response, for instance, to xml or depending to Accept-Encoding request header.

    Sidenote about the naming. If you have some interface lets say example.Showable don't call it example.IShowable or example.ShowableI (find out by yourself why). Also do not call implementations of this interface like example.ShowableImpl or example.impl.Showable. Concrete implementation of the interface must be specific in something that differs it from all other implementations (if there is only one implementation you are trying to trick yourself that you are loosely coupled). Reflect its specifics in its name like example.ShowableWhiteBoard, example.ShowablePixelBitmap.?

    • ResponseEncoder evicts FullHttpResponse from FullEncodedResponse and handles such aspects as cookies, session ids, keep-alive, etc. Also it assembles responses in the right order.

    • HttpResponseEncoder is a netty thing that obviously produces raw bytes from?FullHttpResponse. Pay attention at ReferenceCountUtil.release(httpRequest). Netty uses pool of buffers and reference counting to omit problems with GC. See?Reference counted objects.

    All those handlers are composed together into pipeline by?DefaultServerInitializer.?Feel free to browse the source code.?Also take a look at various handlers from the box that you may find useful like?HttpContentCompressor or?SslHandler.

    轉載于:https://www.cnblogs.com/davidwang456/p/5124998.html

    總結

    以上是生活随笔為你收集整理的Let's do our own full blown HTTP server with Netty--转载的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 日韩av无码一区二区三区不卡 | 不卡的av电影 | 日韩啪啪网站 | 精品一区二区在线播放 | 一区二区三区精彩视频 | 日韩大尺度视频 | 男女操操操| 日韩av在线高清 | 国产精品伦一区二区三区免费看 | 无码丰满熟妇 | 在线中文字幕观看 | 亚洲最大毛片 | 五十路在线 | 国产视频精品在线 | 高hnp视频| 亚洲视频观看 | 成人av入口 | 蜜桃91精品入口 | 国产女人高潮毛片 | 在线视频日韩精品 | 亚洲自拍三区 | 午夜操操 | 久久日韩| 国产真实偷伦视频 | 福利资源在线观看 | 中文久草| 天天摸天天看 | 色综合五月婷婷 | 先锋影音中文字幕 | 色爱成人综合 | 亚洲a∨无码无在线观看 | www天堂在线| 久久片 | av无码精品一区二区三区宅噜噜 | 丰满少妇一区二区 | 国产黄色片免费在线观看 | 波多野在线视频 | julia一区二区中文久久97 | 国产精品久久久免费视频 | 一级大片黄色 | 亚洲成人91 | 爆操少妇 | 欧美videos另类极品 | 天天躁日日摸久久久精品 | 国产一二区在线 | 免费看一级 | 日韩国产毛片 | 青青草成人免费视频 | 国产日韩欧美精品在线 | 欧美在线激情 | 99精品国产一区二区 | 在线播放黄色网址 | 色哟哟一区二区 | 黄色片在线免费观看 | 天天天天天干 | 暧暧视频在线观看 | 日本xxxx裸体xxxx出水 | 青青草手机在线观看 | 色吧久久 | 在线观看亚洲免费视频 | 国产无遮无挡120秒 欧美综合图片 | 五月婷婷影院 | 成人免费一级片 | 久久国产色av免费观看 | 欧美aa| av在线首页 | 亚洲成人三级 | 久久久久久久久久久综合 | 国产原创在线播放 | 中文字幕日韩一区 | 毛片你懂的 | 乳孔很大能进去的av番号 | 胖女人做爰全过程 | 免费黄色美女网站 | 午夜国产在线视频 | av高清在线 | 91春色 | 神马午夜dy888| 天天碰免费视频 | 中文在线а√天堂官网 | 色老久久 | 亚洲国产精品无码久久久 | 成年人在线观看视频网站 | 欧洲黄视频 | www.youjizz日本 | 韩国黄色网址 | 欧美人与禽猛交乱配视频 | 深田咏美中文字幕 | 久久永久免费 | 成人av免费在线播放 | 用力挺进新婚白嫩少妇 | 国产又黄又骚 | 中文字幕久热 | 91蝌蚪网| 欧美人一级淫片a免费播放 西方av在线 | 亚洲高清免费视频 | 公妇借种乱htp109cc | 日韩成人av影院 | 在线精品亚洲欧美日韩国产 |