Tomcat启动慢的原因及解决方法

Tomcat启动慢的原因及解决方法

 

在CentOS启动Tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是session引起的随机数问题导致的。Tocmat的Session ID是通过SHA1算法计算得到的,计算Session ID的时候必须有一个密钥。为了提高安全性Tomcat在启动的时候回通过随机生成一个密钥。

 

1.具体报错如下:

22-Apr-2019 19:33:07.623 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of
 SecureRandom instance for session ID generation using [SHA1PRNG] took [55,507] milliseconds.
22-Apr-2019 19:33:07.653 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web appli
cation directory /home/apache-tomcat-8.0.27/webapps/ROOT has finished in 55,935 ms

 

主要原因是生成随机数的时候卡住了,导致tomcat启动不了。

是否有足够的熵来用于产生随机数,可以通过如下命令来查看

[root@pre1 tools]# cat /proc/sys/kernel/random/entropy_avail
20

为了加速/dev/random提供随机数的速度,你可以通过操作设备的外设,让其产生大量的中断(如网络传输数据,按键,移动鼠标,在命令行敲几个不同的命令,俗称聚气。

 

2.解决方法一

vim $JAVA_HOME/jre/lib/security/java.security

securerandom.source=file:/dev/random

改为

securerandom.source=file:/dev/urandom

 

3.解决方法二

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)

 

4.解决方法三

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

systemctl start rngd  # 启动服务

查看启动状态

[root@pre1 ~]# systemctl status rngd
● rngd.service - Hardware RNG Entropy Gatherer Daemon
   Loaded: loaded (/usr/lib/systemd/system/rngd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-05-15 10:15:54 CST; 4s ago
 Main PID: 1540 (rngd)
   CGroup: /system.slice/rngd.service
           └─1540 /sbin/rngd -f

May 15 10:15:54 RAP systemd[1]: Started Hardware RNG Entropy Gatherer Daemon.
May 15 10:15:54 RAP systemd[1]: Starting Hardware RNG Entropy Gatherer Daemon...
May 15 10:15:54 RAP rngd[1540]: Initalizing available sources
May 15 10:15:54 RAP rngd[1540]: Failed to init entropy source 0: Hardware RNG Device
May 15 10:15:54 RAP rngd[1540]: Failed to init entropy source 2: Intel RDRAND Instruction RNG
May 15 10:15:56 RAP rngd[1540]: Enabling JITTER rng support

 

posted @ 2019-05-15 17:37  何宇泽  阅读(5699)  评论(0编辑  收藏  举报