栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Netty高级进阶之基于Netty的HTTP服务器开发

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Netty高级进阶之基于Netty的HTTP服务器开发

本通过实战演练,学习了如何基于Netty开发一个HTTP服务器。

Netty高级进阶之基于Netty的HTTP服务器开发 介绍

Netty的HTTP协议栈可靠性高,性能优异。相对于传统的Tomcat、Jetty等服务器,它更加轻量级和小巧,灵活性和定制型也更好。

功能需求
  1. Netty服务器在8080端口监听
  2. 浏览器发出请求”http://localhost:8080“
  3. 服务器回复消息给客户端”你好,我是Netty服务器“,并对特定请求资源进行过滤
服务端代码实现
  1. NettyHttpServer

    public class NettyHttpServer {
        private int port;
    
        public NettyHttpServer(int port) {
            this.port = port;
        }
    
        public void run() throws InterruptedException {
            NioEventLoopGroup bossGroup = null;
            NioEventLoopGroup workerGroup = null;
            try {
                // 1. 创建bossGroup线程组: 处理网络事件--连接事件,默认是2*处理器线程数目
                bossGroup = new NioEventLoopGroup(1);
                // 2. 创建workerGroup线程组: 处理网络事件--读写事件,默认是2*处理器线程数目
                workerGroup = new NioEventLoopGroup();
                // 3. 创建服务端启动助手
                ServerBootstrap serverBootstrap = new ServerBootstrap();
                // 4. 设置bossGroup线程组和workerGroup线程组
                serverBootstrap.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 128)  // 5. 设置服务端通道实现为NIO
                        .childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE)  // 6. 参数设置,设置活跃状态,child是设置workerGroup
                        .childHandler(new ChannelInitializer() { // 7. 创建一个通道初始化对象
                            @Override
                            protected void initChannel(SocketChannel ch) throws Exception {
                                // 8. 向pipeline中添加自定义业务处理handler
                                // 自定义处理类
                                ch.pipeline().addLast(new HttpServerCodec());
                                ch.pipeline().addLast(new NettyHttpServerHandler());
                            }
                        });
                // 9. 启动服务端并绑定端口,同时将异步改为同步
                ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
                System.out.println("HTTP服务器启动成功");
    
                channelFuture.addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (future.isSuccess()) {
                            System.out.println("端口绑定成功!");
                        } else {
                            System.out.println("端口绑定失败!");
                        }
                    }
                });
    
                // 10. 关闭通道和关闭连接池(不是真正关闭,只是设置为关闭状态)
                channelFuture.channel().closeFuture().sync();
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    
        public static void main(String[] args) throws InterruptedException {
            new NettyHttpServer(8080).run();
        }
    }
    
  2. HTTP处理类

    public class NettyHttpServerHandler extends SimpleChannelInboundHandler {
    
        
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
            // 1.判断是否是HTTP请求
            if (msg instanceof HttpRequest) {
                DefaultHttpRequest request = (DefaultHttpRequest) msg;
                System.out.println("浏览器请求路径:" + request.uri());
    
                // 2.响应浏览器
                ByteBuf byteBuf = Unpooled.copiedBuffer("你好,我是Netty服务端", CharsetUtil.UTF_8);
                DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
                response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
                response.headers().set(HttpHeaderNames.CONTENT_LENGTH, byteBuf.readableBytes());
    
                ctx.writeAndFlush(response);
            }
        }
    }
    
  3. 运行效果

  4. 过滤掉图标

    // 图标不响应
    if("/favicon.ico".equals(request.uri())){
        System.out.println("图标不处理");
        return;
    }
    

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aaMLOZRf-1651643835703)(https://cdn.jsdelivr.net/gh/terwer/upload/img/image-20220429013259492.png)]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860198.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号