现象
在一次CentOS 7系统中安装Tomcat,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是session引起的随机数问题导致的。Tocmat的Session ID是通过SHA1算法计算得到的,计算Session ID的时候必须有一个密钥。为了提高安全性Tomcat在启动的时候回通过随机生成一个密钥。
INFO: Starting service Catalina
Sep 30, 2017 9:29:55 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.81
Sep 30, 2017 9:29:55 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/ROOT
Sep 30, 2017 9:31:03 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [67,190] milliseconds.
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/ROOT has finished in 67,666 ms
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/docs
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/docs has finished in 61 ms
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/examples
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/examples has finished in 334 ms
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/host-manager
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/host-manager has finished in 63 ms
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/manager
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/manager has finished in 47 ms
Sep 30, 2017 9:31:03 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 30, 2017 9:31:03 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Sep 30, 2017 9:31:03 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 68241 ms
图片看的更清楚:
分析结果
主要原因是生成随机数的时候卡住了,导致tomcat启动不了。
解决方案
vim $JAVA_HOME/jre/lib/security/java.security
securerandom.source=file:/dev/urandom
改为
securerandom.source=file:/dev/./urandom
结果
重启tomcat,启动时间由68241ms—>956ms
INFO: Starting Servlet Engine: Apache Tomcat/7.0.81
Sep 30, 2017 9:47:22 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/ROOT
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/ROOT has finished in 460 ms
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/docs
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/docs has finished in 48 ms
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/examples
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/examples has finished in 292 ms
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/host-manager
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/host-manager has finished in 50 ms
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/manager
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /usr/tomcat/apache-tomcat-7.0.81/webapps/manager has finished in 46 ms
Sep 30, 2017 9:47:23 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Sep 30, 2017 9:47:23 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Sep 30, 2017 9:47:23 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 956 ms