基于http的netty demo

1.引入netty的pom

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.10.Final</version>
</dependency>

2.编写代码

 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/world

客户端输出:

服务器输出:

 

完整代码下载:

https://download.csdn.net/download/mweibiao/10551574

posted @ 2018-07-18 21:20  无名草110  阅读(430)  评论(0编辑  收藏  举报