一、测试目的
很多中间件都用netty做通讯,如zookeeper\dubbo\rocketMq等等。
我们自己线上也在用netty,但我们目前连接数还不算很大,峰值连接也仅在1万左右。
今天我们来测试一下netty在单机百万连接情况下服务器cpu、内存、网络等使用情况。
二、服务器配置以及设置
1.测试机器配置
两台4核16G8M带宽的华为云服务器。
服务器:华为云,centos 8.2,4核16G,部署运行netty-server.jar
客户端:华为云,centos 8.2,4核16G,部署运行netty-client.jar
jdk:1.8
netty:4.1.67.Final
spring-boot:2.3.9.RELEASE
2.查看linux一个进程能够打开的最大文件数连接
ulimit -n
默认如下:
可以通过 ulimit -n 1000000进行临时修改,
注意这个数值不宜过高,我在测试过程中设置2000000时会连接不上系统了,
这个值建议可以设置为1000000进行测试。
也通过vi /etc/security/limits.conf
修改为1000000永久配置,修改如下:
3.查看全局文件句柄限制
cat /proc/sys/fs/file-max
默认如下:8.2默认配置就很高了,可以不修改
ps:centos8.2的默认配置就已经很高了,这里就不改了。
如果你机器默认值过低的话要进行以下修改
echo 1000000> /proc/sys/fs/file-max这个在重启后会失效,
下面这个操作就会永久生效:
vi /etc/sysctl.conf
我们在文件末尾加上
fs.file-max=1000000
注意,以上配置服务器和客户端都要修改才行,修改完后重启服务器和客户端。
重启后分别查看服务器和客户端的配置是否生效了:
三、编写服务端代码
1.服务端口绑定
- 在开始端口绑定之前先普及一下很多人对tcp端口的几种常见误解:
- 服务器上一个端口仅会建立一个连接,服务器上最大的连接数量范围是0-65535。
linux内核对tcp连接的识别是通过四元组(源ip,源port,目标ip,目标port)来区分的。只要四元组中任意一个不同就被认为是完全不同的连接了。
所以服务器上同一个端口上只要目标ip,目标port(即客户端ip、客户端端口)不同就会被认为是完全不同的连接了,
所以服务器上同一个端口上是能建立无数个tcp连接的,只要设置系统能打开的文件数量足够大即可,而能打开的文件数量由内存来决定,
所以理论上只要内存足够大,那么服务器上仅一个端口上的连接数量就可以是无数个了,就更没服务器上最大的连接数量范围是0-65535这么一说了。
- 服务器上在接受到客户端的连接后会占用一个新的端口跟客户端保持连接
服务器上不会为客户端的连接开一个新的端口进行通讯的,比如web服务的80端口上,所有的客户端连接都是用的80。
根据四元组可知,只要目标ip、目标port任意一个不相同,那么80端口上可以接受无数连接的。
同理你自己程序所绑定监听的端口同样也仅是在你所绑定的端口上做通讯的,同样不会为客户端连接新开一个服务端口去进行通讯的。
- 客户端connect建立成功后端口会占用不可重用或者客户端能产生的最大连接数量范围是0-65535。
在客户端的同一个端口上,只要源ip、源port不相同(即服务器ip、服务器端口),那么客户端上同一个端口上的这些连接就会被认为是不相同的连接了,
所以客户端同一个端口上同样是可以连接无数个不同的服务器的,同样最终的上限还是由机器内存本身来决定。
- 下面正式开始本测试的测试方法:
启动一个服务端绑定8000端口。
当我们服务器端在接受到一个客户端的连接之后,服务器端此时并不会新占用一个服务端口来保持连接,而是依然会用8000端口跟客户端进行保持通讯。
当客户端在成功连接到服务器后会用一个随机的客户端端口来跟服务器保持连接,如果此时我们仅用一台客户端去连接服务器的一个8000端口的话,根据四元组可知这一台客户端此时最多只能有65536个连接。
如果按这样的话我们大概需要16台客户端去连接8000端口的服务器才能测试得了100万的连接,这样操作上就很麻烦了,而且我们实际上也没有这么多的客户端去做测试。
所以为了能在只有一台服务器、一台客户端的情况下去做百万连接测试,我们可以根据上面所说的四元组对服务器端、客户端做一些巧妙的设计即可实现,具体方法如下:
- 一台服务端从8000端口开始监听往后的100个端口,也就是用一台服务器提供100个监听端口给客户端,按100*65536,理论上一台客户端能产生650万个连接了。
一台客户端从8000端口开始往后进行连接。根据四元组可知客户端此时同一个端口可同时连接多个同ip但不同端口的服务,比如客户端的62102端口上可同时连接服务端的8000~8100端口。
此时客户端便可用while一直不停的去连接服务器上的那100个不同的端口直到客户端产生超过100万连接后抛出异常。<br />具体的实现代码如下:
2.服务端代码实现
pom.xml
<dependencies><dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.67.Final</version></dependency></dependencies><!--打包jar--><build><finalName>netty-server</finalName><plugins><!--spring-boot-maven-plugin--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.9.RELEASE</version><!--解决打包出来的jar文件中没有主清单属性问题--><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><!--指定maven-compiler版本和<source>编译版本--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build>
服务端 ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel;
public final class Server { final static int BEGIN_PORT = 8000; final static int N_PORT = 100; public static void main(String[] args) { new Server().start(BEGIN_PORT, N_PORT); } public void start(int beginPort, int nPort) { System.out.println(“server starting….”); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup); bootstrap.channel(NioServerSocketChannel.class); bootstrap.childOption(ChannelOption.SO_REUSEADDR, true); bootstrap.childHandler(new ConnectionCountHandler()); for (int i = 0; i < nPort; i++) { int port = beginPort + i; bootstrap.bind(port).addListener((ChannelFutureListener) future -> { System.out.println(“bind success in port: “ + port); }); } System.out.println(“server started!”); } }
简单的连接数统计Hanlder```javaimport io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;@ChannelHandler.Sharablepublic class ConnectionCountHandler extends ChannelInboundHandlerAdapter {private AtomicInteger nConnection = new AtomicInteger();public ConnectionCountHandler() {//间隔2秒打印当前服务端的连接总数量Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {System.out.println("connections: " + nConnection.get());}, 0, 2, TimeUnit.SECONDS);}@Overridepublic void channelActive(ChannelHandlerContext ctx) {//当有新的连接进来触发计算器自增1nConnection.incrementAndGet();}@Overridepublic void channelInactive(ChannelHandlerContext ctx) {//当有连接断开了触发计算器自减1nConnection.decrementAndGet();}}
3.编写启动脚本
我机器是16G的内存,机器上啥都没安装,16G能分15个G给JVM,所以选择了G1。
如果你的机器能配置的JVM内存小于8G的话,那建议用CMS。
#!/bin/bashJAVA_OPTS="-server-Xmx13G-Xms13G-Xmn10G-XX:MetaspaceSize=256M-XX:+UseG1GC-XX:MaxGCPauseMillis=50-XX:G1HeapRegionSize=16M"java $JAVA_OPTS -jar netty-server.jar
我这里做了3个G预留,给JVM配置了13个G的堆内存和新生代10个G
服务器启动脚本:sh start.sh,程序启动打印启动成功结果,等待客户端连接。
然后再启动客户端脚本:sh start.sh。
四、编写客户端代码
1.客户端代码实现
import io.netty.bootstrap.Bootstrap;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;public class Client {private static final String SERVER_HOST = "192.168.0.94";final static int BEGIN_PORT = 8000;final static int N_PORT = 100;public static void main(String[] args) {new Client().start(BEGIN_PORT, N_PORT);}public void start(final int beginPort, int nPort) {System.out.println("client starting....");EventLoopGroup eventLoopGroup = new NioEventLoopGroup();final Bootstrap bootstrap = new Bootstrap();bootstrap.group(eventLoopGroup);bootstrap.channel(NioSocketChannel.class);bootstrap.option(ChannelOption.SO_REUSEADDR, true);bootstrap.handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) {}});int index = 0;int port;while (!Thread.interrupted()) {port = beginPort + index;try {ChannelFuture channelFuture = bootstrap.connect(SERVER_HOST, port);channelFuture.addListener((ChannelFutureListener) future -> {if (!future.isSuccess()) {System.out.println("connect failed, exit!");}else{System.out.println("serverAddress:"+ channelFuture.channel().remoteAddress().toString() +",localAddress:"+channelFuture.channel().localAddress().toString());}});channelFuture.get();} catch (Exception e) {}if (++index == nPort) {index = 0;}}}}
五、测试结果
一、100万连接测试结果
1、0个连接时系统CPU和内存情况
2、10万连接时系统CPU和内存情况
3、50万连接时系统CPU和内存情况
4、80万连接时系统CPU和内存情况
5、100万连接时系统CPU和内存情况
程序实际上没有达到100万连接,在达到99万多接近100万连接的时候程序就抛出打开文件数过多的异常了
下图是我在华为云服务器控制面板看到的网络瞬间的峰值情况
从下图可以看出当接近百万连接时,cpu的使用率还是比较低的,而内存也仅是使用了5.78个G(包含了系统)。
堆使用情况与GC日志情况,一次GC都没产生。
二、测试总结
1.测试结果有点出乎意料的,没想到在普通的4核心16G机器便能轻松达到百万级别的长连接。
整体来看netty百万连接对cpu没太大的要求,比较的吃内存,毕竟打开了上百万个文件。
2.当然,上面也仅是做了连接数的测试,线上真实的情况下还要考虑数据传输带来的网络压力。

