解决springboot war包在tomcat部署时启动慢的问题

web.jpg
>>>我的博客<<<

最近测试服务器tomcat发布一次很慢,为了解决这个问题,特地看了tomcat的log文件,启动日志在tomcat/logs/catalina.out文件中。

1、INFO:org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory xxx.war卡顿

刺风同学就是好记性不如烂笔头的实践者,被我也用上了。
找到jdk1.x.x_xx/jre/lib/security/Java.security文件,在文件中找到securerandom.source这个设置项,将其改为:

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

重启tomcat,这一步的速度明显加快。
原理据说是这样的:

linux或者部分unix系统提供随机数设备是/dev/random 和/dev/urandom ,
两个有区别,urandom安全性没有random高,但random需要时间间隔生成随机数。jdk默认调用random。

2、 WARN: Establishing SSL connection without server's identity verification is not recommended.

具体错误是这样的:

Mon Jul 09 09:30:13 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Jul 09 09:30:13 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Mon Jul 09 09:30:14 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

这个错误会打几遍,每一遍都会卡一会儿,应该是在验证mysql的SSL配置。
查阅资料说mysql的高版本默认要求使用SSL链接,所以在没有配置SSL也没有说SSL不需要时,就会一直验证,验证好几遍以后放弃。
这个时间大概有1分钟。
当然,方案就是在jdbc的链接上加上userSSL=false这个参数,
jdbc:p6spy:mysql://127.0.0.1:3306/chuangke?characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Hongkong&autoReconnect=true
类似这样。

3、 禁用Springboot的JNDI功能

原来的log

10:40:24.103 [localhost-startStop-1] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Replacing PropertySource 'servletContextInitParams' with 'servletContextInitParams'
10:40:24.685 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.exception-conversion-word]
10:40:24.686 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.exception-conversion-word] not found - trying original name [logging.exception-conversion-word]. javax.naming.NameNotFoundException: Name [logging.exception-conversion-word] is not bound in this Context. Unable to find [logging.exception-conversion-word].
10:40:24.687 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.exception-conversion-word]
10:40:24.687 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.exception-conversion-word] threw NamingException with message: Name [logging.exception-conversion-word] is not bound in this Context. Unable to find [logging.exception-conversion-word].. Returning null.
10:40:24.687 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.exception_conversion_word]
10:40:24.688 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.exception_conversion_word] not found - trying original name [logging.exception_conversion_word]. javax.naming.NameNotFoundException: Name [logging.exception_conversion_word] is not bound in this Context. Unable to find [logging.exception_conversion_word].
10:40:24.688 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.exception_conversion_word]
10:40:24.688 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.exception_conversion_word] threw NamingException with message: Name [logging.exception_conversion_word] is not bound in this Context. Unable to find [logging.exception_conversion_word].. Returning null.
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.exceptionConversionWord]
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.exceptionConversionWord] not found - trying original name [logging.exceptionConversionWord]. javax.naming.NameNotFoundException: Name [logging.exceptionConversionWord] is not bound in this Context. Unable to find [logging.exceptionConversionWord].
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.exceptionConversionWord]
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.exceptionConversionWord] threw NamingException with message: Name [logging.exceptionConversionWord] is not bound in this Context. Unable to find [logging.exceptionConversionWord].. Returning null.
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.exceptionconversionword]
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.exceptionconversionword] not found - trying original name [logging.exceptionconversionword]. javax.naming.NameNotFoundException: Name [logging.exceptionconversionword] is not bound in this Context. Unable to find [logging.exceptionconversionword].
10:40:24.689 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.exceptionconversionword]
10:40:24.690 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.exceptionconversionword] threw NamingException with message: Name [logging.exceptionconversionword] is not bound in this Context. Unable to find [logging.exceptionconversionword].. Returning null.
10:40:24.690 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.EXCEPTION-CONVERSION-WORD]
10:40:24.690 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.EXCEPTION-CONVERSION-WORD] not found - trying original name [logging.EXCEPTION-CONVERSION-WORD]. javax.naming.NameNotFoundException: Name [logging.EXCEPTION-CONVERSION-WORD] is not bound in this Context. Unable to find [logging.EXCEPTION-CONVERSION-WORD].
10:40:24.690 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.EXCEPTION-CONVERSION-WORD]
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.EXCEPTION-CONVERSION-WORD] threw NamingException with message: Name [logging.EXCEPTION-CONVERSION-WORD] is not bound in this Context. Unable to find [logging.EXCEPTION-CONVERSION-WORD].. Returning null.
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.EXCEPTION_CONVERSION_WORD]
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.EXCEPTION_CONVERSION_WORD] not found - trying original name [logging.EXCEPTION_CONVERSION_WORD]. javax.naming.NameNotFoundException: Name [logging.EXCEPTION_CONVERSION_WORD] is not bound in this Context. Unable to find [logging.EXCEPTION_CONVERSION_WORD].
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.EXCEPTION_CONVERSION_WORD]
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.EXCEPTION_CONVERSION_WORD] threw NamingException with message: Name [logging.EXCEPTION_CONVERSION_WORD] is not bound in this Context. Unable to find [logging.EXCEPTION_CONVERSION_WORD].. Returning null.
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging.EXCEPTIONCONVERSIONWORD]
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging.EXCEPTIONCONVERSIONWORD] not found - trying original name [logging.EXCEPTIONCONVERSIONWORD]. javax.naming.NameNotFoundException: Name [logging.EXCEPTIONCONVERSIONWORD] is not bound in this Context. Unable to find [logging.EXCEPTIONCONVERSIONWORD].
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging.EXCEPTIONCONVERSIONWORD]
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging.EXCEPTIONCONVERSIONWORD] threw NamingException with message: Name [logging.EXCEPTIONCONVERSIONWORD] is not bound in this Context. Unable to find [logging.EXCEPTIONCONVERSIONWORD].. Returning null.
10:40:24.691 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging_exception-conversion-word]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging_exception-conversion-word] not found - trying original name [logging_exception-conversion-word]. javax.naming.NameNotFoundException: Name [logging_exception-conversion-word] is not bound in this Context. Unable to find [logging_exception-conversion-word].
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging_exception-conversion-word]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging_exception-conversion-word] threw NamingException with message: Name [logging_exception-conversion-word] is not bound in this Context. Unable to find [logging_exception-conversion-word].. Returning null.
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging_exception_conversion_word]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging_exception_conversion_word] not found - trying original name [logging_exception_conversion_word]. javax.naming.NameNotFoundException: Name [logging_exception_conversion_word] is not bound in this Context. Unable to find [logging_exception_conversion_word].
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging_exception_conversion_word]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging_exception_conversion_word] threw NamingException with message: Name [logging_exception_conversion_word] is not bound in this Context. Unable to find [logging_exception_conversion_word].. Returning null.
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging_exceptionConversionWord]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging_exceptionConversionWord] not found - trying original name [logging_exceptionConversionWord]. javax.naming.NameNotFoundException: Name [logging_exceptionConversionWord] is not bound in this Context. Unable to find [logging_exceptionConversionWord].
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging_exceptionConversionWord]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiPropertySource - JNDI lookup for name [logging_exceptionConversionWord] threw NamingException with message: Name [logging_exceptionConversionWord] is not bound in this Context. Unable to find [logging_exceptionConversionWord].. Returning null.
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/logging_exceptionconversionword]
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/logging_exceptionconversionword] not found - trying original name [logging_exceptionconversionword]. javax.naming.NameNotFoundException: Name [logging_exceptionconversionword] is not bound in this Context. Unable to find [logging_exceptionconversionword].
10:40:24.692 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiTemplate - Looking up JNDI object with name [logging_exceptionconversionword]
           

参考配置教程修改。

摘要:在实际项目开发中使用springboot的时候,可以使用jar包的方式运行项目,也可以将springboot项目打成war包使用。springboot war包运行可能会出现

 [localhost-startStop-1] DEBUG org.springframework.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/LOGGING.pattern_level] not found - trying original name [LOGGING.pattern_level]. javax.naming.NameNotFoundException: Name [LOGGING.pattern_level] is not bound in this Context. Unable to find [LOGGING.pattern_level]。

反正就是诸如此类的问题吧。

上述的问题,在高版本的spring boot中会出现的,低版本不会出现这个问题。因为高版本中引入了JNDI查询的操作。

解决方案:

在项目的根目录中新建spring.properties配置文件,如下图所示:

image.png

添加属性以及值,如下所示:

spring.jndi.ignore=true

原理:

打开StandardServletEnvironment类,该类中的customizePropertySources方法如下:

public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
}
    super.customizePropertySources(propertySources);
}
JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()代码如下:
public static final String IGNORE_JNDI_PROPERTY_NAME = "spring.jndi.ignore";
private static final boolean shouldIgnoreDefaultJndiEnvironment =
SpringProperties.getFlag(IGNORE_JNDI_PROPERTY_NAME);
public static boolean isDefaultJndiEnvironmentAvailable() {
//如果忽略jndi则返回false
if (shouldIgnoreDefaultJndiEnvironment) {
return false;
}
    try {
                  //准备jndi环境
        new InitialContext().getEnvironment();
        return true;
}
catch (Throwable ex) {
        return false;
}
}

通过上文可知:

1、如果我们需要忽略jndi则可以配置spring.jndi.ignore值为true即可。

2、在哪里配置呢?我们不妨跟进SpringProperties类中的getFlag方法。

spring.jndi.ignore获取原理

SpringProperties类的getFlag方法如下所示:

public static boolean getFlag(String key) {
    return Boolean.parseBoolean(getProperty(key));
}

继续跟进getProperty方法,如下所示:

public static String getProperty(String key) {
       //获取spring.jndi.ignore值
String value = localProperties.getProperty(key);
if (value == null) {
    try {//获取系统的变量
    value = System.getProperty(key);
    }
        catch (Throwable ex) {
    }
    }
    return value;
}

上述的方法可以总结如下:

从localProperties集合中获取spring.jndi.ignore属性,如果没有获取到则直接从环境变量中进行获取。localProperties集合在哪里初始化的呢?我们看一下SpringProperties类中的静态代码块,如下所示:

private static final String PROPERTIES_RESOURCE_LOCATION = "spring.properties";
static {
try {
    ClassLoader cl = SpringProperties.class.getClassLoader();
    URL url = (cl != null ? cl.getResource(PROPERTIES_RESOURCE_LOCATION) :ClassLoader.getSystemResource(PROPERTIES_RESOURCE_LOCATION));
    if (url != null) {
        logger.info("Found 'spring.properties' file in local classpath");
        InputStream is = url.openStream();
        try {
            localProperties.load(is);
        }
        finally {
            is.close();
            }
    }
    }
catch (IOException ex) {
}
}
}

看到上面的代码,我们明白了,原来这里是直接读取跟目录中的spring.properties文件中的所有属性。看到这里,焕然大悟。原来如此。

  • 调试中,我修改了application.yml或者application-dev.yml配置文件,在文件中加入了spring.jndi.ignore=true均为生效,必须新建一个spring.properties,我没有验证spring.yml行不行。

重启tomcat容器,没有jndi的警告日志了。

注意:这种日志如果在本地以直接运行springboot的入口文件的方式启动,是没有的,只有当打成war包,放在tomcat里,启动tomcat/bin/startup.sh时才会有。

经过几步修改,tomcat启动速度明显提高。

部署慢修改原文:https://www.cnblogs.com/mycifeng/p/6972446.html
禁用jndi方式原文:https://blog.csdn.net/qq_30739519/article/details/78536965

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容