java程序 启动慢_spring boot 程序启动缓慢的问题
今天發(fā)現(xiàn)一臺服務(wù)器上的springboot程序啟動特別慢,完全啟動起來用了有好幾分鐘。剛開始以為是代碼寫的有問題造成了卡死,直到看到這條log:
2017-03-08 10:06:49.600 INFO 6439 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8888 (http)
2017-03-08 10:06:49.613 INFO 6439 --- [main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-03-08 10:06:49.614 INFO 6439 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.37
......
2017-03-08 10:09:10.167 INFO 6439 --- [ost-startStop-1] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [140,108] milliseconds.
原來是tomcat的啟動耗費了140多秒。而罪魁禍首是這個SecureRandom類。
其實,這個問題我之前就有耳聞,但從沒遇到過,也就沒太在意。今天終于讓我遇上了,墨菲定律又生生應(yīng)驗了一回。。
Tomcat 7+ heavily relies on SecureRandom class to provide random values for its session ids and in other places. Depending on your JRE it can cause delays during startup if entropy source that is used to initialize SecureRandom is short of entropy. You will see warning in the logs when this happens, e.g.:
org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [5172] milliseconds.
意思是tomcat7以上的版本,在啟動時會調(diào)用SecureRandom類來生成隨機數(shù)。如果用于初始化SecureRandom的熵源是個短熵(熵不夠用),那么就會報文章開頭說的warning了。
jdk的配置文件中,使用securerandom.source設(shè)置了熵源:
cat /usr/java/jdk1.8.0_121/jre/lib/security/java.security
securerandom.source=file:/dev/random
可以看到默認值是:/dev/random。
所以程序啟動后SecureRandom類會讀取/dev/random以獲取隨機序列,這是一個同步操作。當(dāng)熵池(entropy pool) 中沒有足夠的熵時,讀取/dev/random就會造成阻塞,直到收集到了足夠的熵,程序才會繼續(xù)往下進行。
(關(guān)于什么是/dev/random,可以查看 wiki的介紹)
解決方法是修改成非阻塞的熵源/dev/urandom。
可以修改java.security文件中的securerandom.source值,也可以使用參數(shù)java.security.egd:
java -jar app.jar -Djava.security.egd=file:/dev/./urandom
至于為什么是/dev/./urandom,而不是/dev/urandom,這源于java的一個bug。大意是/dev/urandom在某些情況下可能還是最終會轉(zhuǎn)換成調(diào)用/dev/random。所以為了保險起見,還是使用/dev/./urandom吧!
總結(jié)
以上是生活随笔為你收集整理的java程序 启动慢_spring boot 程序启动缓慢的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PTA-6-1 数组循环右移 (20分)
- 下一篇: centos下php5.4.22连接ms