netty依赖精简

首页 / 新闻资讯 / 正文

通常情况下为了方便我们在使用netty时会使用以下的maven依赖声明

<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency>     <groupId>io.netty</groupId>     <artifactId>netty-all</artifactId>     <version>4.1.66.Final</version> </dependency>

这样引入的jar包netty-all(约4M)体积是比较大的
netty-all提供了丰富的功能,如流量整形,native传输,各种应用层协议的codec,在开发过程中有时可能不需要用到全部的高级特性,只需要最基本的NIO传输功能,这时只需要引入依赖

        <dependency>             <groupId>io.netty</groupId>             <artifactId>netty-transport</artifactId>             <version>4.1.66.Final</version>         </dependency>

此依赖会通过传递依赖的形式自动引入
netty-buffer,netty-common,netty-resolver,netty-transport这四个库(合约1.39M),这样就比直接引入整个依赖在小很多。对于服务器应用来说jar的体积对内存占用影响不显著,但对于资源受限的场景,如移动端,精简依赖还是很有必要的。