Raspberry PI 上的 Spring Boot 启动缓慢



将Spring Boot 2用于物联网应用程序时,我注意到Spring的启动时间非常慢。该平台是Raspberry PI 2B - 当然,这将比PC慢得多。对于正常的代码执行,我测量了 20 倍到 50 倍的差异。

  • 我的 PC 上的 Spring 启动启动时间 (Win10 x64):5 秒
  • PI(Ubuntu Server,docker 中的 ARM java 运行 jar)上的 Spring 启动启动时间:11 分钟

如果我使用我测量过的最高因子(50x),我希望看到启动时间不到当前状态的一半。到目前为止,我尝试了:

  • 从自动配置中排除依赖项(现在最小,只有 spring-boot-starter-web、kotlin、undertow、webflux、reactor 和 kafka)
  • 登录 DEBUG 以查看安静期间发生了什么(主要是设置 bean,没有什么可疑的,尽管有些 bean 需要几秒钟才能加载)
  • ARM上Java的不同docker镜像(无效果)
  • CPU 始终处于 100%,内存约为 20%,并且有足够的磁盘空间

我的初步结论是,加载 bean 会消耗所有 CPU 周期。我想初始化一个 bean 应该不需要几秒钟,但它确实需要。这里的瓶颈可能是什么?我可以让 Spring 以任何方式更快地加载吗?


这是PC输出的第一部分:

2018-01-06 13:43:03.462  INFO 9144 --- [           main] c.e.b.BasestationApplicationKt           : Starting BasestationApplicationKt on GPC with PID 9144 (C:DataCodeappgitbasestationoutproductionclasses started by User in C:DataCodeappgit)
2018-01-06 13:43:03.471  INFO 9144 --- [           main] c.e.b.BasestationApplicationKt           : The following profiles are active: dev
2018-01-06 13:43:03.637  INFO 9144 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@294e5088: startup date [Sat Jan 06 13:43:03 CET 2018]; root of context hierarchy
2018-01-06 13:43:05.578  INFO 9144 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2018-01-06 13:43:05.600  INFO 9144 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2018-01-06 13:43:05.695  WARN 9144 --- [           main] io.undertow.websockets.jsr               : UT026009: XNIO worker was not set on WebSocketDeploymentInfo, the default worker will be used
2018-01-06 13:43:05.695  WARN 9144 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2018-01-06 13:43:05.721  INFO 9144 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2018-01-06 13:43:05.722  INFO 9144 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2088 ms
2018-01-06 13:43:05.851  INFO 9144 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-06 13:43:05.857  INFO 9144 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-06 13:43:07.323  INFO 9144 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public com.app.basestation.model.Message com.app.basestation.BasestationController.home()
2018-01-06 13:43:07.330  INFO 9144 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-01-06 13:43:07.349  INFO 9144 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-01-06 13:43:07.509  INFO 9144 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@294e5088: startup date [Sat Jan 06 13:43:03 CET 2018]; root of context hierarchy
2018-01-06 13:43:08.519  INFO 9144 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-06 13:43:08.553  INFO 9144 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2018-01-06 13:43:08.678  INFO 9144 --- [           main] o.s.b.w.e.u.UndertowServletWebServer     : Undertow started on port(s) 11112 (http)

以及 PI 的输出:

2018-01-06 12:48:41.689  INFO 1 --- [           main] c.e.b.BasestationApplicationKt           : Starting BasestationApplicationKt on ubuntu with PID 1 (/app.jar started by root in /)
2018-01-06 12:48:42.019  INFO 1 --- [           main] c.e.b.BasestationApplicationKt           : The following profiles are active: prd
2018-01-06 12:48:49.827  INFO 1 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@128c152: startup date [Sat Jan 06 12:48:49 UTC 2018]; root of context hierarchy
2018-01-06 12:54:05.276  INFO 1 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
2018-01-06 12:54:08.404  INFO 1 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
2018-01-06 12:54:15.847  WARN 1 --- [           main] io.undertow.websockets.jsr               : UT026009: XNIO worker was not set on WebSocketDeploymentInfo, the default worker will be used
2018-01-06 12:54:15.852  WARN 1 --- [           main] io.undertow.websockets.jsr               : UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2018-01-06 12:54:19.930  INFO 1 --- [           main] io.undertow.servlet                      : Initializing Spring embedded WebApplicationContext
2018-01-06 12:54:19.934  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 330155 ms
2018-01-06 12:54:42.544  INFO 1 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-06 12:54:43.206  INFO 1 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-06 12:57:18.683  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public com.app.basestation.model.Message com.app.basestation.BasestationController.home()
2018-01-06 12:57:19.734  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-01-06 12:57:19.758  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-01-06 12:57:44.597  INFO 1 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@128c152: startup date [Sat Jan 06 12:48:49 UTC 2018]; root of context hierarchy
2018-01-06 12:59:36.677  INFO 1 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-06 12:59:37.807  INFO 1 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2018-01-06 12:59:42.664  INFO 1 --- [           main] o.s.b.w.e.u.UndertowServletWebServer     : Undertow started on port(s) 11112 (http)

更新:

对于那些阅读本文的人,由于新的Oracle政策,我在2019年搬回了OpenJDK。我注意到的是,OpenJDK最近在性能上取得了显著的进步。如果您使用 OpenJDK 11 及更高版本,它的性能与 Oracle JDK 相似,并且您可以轻松地配置其语言级别以使用 Java 8 代码。也就是说,如果这对您不起作用,请尝试以下操作。


我搬到了一个新的映像:Oracle JDK,而不是@snodnipper建议的OpenJDK。

我必须做几件事,包括安装比标准存储库中可用的版本更新的 Java 8。这是最终为我工作的 Dockerfile,现在应用程序在 2 分钟内启动。

FROM resin/raspberrypi3-buildpack-deps:jessie-scm
ENV LANG C.UTF-8
ENV JAVA_HOME /usr/lib/jvm/java-8-oracle
RUN ["cross-build-start"]
RUN echo "deb http://archive.raspberrypi.org/debian/ jessie main ui staging" > /etc/apt/sources.list.d/raspi.list
RUN rm -f /usr/bin/entry.sh
RUN wget -qO - http://archive.raspberrypi.org/debian/raspberrypi.gpg.key | apt-key add -
RUN { 
echo '#!/bin/bash'; 
echo 'set -e'; 
echo; 
echo 'dirname "$(dirname "$(readlink -f "$(which javac || which java)")")"'; 
} > /usr/local/bin/docker-java-home && 
chmod +x /usr/local/bin/docker-java-home
RUN apt-key adv --recv-key --keyserver keyserver.ubuntu.com C2518248EEA14886 && 
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" >> /etc/apt/sources.list.d/raspi.list
RUN set -x && 
apt-get update && 
apt-cache madison oracle-java8-installer && 
echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections && 
apt-get install -y oracle-java8-installer oracle-java8-set-default && 
rm -rf /var/lib/apt/lists/* && 
[ "$JAVA_HOME" = "$(docker-java-home)" ]
RUN [ "cross-build-end" ]
ADD build/libs/app-0.0.1-SNAPSHOT.jar /app.jar
ENV JAVA_OPTS=""
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar

相关内容

  • 没有找到相关文章

最新更新