tomcat 启动慢的问题排查

版权声明:本博客所有文章由leoss原创,转载请注明出处 https://blog.csdn.net/qq_14869093/article/details/85766333

现象

最近新增了一批服务器,运维反馈说,部署的tomcat启动很慢,但是没有报任何异常,启动后应用也能正常运行。正常启动应用10多秒就完成了,但是在新服务器上却长达2分多钟。

问题排查

通过观察启动过程,我们发现启动时在某个地方停顿了很久,并且某条输出日志里有个 时间 ”[142,445] milliseconds“,算起刚好是2分多钟。而过了这条日志,程序就很快的启动完了。问题应该是和这个日志有关。
日志如下:

03-Jan-2019 15:52:53.587 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [142,445] milliseconds.

原因

Tomcat 7/8 都使用org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom 类产生安全随机类SecureRandom的实例作为会话ID。

当使用SecureRandom 的时候默认使用的/dev/random 来生成随机种子,如果没有足够的种子数据,会一直等待,导致jvm卡住,耗费比较长的时间。(这些数据是由键盘,鼠标,磁盘活动等产生的,如果没有这些活动,就没有足够的数据)。而我们刚好是新服务器,所有出现了这个问题。

扩展

/dev/random与/dev/urandom

/dev/random和/dev/urandom是unix系统下的随机函数生成器,很多应用都需要使用random提供的随机数,比如ssh keys, SSL keys, TCP/IP sequence numbers等等。他们的区别在于:

  1. random是阻塞的,urandom是非阻塞的,
  2. urandom生成的随机值没有random随机。
  3. random非常适合那些需要非常高质量随机性的场景,比如一次性的支付或生成密钥的场景。而对随机要求不是非常绝对的情况,则可以使用urandom。

random是阻塞,random会根据收集的环境噪音(熵池)产生随机数,如果环境噪音不够它就会阻塞。

环境噪声

Linux上任何I/O,键盘终端、内存、CPU等设备的使用都会产生环境噪声,并被收集,作为熵池。

SecureRandom

在$JAVA_PATH/jre/lib/security/java.security中有一段注释

#
# Sun Provider SecureRandom seed source.
#
# Select the primary source of seed data for the "SHA1PRNG" and
# "NativePRNG" SecureRandom implementations in the "Sun" provider.
# (Other SecureRandom implementations might also use this property.)
#
# On Unix-like systems (for example, Solaris/Linux/MacOS), the
# "NativePRNG" and "SHA1PRNG" implementations obtains seed data from
# special device files such as file:/dev/random.
#
# On Windows systems, specifying the URLs "file:/dev/random" or
# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding
# mechanism for SHA1PRNG.
#
# By default, an attempt is made to use the entropy gathering device
# specified by the "securerandom.source" Security property.  If an
# exception occurs while accessing the specified URL:
#
#     SHA1PRNG:
#         the traditional system/thread activity algorithm will be used.
#
#     NativePRNG:
#         a default value of /dev/random will be used.  If neither
#         are available, the implementation will be disabled.
#         "file" is the only currently supported protocol type.
#
# The entropy gathering device can also be specified with the System
# property "java.security.egd". For example:
#
#   % java -Djava.security.egd=file:/dev/random MainClass
#
# Specifying this System property will override the
# "securerandom.source" Security property.
#
# In addition, if "file:/dev/random" or "file:/dev/urandom" is
# specified, the "NativePRNG" implementation will be more preferred than
# SHA1PRNG in the Sun provider.
#
securerandom.source=file:/dev/random

简单的解释下: java 的SecureRandom 使用 SHA1PRNG或NativePRNG算法 生成随机数。在类unix系统上它们是基于某种特殊设备块( 熵收集设备),如前面提到的/dev/random(默认就是这个)来生成的随机数。
通过java.security.egd或者securerandom.source可以指定使用的熵收集设备,配置java.security.egd会覆盖securerandom.source。

解决方案

问题的根本原因就是 /dev/random的熵池 不足导致阻塞,那么只需改成非阻塞的/dev/urandom,或者增大熵池即可。

方案1:设置Tomcat启动参数

在tomcat启动配置文件($tomcat/bin/catalina.sh)里添加一个JAVA_OPTS参数,如果有别的JAVA_OPTS参数注意下兼容。

JAVA_OPTS="-Djava.security.egd=file:/dev/urandom"

方案2:设置JVM环境(全局) 推荐

打开$JAVA_PATH/jre/lib/security/java.security这个文件,找到下面的内容,并替换:
这样做的好处是全局生效,如果部署了别的tomcat也不会再发生相同问题。

securerandom.source=file:/dev/random

替换成

securerandom.source=file:/dev/urandom

方案3:增大/dev/random的熵池

问题的根本原因的熵池不够,那么使用某种办法增大熵池就行,参考:彻底解决问题
因为/dev/random 是系统的,不只是tomcat用到了,像linux本身,nignx,ssl等很多应用都用到,所以这个方案应该是最好的解决方式。不过我们的服务器仅做tomcat服务,所以就没去参试了。

参考:
https://blog.csdn.net/chszs/article/details/49494701
https://blog.csdn.net/sxhong/article/details/62889003?locationNum=4&fps=1
http://www.th7.cn/Program/java/201507/497656.shtml
https://www.tuicool.com/articles/uaiURzF

猜你喜欢

转载自blog.csdn.net/qq_14869093/article/details/85766333
今日推荐