Netty的http服务器编程(三)

2021-07-03 21:06

阅读:467

标签:worker   font   event   客户   efault   log   final   imp   服务启动   

 

Netty提供的handlerHttpServerCodec http 请求编解码处理类

 

 

第一个netty的例子,,server服务端的编写,客户端使用 curl : http://IP:PORT请求 

 

 

 

服务启动类

 

 1 public class TestServer {
 2 
 3     public static void main(String[] args) throws Exception {
 4 
 5         //定义两个线程组
 6         EventLoopGroup bossGroup = new NioEventLoopGroup();//接收连接,分发给worker
 7         EventLoopGroup workerGroup = new NioEventLoopGroup();//处理连接
 8 
 9         try{
10             //启动服务器  : 简化服务端启动
11             ServerBootstrap serverBootstrap = new ServerBootstrap();
12             serverBootstrap.group(bossGroup,workerGroup)
13                     .channel(NioServerSocketChannel.class)
14                     .childHandler(new TestServerInitializer());//子处理器
15 
16             //绑定端口
17             ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
18             channelFuture.channel().closeFuture().sync();
19         }finally {
20 
21             //优雅关闭线程组
22             bossGroup.shutdownGracefully();
23             workerGroup.shutdownGracefully();
24         }
25 
26 
27 
28     }
29 
30 
31 }

 

 

 

初始化类 :

 

 1 public class TestServerInitializer extends ChannelInitializer {
 2 
 3     @Override
 4     protected void initChannel(SocketChannel ch) throws Exception {
 5         //拦截器
 6         ChannelPipeline pipeline = ch.pipeline();
 7         //注册拦截器
 8         pipeline.addLast("httpServerCodec",new HttpServerCodec());//请求编解码
 9         pipeline.addLast("testHttpServerHandler",new TestHttpServerHandler());
10     }
11 }

 

 

 

 

 

自定义处理器类:

 

 1 public class TestHttpServerHandler  extends SimpleChannelInboundHandler {
 2 
 3     //读取客户端的请求,向客户端返回响应的方法
 4     //计划在5.0 改名 messagereceived  消息接收,5.0已废弃
 5     @Override
 6     protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
 7 
 8         System.out.println(msg.getClass());
 9 
10         System.out.println(ctx.channel().remoteAddress());
11 
12         Thread.sleep(8000);
13 
14         if(msg instanceof HttpRequest){
15             HttpRequest httpRequest = (HttpRequest)msg;
16 
17             System.out.println("请求方法名:"+httpRequest.getMethod().name());
18 
19             URI uri = new URI(httpRequest.getUri());
20             if("/favicon.ico".equals(uri.getPath())){
21                 System.out.println("请求favicon.ico");
22                 return;
23             }
24             //构造响应内容
25             ByteBuf content = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
26 
27             FullHttpResponse response = new DefaultFullHttpResponse(
28                     HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
29 
30             response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
31             response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
32 
33 
34             //返回响应
35             ctx.writeAndFlush(response);
36             ctx.close();
37         }
38 
39     }
40 
41     @Override
42     public void channelActive(ChannelHandlerContext ctx) throws Exception {
43         System.out.println("channel active");
44         super.channelActive(ctx);
45     }
46 
47     @Override
48     public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
49         System.out.println("channel registered");
50         super.channelRegistered(ctx);
51     }
52 
53     @Override
54     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
55         System.out.println("handler added");
56         super.handlerAdded(ctx);
57     }
58 
59     @Override
60     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
61         System.out.println("channel inactive");
62         super.channelInactive(ctx);
63     }
64 
65     @Override
66     public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
67         System.out.println("channel unregistered");
68         super.channelUnregistered(ctx);
69     }
70 }

 

Netty的http服务器编程(三)

标签:worker   font   event   客户   efault   log   final   imp   服务启动   

原文地址:http://www.cnblogs.com/fubinhnust/p/7123543.html


评论


亲,登录后才可以留言!