1. 程式人生 > >tomcat 啟動慢的問題排查

tomcat 啟動慢的問題排查

tomcat 啟動慢的問題排查

現象

最近新增了一批伺服器,運維反饋說,部署的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