【问题标题】:Piped Streaming with Netty使用 Netty 进行管道流式传输
【发布时间】:2016-01-18 07:07:05
【问题描述】:

我正在尝试将“缩略图生成器”实现为微服务。我认为这样的东西可能最适合作为 TCP 服务器,因此在简要调查了一些选项后,我选择了 Netty。为了使服务尽可能节省内存,我宁愿避免将完整图像加载到内存中,因此一直在尝试构建一个管道,其“ThumbnailHandler”可以使用管道流来利用 Netty 的分块读取,以便随着 Netty 接收到更多字节,缩略图生成器可以遍历更多的流。不幸的是,我一般对 Netty 或 NIO 模式不够熟悉,不知道我是否会以最好的方式来解决这个问题,而且我什至无法让简化的版本像我预期的那样工作。

这是我的服务器设置:

public class ThumbnailerServer {

    private int port;

    public ThumbnailerServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
        final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
        final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER);
        final NioEventLoopGroup connectGroup = new NioEventLoopGroup(0, connectFactory, NioUdtProvider.BYTE_PROVIDER);

        try {
            ServerBootstrap b = new ServerBootstrap();

            b.group(acceptGroup, connectGroup)
             .channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
             .option(ChannelOption.SO_BACKLOG, 128)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new ChannelInitializer<UdtChannel>() {
                 @Override
                 public void initChannel(UdtChannel ch) throws Exception {
                     ChannelPipeline p = ch.pipeline();
                     p.addLast("handler", new ThumbnailerServerHandler());
                 }
             });

            // bind and start to accept incoming connections.
            b.bind(port).sync().channel().closeFuture().sync();
        } finally {
            connectGroup.shutdownGracefully();
            acceptGroup.shutdownGracefully();
        }
    }

}

还有缩略图处理程序:

public class ThumbnailerServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private static final Logger logger = LoggerFactory.getLogger(ThumbnailerServerHandler.class);
    private PipedInputStream toThumbnailer = new PipedInputStream();
    private PipedOutputStream fromClient = new PipedOutputStream(toThumbnailer);

    private static final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
            Executors.newFixedThreadPool(5));

    private ListenableFuture<OutputStream> future;

    public ThumbnailerServerHandler() throws IOException {
        super(ByteBuf.class, true);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        future = executor.submit(() -> ThumbnailGenerator.generate(toThumbnailer));
        future.addListener(() -> {
            try {
                ctx.writeAndFlush(future.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }, executor);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        this.fromClient.close();
        this.toThumbnailer.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        int readableBytes = msg.readableBytes();
        msg.readBytes(this.fromClient, readableBytes);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.error("Encountered error during communication", cause);
        ctx.close();
    }


}

这是我简化的“缩略图”,直到我完成整个流程:

public class ThumbnailGenerator {

    public static OutputStream generate(InputStream toThumbnailer) {
        OutputStream stream = new ByteArrayOutputStream();
        try {
            IOUtils.copy(toThumbnailer, stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stream;
    }

}
  1. 像这样在 handlerAdded 方法中剥离异步任务是否合适?有没有更“简洁”的方式来做到这一点?
  2. IOUtils.copy 应该并且会阻塞(由于对管道输入流的读取),直到有数据可供读取,这就是为什么我将其卸载到执行程序池中,因为我无法在处理程序中阻塞如果我想继续接收字节。但是,我发现这永远不会完成,但它确实取得了进展。那是因为我从未遇到过 EOF 字节(-1)吗?我怎样才能使这个流程发挥作用?
  3. 我是否缺少 netty 中可以简化此过程的构造?我曾考虑将它实现为一个解码器,在它拥有整个流之前不会解码,但随后我会将所有内容加载到内存中。

【问题讨论】:

    标签: java stream netty nio


    【解决方案1】:

    好的,事实证明我有一些误解可以解释为什么我无法正常工作。

    1) 许多文件类型没有所谓的终端字节。事实上,EOF 字节(最常见的是 -1,因为它是一个溢出值)通常是读者提供的一种实现,用于与他们的消费者沟通他们已经到达内容的末尾。它通常不存在于文件本身中。

    2) channelReadComplete 并不像听起来那么清晰。在达到 netty 中配置的最大读取次数(默认为 10)或有理由相信消息已完全发送时调用 channelReadComplete,如读取空缓冲区或接收到小于配置的块大小。

    至于为什么输入流副本看起来像挂起,那是因为管道输入流从未产生终端值(这是 EOF 字节的读取器实现的示例)。 PipedInputStreams 仅在驱动它们的输出流关闭后才指示 EOF。

    为了使这个实现工作,我应该将消息计数增加到足够高的数量,并在最后一次读取返回小于块大小的情况下调用受信任的 channelReadComplete。此时,关闭并重置下一条消息的输出流是安全的。关闭输出流将导致输入流最终返回该 EOF 字节,其他一切都可以继续进行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-09
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 2018-02-21
      • 2017-05-26
      • 2011-10-16
      相关资源
      最近更新 更多