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

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

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

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

public class JRand {dom

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

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

執行測試:
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。.net

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

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

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優化啓動

相關文章
相關標籤/搜索