向netty服务器发出请求时,未调用ChannelRead方法

向netty服务器发出请求时,未调用ChannelRead方法,netty,Netty,我正在实现一个netty服务器,它接受http请求并执行一些业务逻辑 基本上,我接受localhost/hello上的GET请求,sysouturi是hello 运行服务器时,在CustomRequestHandler#channelRead0方法中看不到任何系统输出。 我一直打开调试器,我看到channelRead0方法没有被调用。 我想不出是什么问题 我使用的是Netty 4.1.30.Final版本 public class HttpServer { public static

我正在实现一个netty服务器,它接受http请求并执行一些业务逻辑 基本上,我接受localhost/hello上的GET请求,sysout
uri是hello

运行服务器时,在CustomRequestHandler#channelRead0方法中看不到任何系统输出。 我一直打开调试器,我看到channelRead0方法没有被调用。 我想不出是什么问题

我使用的是Netty 4.1.30.Final版本

public class HttpServer {

    public static void main( String[] args ) throws Exception {

        InetSocketAddress tcpSocketAddress = new InetSocketAddress(8080);

        System.out.println("Starting http server at " + tcpSocketAddress.getPort());
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {

        ServerBootstrap bootstrap = new ServerBootstrap()
                .group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new MyChannelInitializer())
                .childOption(ChannelOption.AUTO_READ, false);


        ChannelFuture serverChannelFuture = bootstrap.bind(tcpSocketAddress).sync();
        System.out.println("open the browser");
        serverChannelFuture.channel().closeFuture().sync();
        } finally {
             bossGroup.shutdownGracefully();
             workerGroup.shutdownGracefully();
        }
    }
}


public class MyChannelIntializer extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel ch) {
        ChannelPipeline p = ch.pipeline();
        p.addLast(new LoggingHandler(LogLevel.INFO));
        //p.addLast(new HttpServerCodec());
        p.addLast(new HttpRequestDecoder());
        p.addLast(new HttpResponseEncoder());
        p.addLast(new CustomRequestHandler());
    }
}

public class CustomRequestHandler extends SimpleChannelInboundHandler<Object> {

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object httpObject) {
        System.out.println("hello world");
        if (httpObject instanceof HttpRequest) {
            HttpRequest request = (HttpRequest) httpObject;
            System.out.println("uri is " + request.getUri());
    }
}
公共类HttpServer{
公共静态void main(字符串[]args)引发异常{
InetSocketAddress tcpSocketAddress=新的InetSocketAddress(8080);
System.out.println(“在“+tcpSocketAddress.getPort()上启动http服务器”);
EventLoopGroup bossGroup=新的NioEventLoopGroup(1);
EventLoopGroup workerGroup=新的NioEventLoopGroup();
试一试{
ServerBootstrap bootstrap=newserverbootstrap()
.group(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)
.handler(新的LoggingHandler(LogLevel.INFO))
.childHandler(新的MyChannelInitializer())
.childOption(ChannelOption.AUTO_READ,false);
ChannelFuture服务器ChannelFuture=bootstrap.bind(tcpSocketAddress.sync();
System.out.println(“打开浏览器”);
serverChannelFuture.channel().closeFuture().sync();
}最后{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
公共类MyChannelInitializer扩展了ChannelInitializer{
@凌驾
公共频道(SocketChannel ch){
ChannelPipeline p=通道管道();
p、 addLast(新的LoggingHandler(LogLevel.INFO));
//p、 addLast(新的HttpServerCodec());
p、 addLast(新的HttpRequestDecoder());
p、 addLast(新的HttpResponseEncoder());
p、 addLast(新的CustomRequestHandler());
}
}
公共类CustomRequestHandler扩展了SimpleChannelInboundHandler{
@凌驾
公共无效channelReadComplete(ChannelHandlerContext ctx){
ctx.flush();
}
@凌驾
受保护的无效channelRead0(ChannelHandlerContext ctx,对象httpObject){
System.out.println(“你好世界”);
if(HttpRequest的httpObject实例){
HttpRequest请求=(HttpRequest)httpObject;
System.out.println(“uri是”+request.getUri());
}
}

问题在于您使用:

                .childOption(ChannelOption.AUTO_READ, false);
这意味着一旦通道被接受,它将不会处理任何传入数据,直到您每次要处理更多入站数据时调用
ctx.read()


因此,您可以选择删除此行或覆盖
channelActive(…)
以及
CustomRequestHandler
中的
ctx.read()
。这将确保您将尝试读取一些入站数据。也就是说,您需要调用
ctx.read()
当您想处理更多数据时,请再次执行此操作。

对于每个请求,都会创建一个新管道?不是针对每个请求,而是针对每个通道(连接)。如果您有其他问题,请打开另一个堆栈溢出问题。只打开一个。。