字节跳动面试官:单机下如何让Java程序支持百万长连接?

再看客户端类代码,主要功能是循环依次往服务端开启的100个端口发起请求,直到服务端无响应、线程挂起为止,代码如下。

package com.tom.netty.connection;

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;

/**

  • Created by Tom.

*/

public class Client {

private static final String SERVER_HOST = “127.0.0.1”;

public static void main(String[] args) {

new Client().start(Server.BEGIN_PORT, Server.N_PORT);

}

public void start(final int beginPort, int nPort) {

System.out.println(“客户端已启动…”);

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() {

@Override

protected 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(new ChannelFutureListener() {

public void operationComplete(ChannelFuture future) throws Exception {

if (!future.isSuccess()) {

System.out.println(“连接失败,程序关闭!”);

System.exit(0);

}

}

});

channelFuture.get();

} catch (Exception e) {

}

if (port == nPort) { index = 0; }else { index ++; }

}

}

}

最后,将服务端程序打包发布到Linux服务器上,同样将客户端程序打包发布到另一台Linux服务器上。接下来分别启动服务端和客户端程序。运行一段时间之后,会发现服务端监听的连接数定格在一个值不再变化,如下所示。

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

当前客户端连接数: 870

并且抛出如下异常。

Exception in thread “nioEventLoopGroup-2-1” java.lang.InternalError: java.io.FileNotFoundException: /usr/java/jdk1.8.0_121/jre/lib/ext/cldrdata.jar (Too many open files)

at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:1040)

at sun.misc.URLClassPath.getResource(URLClassPath.java:239)

at java.net.URLClassLoader$1.run(URLClassLoader.java:365)

at java.net.URLClassLoader$1.run(URLClassLoader.java:362)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:361)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at java.lang.ClassLoader.loadClass(ClassLoader.java:411)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

at java.util.ResourceBundle$RBClassLoader.loadClass(ResourceBundle.java:503)

at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2640)

at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1501)

at java.util.ResourceBundle.findBundle(ResourceBundle.java:1465)

at java.util.ResourceBundle.findBundle(ResourceBundle.java:1419)

at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1361)

at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)

at java.util.logging.Level.computeLocalizedLevelName(Level.java:265)

at java.util.logging.Level.getLocalizedLevelName(Level.java:324)

at java.util.logging.SimpleFormatter.format(SimpleFormatter.java:165)

at java.util.logging.StreamHandler.publish(StreamHandler.java:211)

at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:116)

at java.util.logging.Logger.log(Logger.java:738)

at io.netty.util.internal.logging.JdkLogger.log(JdkLogger.java:606)

at io.netty.util.internal.logging.JdkLogger.warn(JdkLogger.java:482)

at io.netty.util.concurrent.SingleThreadEventExecutor$5.run (SingleThreadEventExecutor.java:876)

at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run (DefaultThreadFactory.java:144)

at java.lang.Thread.run(Thread.java:745)

这个时候,我们就应该要知道,这已经是服务器所能接受客户端连接数量的瓶颈值,也就是服务端最大支持870个连接。接下来要做的事情是想办法突破这个瓶颈,让单台服务器也能支持100万连接,这是一件多么激动人心的事情。

2 单机百万连接调优解决思路

==============

2.1 突破局部文件句柄限制

==============

首先在服务端输入命令,看一下单个进程所能支持的最大句柄数。

ulimit -n

输入命令后,会出现1 024的数字,表示Linux系统中一个进程能够打开的最大文件数,由于开启一个TCP连接就会在Linux系统中对应创建一个文件,所以就是受这个文件的最大文件数限制。那为什么前面演示的服务端连接数最终定格在870,比1 024小呢?其实是因为除了连接数,还有JVM打开的文件Class类也算作进程内打开的文件,所以,1 024减去JVM打开的文件数剩下的就是TCP所能支持的连接数。 接下来想办法突破这个限制,首先在服务器命令行输入以下命令,打开/etc/security/limits.conf文件。

sudo vi /etc/security/limits.conf

然后在这个文件末尾加上下面两行代码。

  • hard nofile 1000000

  • soft nofile 1000000

前面的*表示当前用户,hard和soft分别表示限制和警告限制,nofile表示最大的文件数标识,后面的数字1 000 000表示任何用户都能打开100万个文件,这也是操作系统所能支持的最大值,如下图所示。

这样调优之后,单机也能扛下100W连接

接下来,输入以下命令。

ulimit -n

这时候,我们发现还是1 024,没变,重启服务器。将服务端程序和客户端程序分别重新运行,这时候只需静静地观察连接数的变化,最终连接数停留在137 920,同时抛出了异常,如下所示。

当前客户端连接数: 137920

当前客户端连接数: 137920

当前客户端连接数: 137920

当前客户端连接数: 137920

当前客户端连接数: 137920

Exception in thread “nioEventLoopGroup-2-1” java.lang.InternalError: java.io.FileNotFoundException: /usr/java/jdk1.8.0_121/jre/lib/ext/cldrdata.jar (Too many open files)

这又是为什么呢?肯定还有地方限制了连接数,想要突破这个限制,就需要突破全局文件句柄数的限制。

2.2 突破全局文件句柄限制

==============

首先在Linux命令行输入以下命令,可以查看Linux系统所有用户进程所能打开的文件数。

cat /proc/sys/fs/file-max

通过上面这个命令可以看到全局的限制,发现得到的结果是10 000。可想而知,局部文件句柄数不能大于全局的文件句柄数。所以,必须将全局的文件句柄数限制调大,突破这个限制。首先切换为ROOT用户,不然没有权限。

sudo -s

echo 2000> /proc/sys/fs/file-max

exit

我们改成20 000来测试一下,继续试验。分别启动服务端程序和客户端程序,发现连接数已经超出了20 000的限制。 前面使用echo来配置/proc/sys/fs/file-max的话,重启服务器就会失效,还会变回原来的10 000,因此,直接用vi命令修改,输入以下命令行。

sodu vi /etc/sysctl.conf

在/etc/sysctl.conf文件末尾加上下面的内容。

fs.file-max=1000000

结果如下图所示。

这样调优之后,单机也能扛下100W连接

接下来重启 Linux服务器,再启动服务端程序和客户端程序。

当前客户端连接数: 9812451

当前客户端连接数: 9812462

当前客户端连接数: 9812489

当前客户端连接数: 9812501

当前客户端连接数: 9812503

最终连接数定格在 98万左右。我们发现主要受限于本机本身的性能。用htop命令查看一下,发现CPU都接近100%,如下图所示。

这样调优之后,单机也能扛下100W连接

以上是操作系统层面的调优和性能提升,下面主要介绍基于Netty应用层面的调优。

3 Netty应用级别的性能调优

================

3.1 Netty应用级别的性能瓶颈复现

====================

首先来看一下应用场景,下面是一段标准的服务端应用程序代码。

package com.tom.netty.thread;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.*;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioServerSocketChannel;

import io.netty.handler.codec.FixedLengthFrameDecoder;

/**

  • Created by Tom.

*/

public class Server {

private static final int port = 8000;

public static void main(String[] args) {

EventLoopGroup bossGroup = new NioEventLoopGroup();

EventLoopGroup workerGroup = new NioEventLoopGroup();

final EventLoopGroup businessGroup = new NioEventLoopGroup(1000);

ServerBootstrap bootstrap = new ServerBootstrap();

bootstrap.group(bossGroup, workerGroup)

.channel(NioServerSocketChannel.class)

.childOption(ChannelOption.SO_REUSEADDR, true);

bootstrap.childHandler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel ch) {

//自定义长度的解码,每次发送一个long类型的长度数据

//每次传递一个系统的时间戳

ch.pipeline().addLast(new FixedLengthFrameDecoder(Long.BYTES));

ch.pipeline().addLast(businessGroup, ServerHandler.INSTANCE);

}

});

ChannelFuture channelFuture = bootstrap.bind(port).addListener(new ChannelFutureListener() {

public void operationComplete(ChannelFuture channelFuture) throws Exception {

System.out.println("服务端启动成功,绑定端口为: " + port);

}

});

}

}

我们重点关注服务端的逻辑处理ServerHandler类。

package com.tom.netty.thread;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandler;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.ThreadLocalRandom;

/**

  • Created by Tom.

*/

@ChannelHandler.Sharable

public class ServerHandler extends SimpleChannelInboundHandler {

public static final ChannelHandler INSTANCE = new ServerHandler();

//channelread0是主线程

@Override

protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {

ByteBuf data = Unpooled.directBuffer();

//从客户端读一个时间戳

data.writeBytes(msg);

//模拟一次业务处理,有可能是数据库操作,也有可能是逻辑处理

Object result = getResult(data);

//重新写回给客户端

ctx.channel().writeAndFlush(result);

}

//模拟去数据库获取一个结果

protected Object getResult(ByteBuf data) {

int level = ThreadLocalRandom.current().nextInt(1, 1000);

//计算出每次响应需要的时间,用来作为QPS的参考数据

//90.0% == 1ms 1000 100 > 1ms

int time;

if (level <= 900) {

time = 1;

//95.0% == 10ms 1000 50 > 10ms

} else if (level <= 950) {

time = 10;

//99.0% == 100ms 1000 10 > 100ms

} else if (level <= 990) {

time = 100;

//99.9% == 1000ms 1000 1 > 1000ms

} else {

time = 1000;

}

try {

Thread.sleep(time);

} catch (InterruptedException e) {

}

return data;

}

}

上面代码中有一个getResult()方法。可以把getResult()方法看作是在数据库中查询数据的一个方法,把每次查询的结果返回给客户端。实际上,为了模拟查询数据性能,getResult()传入的参数是由客户端传过来的时间戳,最终返回的还是客户端传过来的值。只不过返回之前做了一次随机的线程休眠处理,以模拟真实的业务处理性能。如下表所示是模拟场景的性能参数。

数据处理的业务接口占比

处理所耗的时间

90%

1ms

95%

10ms

99%

100ms

99.9%

1000ms

下面来看客户端,也是一段标准的代码。

package com.tom.netty.thread;

import io.netty.bootstrap.Bootstrap;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelOption;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.SocketChannel;

import io.netty.channel.socket.nio.NioSocketChannel;

import io.netty.handler.codec.FixedLengthFrameDecoder;

/**

  • Created by Tom.

*/

public class Client {

private static final String SERVER_HOST = “127.0.0.1”;

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

new Client().start(8000);

}

public void start(int port) throws Exception {

EventLoopGroup eventLoopGroup = new NioEventLoopGroup();

final Bootstrap bootstrap = new Bootstrap();

bootstrap.group(eventLoopGroup)

.channel(NioSocketChannel.class)

.option(ChannelOption.SO_REUSEADDR, true)

.handler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel ch) {

ch.pipeline().addLast(new FixedLengthFrameDecoder(Long.BYTES));

ch.pipeline().addLast(ClientHandler.INSTANCE);

}

});

最后如何让自己一步步成为技术专家

说句实话,如果一个打工人不想提升自己,那便没有工作的意义,毕竟大家也没有到养老的年龄。

当你的技术在一步步贴近阿里p7水平的时候,毫无疑问你的薪资肯定会涨,同时你能学到更多更深的技术,交结到更厉害的大牛。

推荐一份Java架构之路必备的学习笔记,内容相当全面!!!

成年人的世界没有容易二字,前段时间刷抖音看到一个程序员连着加班两星期到半夜2点的视频。在这个行业若想要拿高薪除了提高硬实力别无他法。

你知道吗?现在有的应届生实习薪资都已经赶超开发5年的程序员了,实习薪资26K,30K,你没有紧迫感吗?做了这么多年还不如一个应届生,真的非常尴尬!

进了这个行业就不要把没时间学习当借口,这个行业就是要不断学习,不然就只能被裁员。所以,抓紧时间投资自己,多学点技术,眼前困难,往后轻松!

【关注】+【转发】+【点赞】支持我!创作不易!
p = new Bootstrap();

bootstrap.group(eventLoopGroup)

.channel(NioSocketChannel.class)

.option(ChannelOption.SO_REUSEADDR, true)

.handler(new ChannelInitializer() {

@Override

protected void initChannel(SocketChannel ch) {

ch.pipeline().addLast(new FixedLengthFrameDecoder(Long.BYTES));

ch.pipeline().addLast(ClientHandler.INSTANCE);

}

});

最后如何让自己一步步成为技术专家

说句实话,如果一个打工人不想提升自己,那便没有工作的意义,毕竟大家也没有到养老的年龄。

当你的技术在一步步贴近阿里p7水平的时候,毫无疑问你的薪资肯定会涨,同时你能学到更多更深的技术,交结到更厉害的大牛。

推荐一份Java架构之路必备的学习笔记,内容相当全面!!!

[外链图片转存中…(img-C2FrSk76-1720101831715)]

成年人的世界没有容易二字,前段时间刷抖音看到一个程序员连着加班两星期到半夜2点的视频。在这个行业若想要拿高薪除了提高硬实力别无他法。

你知道吗?现在有的应届生实习薪资都已经赶超开发5年的程序员了,实习薪资26K,30K,你没有紧迫感吗?做了这么多年还不如一个应届生,真的非常尴尬!

进了这个行业就不要把没时间学习当借口,这个行业就是要不断学习,不然就只能被裁员。所以,抓紧时间投资自己,多学点技术,眼前困难,往后轻松!

【关注】+【转发】+【点赞】支持我!创作不易!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值