Reasons and solutions for the slow startup of Tomcat under CentOS7


Phenomenon

  When Tomcat is installed in a CentOS 7 system, the startup process is very slow and takes a few minutes. After checking the log, it is found that the time-consuming is here: it is caused by the random number problem caused by the session. Tocmat's Session ID is calculated by the SHA1 algorithm, and there must be a key when calculating the Session ID. To improve security, Tomcat generates a random key at startup. 

复制代码
20-Jul-2017 02:54:56.797 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /application/apache-tomcat-8.0.27/webapps/manager
20-Jul-2017 02:54:56.848 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /application/apache-tomcat-8.0.27/webapps/manager has finished in 51 ms
20-Jul-2017 02:54:56.864 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
20-Jul-2017 02:54:56.873 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
20-Jul-2017 02:54:56.874 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 34487 ms
复制代码

Analysis results

The main reason is that it gets stuck when generating random numbers, which makes tomcat unable to start.
Whether there is enough entropy to generate random numbers can be checked by the following command
[root@tomcat tools]# cat /proc/sys/kernel/random/entropy_avail
7
In order to speed up /dev/random providing random numbers, you By operating the peripherals of the device, it can generate a large number of interrupts (such as network transmission of data, keystrokes, moving the mouse, typing several different commands on the command line, commonly known as gas gathering.
cat /dev/random consumes energy.

After inspection, it was found that the reason was that there was no rng-tools package when installing the system at the minimum.

[root@tomcat ~]# rpm -qa rng-tools
[root@tomcat ~]#

solution

Option One

vim $JAVA_HOME/jre/lib/security/java.security
securerandom.source=file:/dev/random
change to
securerandom.source=file:/dev/urandom

Option II

vim $TOMCAT_HOME/bin/catalina.sh
if [[ "$JAVA_OPTS" != *-Djava.security.egd=* ]]; then
    JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom"
fi

  这个系统属性egd表示熵收集守护进程(entropy gathering daemon)。

方案三

yum install rng-tools # 安装rngd服务(熵服务,增大熵池)
systemctl start rngd  # 启动服务

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324635134&siteId=291194637