一、前言
- 实例要求:使用 IDEA 创建 Netty 项目
- Netty 服务器在 6668 端口监听,浏览器发出请求 http://localhost:6668/
- 服务器可以回复消息给客户端”Hello!我是服务器5”,并对特定请求资源进行过滤。
- 目的:Netty 可以做 Http 服务开发,并且理解 Handler 实例和客户端及其请求的关系。
- 看老师代码演示
二、示例代码
HttpServer
```java package com.supkingx.netty.http;
import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
- @description:
- @Author: wangchao
@Date: 2021/12/9 */ public class HttpServer { public static void main(String[] args) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);NioEventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ServerInitializer());ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();channelFuture.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}
} }
<a name="M0sxT"></a>#### HttpServerCodec�HttpServerCodec 是 netty 提供的处理 http 的编码解码器<br />通过下图可以看到它继承了 decoder 和 encoder<br /><a name="fjKYY"></a>## ServerInitializer```javapackage com.supkingx.netty.http;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.http.HttpServerCodec;/*** @description:* @Author: wangchao* @Date: 2021/12/9*/public class ServerInitializer extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {// 向管道加入处理器// 得到管道ChannelPipeline pipeline = ch.pipeline();// 加入一个 netty 提供的 httpServerCodec codec =》【coder - decoder】// HttpServerCodec 说明// 1、HttpServerCodec 是 netty 提供的处理 http 的编码解码器pipeline.addLast("MyHttpServerCodec", new HttpServerCodec());// 2、增加一个自定义的handlerpipeline.addLast("MyTestHttpServerHandler", new HttpServerHandler());}}
HttpServerHandler
package com.supkingx.netty.http;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.codec.http.DefaultFullHttpRequest;import io.netty.handler.codec.http.DefaultFullHttpResponse;import io.netty.handler.codec.http.DefaultHttpResponse;import io.netty.handler.codec.http.FullHttpResponse;import io.netty.handler.codec.http.HttpHeaderNames;import io.netty.handler.codec.http.HttpObject;import io.netty.handler.codec.http.HttpRequest;import io.netty.handler.codec.http.HttpResponseStatus;import io.netty.handler.codec.http.HttpVersion;import io.netty.util.CharsetUtil;/*** @description: * SimpleChannelInboundHandler 继承了 ChannelInboundHandlerAdapter* * HttpObject 客户端和服务端相互通讯的数据被封装成 HttpObject* @Author: wangchao* @Date: 2021/12/9*/public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {/*** 读取数据** @param ctx 上下文对象,含有 管道pipeline,通道channel,地址* @param msg 就是客户端发送的数据 默认Object* @throws Exception*/@Overrideprotected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {// 判断 msg 是不是 HttpRequest 请求if (msg instanceof HttpRequest) {System.out.println("msg 类型=" + msg.getClass());System.out.println("客户端地址" + ctx.channel().remoteAddress());// 回复信息给浏览器 【http协议】ByteBuf content = Unpooled.copiedBuffer("hello,我是服务器", CharsetUtil.UTF_8);// 构建一个 http 响应,即 httpResponseFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());// 将构建好的 response 返回ctx.writeAndFlush(httpResponse);}}}
演示
启动浏览器并输出 localhost:8899,可以看到浏览器返回值
疑问
同时发现一个问题,通过服务端日志可以发现浏览器请求了两次,这是为什么?
通过浏览器开发工具可知,如下,第一次请求服务器,第二次请求网站图标
选择性接受浏览器请求
通过 获取 uri 过滤特定资源
@Overrideprotected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {// 判断 msg 是不是 HttpRequest 请求if (msg instanceof HttpRequest) {System.out.println("msg 类型=" + msg.getClass());System.out.println("客户端地址" + ctx.channel().remoteAddress());// 获取HttpRequest httpRequest = (HttpRequest) msg;URI uri = new URI(httpRequest.uri());if ("/favicon.ico".equals(uri.getPath())) {System.out.println("请求了 favicon.ico,不做响应");return;}// 回复信息给浏览器 【http协议】ByteBuf content = Unpooled.copiedBuffer("hello,我是服务器", CharsetUtil.UTF_8);// 构建一个 http 响应,即 httpResponseFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());// 将构建好的 response 返回ctx.writeAndFlush(httpResponse);}}
三、杂记
验证浏览器的每一个HTTP链接都会对应一个新 pipeline 和 handler
验证方法:开启多个浏览器窗口实验
@Overrideprotected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {// 判断 msg 是不是 HttpRequest 请求if (msg instanceof HttpRequest) {// 验证浏览器的每一个新窗口都会对应一个新 pipeline 和 handler// 如果是在原窗口不停的请求,都是同一个 pipeline 和 handlerSystem.out.println("pipeline hashcode" + ctx.pipeline().hashCode() + "; HttpServerHandler hashcode=" + this.hashCode());。。。。。。。。。。。。。。。。
