Netty应用系列之编写http服务

本文介绍了如何使用Netty框架构建一个简单的HTTP服务器。通过添加HttpServerCodec和HttpObjectAggregator处理HTTP消息的编解码,并自定义HttpServerChannelHandler处理请求和响应。启动类HttpServer展示了如何配置和启动服务器监听8080端口。这个过程类似于SpringBoot接口的实现,适合对HTTP服务感兴趣的读者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖引入

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

编写服务端

业务类HttpServerChannelHandler

public class HttpServerChannelHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
        ctx.channel().remoteAddress();
        
        FullHttpRequest request = msg;
        
        System.out.println("请求方法名称为:" + request.method().name());
        
        System.out.println("uri:" + request.uri());
        
        ByteBuf buf = request.content();
        
        System.out.println(buf.toString(CharsetUtil.UTF_8));
        
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,byteBuf);
        response.headers().add(HttpHeaderNames.CONTENT_TYPE, "text/plain");
        response.headers().add(HttpHeaderNames.CONTENT_LENGTH, byteBuf.readableBytes());
        
        ctx.writeAndFlush(response);
        
    }

}

业务类需要实现SimpleChannelInboundHandler
只需要实现一个方法channelRead0即可
该方法是获取客户端请求,然后处理,再响应给客户端,是不是和我们使用springboot写接口类似

编写注册类HttpServerInitializer

接下来我们将编写注册类,这里与编写echo服务器有所区别,echo是隐式创建了注册类,这里是显式。

public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //处理http消息的编解码
        pipeline.addLast("httpServerCodec", new HttpServerCodec());
        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
        //添加自定义的ChannelHandler
        pipeline.addLast("httpServerHandler", new HttpServerChannelHandler());
    }

}

该类需要实现ChannelInitializer
然后我们重写了initChannel方法,将服务类HttpServerChannelHandler添加进channel
另外我们可以看到,会比echo服务多出了两个类HttpServerCodec、HttpObjectAggregator,这两个是netty提供的http编解码相关的类
这两个类有什么用呢?
可以看到服务类那里FullHttpRequest(解码的来)、FullHttpResponse(会被编码)两个对象,这两个对象就是都会经过HttpServerCodec编码器编码和解码。

编写启动类HttpServer

public class HttpServer {
    public static void main(String[] args) {
      //构造两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            //服务端启动辅助类
            ServerBootstrap bootstrap = new ServerBootstrap();
 
            bootstrap.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .childHandler(new HttpServerInitializer());
 
            ChannelFuture future = bootstrap.bind(8080).sync();
            //等待服务端口关闭
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            // 优雅退出,释放线程池资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

我们这里显示把HttpServerInitializer注册进来,这样的话,我们就可以在8080端口启动一个http服务器了。
后面我们需要根据路径uri来处理不用业务了,这是我们业务类实现,是不是有点类似springboot的呢?
用浏览器测试一下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值