Tomcat 启动一直卡在 Root WebApplicationContext: initialization completed in


tomcat 启动一直卡在

Root WebApplicationContext: initialization completed in

重启了很多次,更换jdk版本,tomcat版本都不行。

tomcat所在的JVM进程已经被启动了所以可以排除是JVM退出引起的问题。那么问题真的就是JVM因为某种原因被阻塞了。

主要是因为阿里云的熵池太小 熵池的大小是根据键盘 鼠标之类的噪音产生的数 然后/dev/random会根据熵池来生成随机数 而生成需要有足够的熵池里的噪音数 如果没有达到的话就会一直阻塞 tomcat启动的时候为了生成session id就会获取这个随机数来生成密匙 所以才出现上面的情况 一直阻塞在等待熵池里的数满足生成随机数的大小 3分钟以后熵池里的数够大了才开始部署

yum install -y rng-tools
systemctl start rngd
cp /usr/lib/systemd/system/rngd.service /etc/systemd/system
vi /etc/systemd/system/rngd.service

ExecStart=/sbin/rngd -f -r /dev/urandom

WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [348,853] milliseconds

后来查了一下,发现这个问题抱怨的还是蛮多的,以至于tomcat的wiki里面还单独列出来作为加速启动的一个方面:

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.:

There is a way to configure JRE to use a non-blocking entropy source by setting the following system property: -Djava.security.egd=file:/dev/./urandom

尝试使用-Djava.security.egd=file:/dev/./urandom启动了一下,果然快了很多。

https://wiki.apache.org/tomcat/HowTo/FasterStartUp

Centos6

yum install rng-tools -y
cat /etc/sysconfig/rngd

# Add extra options here
EXTRAOPTIONS="-r /dev/urandom"

service rngd start
cat /proc/sys/kernel/random/entropy_avail