1.引入netty的pomjava
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.10.Final</version> </dependency>
2.編寫代碼bootstrap
1 package com.bill.httpdemo; 2 3 4 import io.netty.bootstrap.ServerBootstrap; 5 import io.netty.channel.ChannelFuture; 6 import io.netty.channel.EventLoopGroup; 7 import io.netty.channel.nio.NioEventLoopGroup; 8 import io.netty.channel.socket.nio.NioServerSocketChannel; 9 10 public class HttpServer { 11 12 public static void main(String[] args) throws Exception { 13 14 // 這2個group都是死循環,阻塞式 15 EventLoopGroup bossGroup = new NioEventLoopGroup(); 16 EventLoopGroup workerGroup = new NioEventLoopGroup(); 17 18 try { 19 ServerBootstrap serverBootstrap = new ServerBootstrap(); 20 serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class). 21 childHandler(new HttpServerInitializer()); 22 23 ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); 24 channelFuture.channel().closeFuture().sync(); 25 } finally { 26 bossGroup.shutdownGracefully(); 27 workerGroup.shutdownGracefully(); 28 } 29 } 30 31 } 32 33 package com.bill.httpdemo; 34 35 import io.netty.buffer.ByteBuf; 36 import io.netty.buffer.Unpooled; 37 import io.netty.channel.ChannelHandlerContext; 38 import io.netty.channel.SimpleChannelInboundHandler; 39 import io.netty.handler.codec.http.*; 40 import io.netty.util.CharsetUtil; 41 42 import java.net.URI; 43 44 public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> { 45 46 /** 47 * 讀取客戶端請求,而且返回給客戶端數據的方法 48 */ 49 @Override 50 protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception { 51 52 if(!(httpObject instanceof HttpRequest)) { 53 return; 54 } 55 56 System.out.println("excute channelRead0"); 57 58 HttpRequest httpRequest = (HttpRequest) httpObject; 59 60 URI uri = new URI(httpRequest.uri()); 61 System.out.println(uri.getPath()); 62 63 ByteBuf content = Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8); 64 FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, 65 HttpResponseStatus.OK, content); 66 response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain"); 67 response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes()); 68 69 channelHandlerContext.writeAndFlush(response); 70 } 71 } 72 73 package com.bill.httpdemo; 74 75 import io.netty.channel.ChannelInitializer; 76 import io.netty.channel.ChannelPipeline; 77 import io.netty.channel.socket.SocketChannel; 78 import io.netty.handler.codec.http.HttpServerCodec; 79 80 public class HttpServerInitializer extends ChannelInitializer<SocketChannel> { 81 82 @Override 83 protected void initChannel(SocketChannel socketChannel) throws Exception { 84 85 ChannelPipeline pipeline = socketChannel.pipeline(); 86 87 pipeline.addLast("HttpServerCodec", new HttpServerCodec()); 88 pipeline.addLast("HttpServerHandler", new HttpServerHandler()); 89 } 90 }
3.啓動服務器,執行HttpServer的main方法瀏覽器
4.瀏覽器輸入網址:服務器
http://127.0.0.1:8899/hello/worldsocket
客戶端輸出:ide
服務器輸出:oop
完整代碼下載:spa