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>>() {
@Override
public 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.
Let's do our own full blown HTTP server with Netty--转载的更多相关文章
- 758B Blown Garland
B. Blown Garland time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces758B Blown Garland 2017-01-20 10:19 87人阅读 评论(0) 收藏
B. Blown Garland time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- B. Blown Garland
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【codeforces 758B】Blown Garland
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 758B Blown Garland
题目链接:http://codeforces.com/contest/758/problem/B 题意:一个原先为4色环的链子少了部分,要你找出死的最少的一种可能,各输出四种颜色的死了多少. 分析:就 ...
- Codeforces 758B:Blown Garland(模拟)
http://codeforces.com/problemset/problem/758/B 题意:给出一个字符串,每4个位置对应一个颜色,如果为‘!’的话,代表该灯泡是坏的,问最后每个颜色坏的灯泡的 ...
- Codeforces 392 B Blown Garland
题意:输入是由连续的RYGB和字符!组成的字符串,R代表红灯,Y代表黄灯,G代表绿灯,B代表蓝灯.简而言之,就是给定的字符串保证了下标对4取余相同的灯颜色都相同,但是有的地方为‘!’代表这个灯坏了,然 ...
- Essential controls for web app
AUTO-COMPLETE/AUTO-SUGGEST Auto-complete using Vaadin Offer auto-suggest or auto-complete to help yo ...
- Bible
001 Love your neighbor as yourself. 要爱人如己.--<旧·利>19:18 002 Resentment kills a foo ...
随机推荐
- Eclipse怎么导入外来项目
从File中点击------>import----->General------>然后按下面的图片显示
- iOS8 对开发人员来说意味着什么?
今天凌晨.Apple WWDC2014 iOS8 正式推出. 或许,对于广大iOS用户来说,iOS8的创新并非特别多. 但对于开发人员来说,影响却将会是无比巨大的! 正如Apple官网上的广告:Hug ...
- 用YourAPP开发网络状态提醒应用
如今的通信真是方便,走到哪里都有网络.Wifi的利用已经到了很普及的程度.即使走到没有wifi信号的地方,利用手机信号也能上网.(若是连手机信号都没有,那就没办法了) 智能手机的使用也大慷慨面了各个年 ...
- java种instanceof方法和getclass方法的区别
在比较一个类是否和另一个类属于同一个类实例的时候,我们通常可以采用instanceof和getClass两种方法通过两者是否相等来判断,但是两者在判断上面是有差别的,下面写个测试类. public c ...
- JS实现下拉菜单的功能
<!DOCTYPE html> <html> <head> <meta charset = "utf8"> <title> ...
- 【DRF解析器和渲染器】
目录 解析器 Django中的解析器 DRF中的解析器 DRF中的渲染器 @ *** 解析器 解析器的作用就是服务端接收客户端传过来的数据,把数据解析成自己想要的数据类型的过程. 本质就是对请求体中的 ...
- hdu5400Arithmetic Sequence
//一个序列,两个公差d1,d2 //问有多少个区间使得这个区间存在一个点,它的左边是公差为d1的序列 //它的右边是公差为d2的序列 //直接存入每一个点向左和向右延伸的公差长度,乘一下即可 //还 ...
- Android使用蓝牙连接adb调试App
使用WiFi连接Android设备调试APP的教程非常多,可是项目中须要使用蓝牙进行通信.所以牵扯使用蓝牙调用adb. 1. 将电脑蓝牙与手机进行配对(控制面板->设备和打印机->加入 ...
- LeetCode 136 Single Number(仅仅出现一次的数字)
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array ...
- 8lession-基础类型转化
Python数据类型转换 有时候,我们需要对数据内置的类型进行转换,数据类型的转换,你只需要将数据类型作为函数名即可. 以下几个内置的函数可以执行数据类型之间的转换.这些函数返回一个新的对象,表示转换 ...