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


linux上的tomcat上部署上springboot的war包启动总是很慢,分析启动log,三步调整,使启动时间从10分钟调整到3分钟。
简书地址

最近测试服务器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。

具体错误是这样的:

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配置文件,如下图所示:

spring.properties

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

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


评论
  目录