Java隨機數坑 Oracle Connection reset 讓tomcat啓動更快

問題背景:
①程序莫名其妙: Caused by: java.net.SocketException: Connection reset
②Tomcat 啓動慢

bug來源:
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6202721

測試示例:
https://blog.csdn.net/simplemurrina/article/details/79479183
import java.security.SecureRandom;

public class JRand {

  public static void main(String args[]) throws Exception {

System.out.println("Ok: " +SecureRandom.getInstance("SHA1PRNG").nextLong());
}
}

執行測試:
time java JRand
time java -Djava.security.egd=file:/dev/urandom JRand
time java -Djava.security.egd=file:/dev/./urandom JRand

解決辦法
1.在Tomcat環境中解決

可以通過配置JRE使用非阻塞的Entropy Source。

在catalina.sh中加入這麼一行:-Djava.security.egd=file:/dev/./urandom 即可。

加入後再啓動Tomcat,整個啓動耗時下降到Server startup in 6213 ms,大大降低了啓動的時間。

2.在JVM環境中解決

先執行which javac命令檢查jdk安裝路徑

/usr/local/java/jdk1.8.0_92/bin/javac

去到$JAVA_PATH/jre/lib/security/java.security這個文件,找到下面的內容:

securerandom.source=file:/dev/urandom

替換成

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

參考:
https://blog.csdn.net/weixin_41350766/article/details/80063016

https://www.cnblogs.com/pthwang/p/8949445.html

https://blog.csdn.net/simplemurrina/article/details/79479183

https://blog.csdn.net/renfufei/article/details/70878077 tomcat優化啓動

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章