使用Netty 实现一个简单的Http服务器,可以接受客户端的请求,并且实现拒绝请求一些请求,比如请求favicon.ico 网站图标的时候拒绝请求。
服务器收到客户端请求之后回传一个简单的消息: "hello, 我是服务器"
1. 代码
1. NettyHttpServerHandler
处理Http请求,如果不是Http请求则拒绝处理。
package netty; 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.*; import io.netty.util.CharsetUtil; import java.net.URI; public class NettyHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> { //channelRead0 读取客户端数据 @Override protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { System.out.println("对应的channel=" + ctx.channel() + " pipeline=" + ctx .pipeline() + " 通过pipeline获取channel" + ctx.pipeline().channel()); System.out.println("当前ctx的handler=" + ctx.handler()); //判断 msg 是不是 httprequest请求 if (msg instanceof HttpRequest) { System.out.println("ctx 类型=" + ctx.getClass()); System.out.println("pipeline hashcode" + ctx.pipeline().hashCode() + " TestHttpServerHandler hash=" + this.hashCode()); System.out.println("msg 类型=" + msg.getClass()); System.out.println("客户端地址" + ctx.channel().remoteAddress()); //获取到 HttpRequest httpRequest = (HttpRequest) msg; //获取uri, 过滤指定的资源 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的相应,即 httpresponse FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8"); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()); //将构建好 response返回 ctx.writeAndFlush(response); } } }
2. NettyServerInitializer 初始化器
package netty; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.http.HttpServerCodec; public class NettyServerInitializer extends ChannelInitializer<SocketChannel> { @Override protected 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. 增加一个自定义的handler pipeline.addLast("MyTestHttpServerHandler", new NettyHttpServerHandler()); System.out.println("ok~~~~"); } }
3. NettyHttpServer主类
package netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class NettyHttpServer { public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new NettyServerInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(6565).sync(); System.out.println("server is ok"); channelFuture.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
2. 测试
浏览器发送请求:
服务器端日志如下:
ok~~~~ 对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] 当前ctx的handler=netty.NettyHttpServerHandler@464839a5 ctx 类型=class io.netty.channel.DefaultChannelHandlerContext pipeline hashcode55230216 TestHttpServerHandler hash=1179138469 msg 类型=class io.netty.handler.codec.http.DefaultHttpRequest 客户端地址/127.0.0.1:56045 对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] 当前ctx的handler=netty.NettyHttpServerHandler@464839a5 对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] 当前ctx的handler=netty.NettyHttpServerHandler@464839a5 ctx 类型=class io.netty.channel.DefaultChannelHandlerContext pipeline hashcode55230216 TestHttpServerHandler hash=1179138469 msg 类型=class io.netty.handler.codec.http.DefaultHttpRequest 客户端地址/127.0.0.1:56045 请求了 favicon.ico, 不做响应 对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] 当前ctx的handler=netty.NettyHttpServerHandler@464839a5