spring boot 程序启动缓慢的问题(二)
今天发现一台服务器上的springboot程序启动特别慢,完全启动起来用了有好几分钟。刚开始以为是代码写的有问题造成了卡死,直到看到这条log:
2017-03-08 10:06:49.600 INFO 6439 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8888 (http)
2017-03-08 10:06:49.613 INFO 6439 --- [main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-03-08 10:06:49.614 INFO 6439 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.37
......
2017-03-08 10:09:10.167 INFO 6439 --- [ost-startStop-1] o.a.c.util.SessionIdGeneratorBase : Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [140,108] milliseconds.
原来是tomcat的启动耗费了140多秒。而罪魁祸首是这个SecureRandom类。
其实,这个问题我之前就有耳闻,但从没遇到过,也就没太在意。今天终于让我遇上了,墨菲定律又生生应验了一回。。
tomcat的文档里有个概念叫Entropy Source(熵源)
Tomcat 7+ heavily relies on SecureRandom class to provide random values for its session ids and in other places. Depending on your JRE it can cause delays during startup if entropy source that is used to initialize SecureRandom is short of entropy. You will see warning in the logs when this happens, e.g.: <DATE> org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [5172] milliseconds.
意思是tomcat7以上的版本,在启动时会调用SecureRandom类来生成随机数。如果用于初始化SecureRandom的熵源是个短熵(熵不够用),那么就会报文章开头说的warning了。
jdk的配置文件中,使用securerandom.source设置了熵源:
cat /usr/java/jdk1.8.0_121/jre/lib/security/java.security securerandom.source=file:/dev/random
可以看到默认值是:/dev/random。
所以程序启动后SecureRandom类会读取/dev/random以获取随机序列,这是一个同步操作。当熵池(entropy pool) 中没有足够的熵时,读取/dev/random就会造成阻塞,直到收集到了足够的熵,程序才会继续往下进行。
(关于什么是/dev/random,可以查看 wiki的介绍)
解决方法是修改成非阻塞的熵源/dev/urandom。
可以修改java.security文件中的securerandom.source值,也可以使用参数java.security.egd:
java -jar app.jar -Djava.security.egd=file:/dev/./urandom
至于为什么是/dev/./urandom,而不是/dev/urandom,这源于java的一个bug。大意是/dev/urandom在某些情况下可能还是最终会转换成调用/dev/random。所以为了保险起见,还是使用/dev/./urandom吧!
spring boot 程序启动缓慢的问题(二)的更多相关文章
- 第一个Spring Boot程序启动报错了(番外篇)
Spring Boot内嵌了一个容器,我可以不用吗?我能不能用外部的容器呢? 当然是可以的! 然后,下面代码在pom文件中一定要有哦! <dependency> <groupId&g ...
- 第一个Spring Boot程序启动报错了
创建完成第一个Spring Boot项目后,准备运行,尝一下胜利的果实. 启动日志如下 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ...
- Spring Boot SpringApplication启动类(二)
目录 前言 1.起源 2.SpringApplication 运行阶段 2.1 SpringApplicationRunListeners 结构 2.1.1 SpringApplicationRunL ...
- 我的第一个spring boot程序(spring boot 学习笔记之二)
第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相 ...
- Spring Boot 入门之 Web 篇(二)
原文地址:Spring Boot 入门之 Web 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之基础篇(一)>介绍了 ...
- Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)
前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...
- 第一章 第一个spring boot程序(转载)
第一章 第一个spring boot程序 本编博客转发自:http://www.cnblogs.com/java-zhao/p/5324185.html 环境: jdk:1.8.0_73 mave ...
- spring boot无法启动,或者正常启动之后无法访问报404的解决办法
以前用spring boot都是用idea的自动创建,或者是用的Jhipster创建的,就没有深究怎么去搭建.但是今天晚上心血来潮,想自己搭一个demo来整合一些技术,于是就花一点时间来手动搭.因为今 ...
- spring boot容器启动详解
目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...
随机推荐
- 8.19 NOIP模拟测试26(B) 嚎叫响彻在贪婪的厂房+主仆见证了 Hobo 的离别+征途堆积出友情的永恒
T1 嚎叫响彻在贪婪的厂房 以前做过一个等比数列的题「序列」,这个类似 是等差数列且公差不为1的条件就是各项差的绝对值的$gcd!=1$,每次拿出序列前两个数,求出差值,插入到set里,每次向后扩展, ...
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- Kafka为什么速度那么快?
Kafka为什么速度那么快? Kafka的消息是保存或缓存在磁盘上的,一般认为在磁盘上读写数据是会降低性能的,因为寻址会比较消耗时间,但是实际上,Kafka的特性之一就是高吞吐率. 即使是普通的服务器 ...
- springcloud(五,多个服务注册中心eureka)
spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...
- HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
window.close(); System.Diagnostics.Process[] myProcesses; myProcesses = System.Diagnostics ...
- Ubuntu 16 PPA源管理(查询、添加、修改、删除)
查询 在Ubuntu中,每个PPA源是单独存放在/etc/apt/sources.list.d/文件夹中的,进入到该文件夹,使用ls命令查询即可列出当前系统添加的PPA源. 添加 sudo add-a ...
- 局域网Linux机器中病毒简单处理 .aliyun.sh 挖矿病毒 ---不彻底
1. 昨天晚上同事打电话给我说自己的服务器上面的redis无故被清空了,并且查看aof 日志有很多 wget和write指令 一想就是大事不好.局域网中病毒了.. 2. 今天早上到公司忙完一阵简单看了 ...
- Android 支持库迁移到AndroidX
一.背景 Android系统版本在不断更新,从最初的Android 1.0到现在Google和各大手机厂商正在推的Android 10,平均下来每个年头都有一个大的版本更新.但用户正在用的手机上的An ...