-Dio.netty.leakDetection.level JVM option is not working without spring-boot-starter jar dependency in spring application

huangapple 未分类评论46阅读模式
英文:

-Dio.netty.leakDetection.level JVM option is not working without spring-boot-starter jar dependency in spring application

问题

我有一个实现了Netty 4 HTTP服务器

public void start() {
    System.out.println("在Start方法中");
    try {
        ServerBootstrap b = new ServerBootstrap();
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .childHandler(new HttpServerPipelineFactory())
            .option(ChannelOption.SO_BACKLOG, 128)
            .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT)
            .childOption(ChannelOption.AUTO_READ, false)
            .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(listenerPort).sync();
        System.out.println("服务器已启动,监听端口:" + listenerPort);
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

HTTP管道工厂类如下

public class HttpServerPipelineFactory extends ChannelInitializer<Channel> {
    @Override
    protected void initChannel(Channel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("codec", new HttpServerCodec());
        pipeline.addLast("compress", new HttpContentCompressor());
        pipeline.addLast("decompress", new HttpContentDecompressor());
        pipeline.addLast("aggregator", new HttpObjectAggregator(512 * 1024));
        pipeline.addLast("chunked", new ChunkedWriteHandler());
        pipeline.addLast("flow", new FlowControlHandler());
        pipeline.addLast("keep-alive", new HttpServerKeepAliveHandler());
        pipeline.addLast("request", new AdvancedHTTPHandler());
    }
}

AdvancedHTTPHandler类如下

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static io.netty.util.CharsetUtil.UTF_8;

public class AdvancedHTTPHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    private static Logger logger = LoggerFactory.getLogger(HTTPRequestHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer("My Netty".getBytes()), false);
        response.headers().add(request.headers());
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        ctx.channel().writeAndFlush(response);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ctx.read();
    }
}

我想调试"-Dio.netty.leakDetection.level=PARANOID"并在这里将其设置为JVM参数如果我没有依赖"implementation("org.springframework.boot:spring-boot-starter:2.2.6.RELEASE")"它就不起作用

如果我有这个依赖"implementation("org.springframework.boot:spring-boot-starter:2.2.6.RELEASE")"并启动服务器我能够看到与资源泄漏相关的日志

如果我注释掉这个依赖并运行项目"gradle with-spring:runMain"则控制台日志中不会输出Netty资源泄漏日志

我的项目在GitHub上链接在这里

为什么我要添加一个不需要的依赖来使一个JVM参数生效呢有什么想法吗
英文:

I have a netty 4 http server Implemented.

public void start() {
    System.out.println(&quot;In Start method&quot;);
    try {
        ServerBootstrap b = new ServerBootstrap();
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        b.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new HttpServerPipelineFactory())
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT)
                .childOption(ChannelOption.AUTO_READ, false)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(listenerPort).sync();
        System.out.println(&quot;server started listening on port &quot; + listenerPort);
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

HTTP pipeline factory class is -

public class HttpServerPipelineFactory extends ChannelInitializer&lt;Channel&gt; {

    @Override
    protected void initChannel(Channel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(&quot;codec&quot;, new HttpServerCodec());
        pipeline.addLast(&quot;compress&quot;, new HttpContentCompressor());
        pipeline.addLast(&quot;decompress&quot;, new HttpContentDecompressor());
        pipeline.addLast(&quot;aggregator&quot;, new HttpObjectAggregator( 512 * 1024));
        pipeline.addLast(&quot;chunked&quot;, new ChunkedWriteHandler());
        pipeline.addLast(&quot;flow&quot;, new FlowControlHandler());
        pipeline.addLast(&quot;keep-alive&quot;, new HttpServerKeepAliveHandler());
        pipeline.addLast(&quot;request&quot;, new AdvancedHTTPHandler());
    }
}

AdvancedHTTPHandler is -

import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static io.netty.util.CharsetUtil.UTF_8;

public class AdvancedHTTPHandler extends SimpleChannelInboundHandler&lt;FullHttpRequest&gt; {

    private static Logger logger = LoggerFactory.getLogger(HTTPRequestHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.copiedBuffer(&quot;My Netty&quot;.getBytes()), false);
        response.headers().add(request.headers());
        response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
        ctx.channel().writeAndFlush(response);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        ctx.read();
    }
}

I want to debug the -Dio.netty.leakDetection.level=PARANOID and I set it as JVM argument here. It is not working if I don't have the dependency implementation(&quot;org.springframework.boot:spring-boot-starter:2.2.6.RELEASE&quot;).

If I have this dependency implementation(&quot;org.springframework.boot:spring-boot-starter:2.2.6.RELEASE&quot;) and start the server, I'm able to see resource leak related logs -

-Dio.netty.leakDetection.level JVM option is not working without spring-boot-starter jar dependency in spring application

If I comment this dependency and the run the project gradle with-spring:runMain then the netty resource leak logs is not output in the console log.

My project is on github here.

Why should I add a dependency that is not needed to make a JVM arg work. Any idea?

huangapple
  • 本文由 发表于 2020年4月10日 11:29:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61133602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定