1. 众所周知,Http协议的上层是TCP,Netty作为非阻塞框架的大佬,完全有能力承担高并发,高可用的角色.先上车,后解释,
2. 可以用Netty创建一个TCP服务,用浏览器请求,看能否收到请求,只要响应的是Http响应头,浏览器就可以解析
1.HttpServer.java
public class HttpServer {
public void start(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.handler(new LoggingHandler(LogLevel.DEBUG))
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
// server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
ch.pipeline().addLast(
new HttpResponseEncoder());
// server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
ch.pipeline().addLast(
new HttpRequestDecoder());
ch.pipeline().addLast(
new HttpObjectAggregator(512 * 1024));
ch.pipeline().addLast(
new HttpServerHandler());
//增加自定义实现的Handler
ch.pipeline().addLast(new HttpServerCodec());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
HttpServer server = new HttpServer();
server.start(8080);
}
public class HttpServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) msg;
System.out.println(Thread.currentThread().getId() + "->");
System.out.println(httpRequest.uri() + ":" + ctx.channel().remoteAddress().toString());
String uri = httpRequest.uri();
if (msg instanceof HttpContent) {
if(HttpPostRequestDecoder.isMultipart(httpRequest)){
// 是POST请求
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(httpRequest);
decoder.offer((HttpContent)httpRequest);
List<InterfaceHttpData> parmList = decoder.getBodyHttpDatas();
System.out.println("form conent : \r\n" + parmList);
}else{
HttpContent content = (HttpContent) msg;
ByteBuf buf = content.content();;
String requestString = buf.toString(io.netty.util.CharsetUtil.UTF_8);
buf.release();
System.out.println("json conent : \r\n" + requestString);
}
}
Map<String, String> resMap = new HashMap<>();
resMap.put("method", httpRequest.method().name());
Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(httpRequest.headers().get(HttpHeaderNames.COOKIE));
resMap.put("uri", uri);
// String text = "<html><head><title>test</title></head><body>你请求 port:"+ctx.channel().remoteAddress().toString()+" uri为:" + uri + ":" + httpRequest.headers().get(HttpHeaderNames.COOKIE) + "</body></html>";
String text = "<html><head><title>test</title></head><body>你请求 port:" + ctx.channel().remoteAddress().toString() + " uri为:" + uri + "</body></html>";
// 创建http响应
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.OK,
Unpooled.copiedBuffer(text, CharsetUtil.UTF_8));
// 设置头信息
response.headers().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("JSESESSION", "1234"));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
// 将html write到客户端
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
String text = "<html><head><title>test</title></head><body>你请求uri为:" + cause.toString() + "</body></html>";
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.OK,
Unpooled.copiedBuffer(text, CharsetUtil.UTF_8));
// 设置头信息
response.headers().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("JSESESSION", "1234"));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
// 将html write到客户端
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
ctx.close();
}
线程组:
EventLoopGroup bossGroup = new NioEventLoopGroup();//主线程
EventLoopGroup workerGroup = new NioEventLoopGroup();// 工作线程
主线程用来接受tcp请求并分发,工作线程用来处理请求
解码器(解析tcp请求过来的数据)
HttpRequestDecoder()
HttpPostRequestDecoder.isMultipart(httpRequest)
用来区分是表单还是Json