编译安装tomcat-native和tomcat-deamon以后,发现toomcat启动很慢,好久才有响应。以下日志供参考:

11-Sep-2017 12:19:28.102 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-9.0/webapps/ROOT]
11-Sep-2017 12:19:28.667 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Sep-2017 12:20:34.870 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [66,193] milliseconds.
11-Sep-2017 12:20:34.917 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-9.0/webapps/ROOT] has finished in [66,815] ms
11-Sep-2017 12:20:34.918 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-9.0/webapps/host-manager]
11-Sep-2017 12:20:35.004 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Sep-2017 12:20:35.013 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-9.0/webapps/host-manager] has finished in [96] ms
11-Sep-2017 12:20:35.014 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-9.0/webapps/manager]
11-Sep-2017 12:20:35.062 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Sep-2017 12:20:35.066 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-9.0/webapps/manager] has finished in [53] ms
11-Sep-2017 12:20:35.074 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
11-Sep-2017 12:20:35.105 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
11-Sep-2017 12:20:35.113 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 67067 ms

tomcat启动耗时67067ms折合67秒,刚刚安装的干净tomcat,显然不对劲。

有一条日志引起了我的注意:

11-Sep-2017 12:20:34.870 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [66,193] milliseconds.

显然tomcat执行到这里时出问题了,google了一下:"tomcat startup take long time SecureRandom",经过搜索,发现了一个解决方法

Slow startup on Tomcat 7.0.57 because of SecureRandom

 问题缘由:当没有足够的entropy反馈给/dev/random时调用securerandom的时候可能引起阻塞。

解决方法1:

修改 /opt/jdk1.8/jre/jre/lib/security/java.security

securerandom.source=file:/dev/./urandom

解决方法2:安装haveged

apt-get install haveged
vim /etc/default/haveged #编辑配置文件,输入下面一行
DAEMON_ARGS="-w 1024"

#刷新配置 update-rc.d haveged defaults

笔者采用了第二种方法,ubuntu16 amd64,64位系统,其他linux系统参考How to Setup Additional Entropy for Cloud Servers Using Haveged

同以上操作顺利解决问题。

更新后重启tomcat日志如下:

[/opt/apache-tomcat-9.0/webapps/ROOT]
11-Sep-2017 12:28:00.324 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Sep-2017 12:28:00.374 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-9.0/webapps/ROOT] has finished in [567] ms
11-Sep-2017 12:28:00.375 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-9.0/webapps/host-manager]
11-Sep-2017 12:28:00.448 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Sep-2017 12:28:00.456 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-9.0/webapps/host-manager] has finished in [80] ms
11-Sep-2017 12:28:00.456 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/opt/apache-tomcat-9.0/webapps/manager]
11-Sep-2017 12:28:00.502 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
11-Sep-2017 12:28:00.504 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/opt/apache-tomcat-9.0/webapps/manager] has finished in [48] ms
11-Sep-2017 12:28:00.514 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
11-Sep-2017 12:28:00.545 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
11-Sep-2017 12:28:00.565 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 803 ms

参考:

Tomcat takes too much time to start - Java SecureRandom

How to Setup Additional Entropy for Cloud Servers Using Haveged

Slow startup on Tomcat 7.0.57 because of SecureRandom

Tomcat 8熵池阻塞变慢详解

posted on 2017-09-11 13:01  你不知道的浪漫  阅读(6317)  评论(0编辑  收藏  举报