使用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