NettyExample 1 构建简单的http服务

本文档展示了如何使用Netty 4.1.15.Final构建一个简单的HTTP服务。通过实例化NioEventLoopGroup,ServerBootstrap,以及配置ChannelPipeline,包括SSL上下文、编码解码器、处理器等,实现了HTTP服务端。服务端处理类HttpServerInboundHandler负责处理FullHttpRequest,获取请求URI、头部信息和请求参数。同时,文中还提到了客户端的构建及其处理类。
摘要由CSDN通过智能技术生成
NettyExample 1 构建简单的http服务

netty 最新版本是netty-4.1.15.Final. 本来还有Netty5,但官网已经放弃5.0了,maven仓库倒是还有5.0的版本。下载netty-4.1.15.Final源码包,里面包含一个 netty-example-4.1.15.Final-sources.jar文件,提供了比较丰富的example例子。

Netty Http服务端代码实现
import com.mdq.HttpServerTest.netty.handler.HttpServerInboundHandler;
import com.mdq.HttpServerTest.netty.handler.TestInBoundHandler1;
import com.mdq.HttpServerTest.netty.handler.TestOutBoundHandler1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.cors.CorsConfig;
import io.netty.handler.codec.http.cors.CorsConfigBuilder;
import io.netty.handler.codec.http.cors.CorsHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.handler.stream.ChunkedWriteHandler;

public class NettyHttpServer {
     private static Logger log = LoggerFactory.getLogger(NettyHttpServer.class);
     static final boolean SSL = System.getProperty("ssl") != null;
     static final int PORT = Integer.parseInt(System.getProperty("port", SSL ? "8443" : "8080"));

     public void start(int port) throws Exception {
           // Configure SSL.
           final SslContext sslCtx;
           if (SSL) {
                SelfSignedCertificate ssc = new SelfSignedCertificate();
                sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
           } else {
                sslCtx = null;
           }
           EventLoopGroup bossGroup = new NioEventLoopGroup();
           EventLoopGroup workerGroup = new NioEventLoopGroup();
           try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                         .childHandler(new HttpServerChannelInitializer(sslCtx))
                         .option(ChannelOption.SO_BACKLOG, 128)
                         .childOption(ChannelOption.SO_KEEPALIVE, true);

                ChannelFuture f = b.bind(port).sync();
                f.channel().closeFuture().sync();
           } finally {
                workerGroup.shutdownGracefully();
                bossGroup.shutdownGracefully();
           }
     }

     public class HttpServerChannelInitializer extends ChannelInitializer<SocketChannel> {
           private final SslContext sslCtx;

           public HttpServerChannelInitializer(SslContext sslCtx) {
                this.sslCtx = sslCtx;
           }

           @Override
           public void initChannel(SocketChannel ch) {
                CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build();
                ChannelPipeline pipeline = ch.pipeline();
                if (sslCtx != null) {
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值