【问题标题】:Why does Spring boot execute SpringApplicationRunListener twice when using Eureka为什么Spring boot在使用Eureka时会执行两次SpringApplicationRunListener
【发布时间】:2016-08-21 10:42:33
【问题描述】:

我使用以下方法定义了一个简单的SpringApplicationRunListener 实现:

@Override
public void finished(ConfigurableApplicationContext configurableApplicationContext, Throwable throwable) {
    logger.info("It's finished");
}

当我在没有任何 Spring 云依赖的情况下运行时,我得到以下日志:

2016-04-27 10:37:37.702  INFO 5720 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-27 10:37:37.703  INFO 5720 --- [           main] b.a.test.LazyFilterRuntimeListener       : It's finished

但是,当我添加以下依赖项时:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

然后“完成”行被记录两次,一次在结尾(比如没有 Spring cloud/Eureka),一次在开始,甚至在 Spring 启动徽标出现之前:

2016-04-27 10:37:35.500  INFO 5720 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@255b53dc: startup date [Wed Apr 27 10:37:35 CEST 2016]; root of context hierarchy
2016-04-27 10:37:35.638  INFO 5720 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2016-04-27 10:37:35.785  INFO 5720 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'encrypt.CONFIGURATION_PROPERTIES' of type [class org.springframework.cloud.bootstrap.encrypt.KeyProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-04-27 10:37:35.786  INFO 5720 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'encryptionBootstrapConfiguration' of type [class org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration$$EnhancerBySpringCGLIB$$268d5fc8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-04-27 10:37:35.839  INFO 5720 --- [           main] b.c.test.LazyFilterRuntimeListener       : It's finished
2016-04-27 10:37:35.842  INFO 5720 --- [           main] be.company.test.TestApplication   : Started TestApplication in 0.499 seconds (JVM running for 0.86)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.7.RELEASE)


...

2016-04-27 10:37:37.702  INFO 5720 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-27 10:37:37.703  INFO 5720 --- [           main] b.c.test.LazyFilterRuntimeListener       : It's finished
2016-04-27 10:37:37.703  INFO 5720 --- [           main] be.company.test.TestApplication   : Started TestApplication in 2.423 seconds (JVM running for 2.721)

它还两次提到“Started Application in ...”这一行。

这种行为有什么特殊原因吗?我正在尝试编写一个SpringApplicationRunListener,它依赖于已创建的特定bean。但是,如果我添加spring cloud,第一次到达finished()方法时,上下文还没有创建,所以目前会抛出错误。

【问题讨论】:

  • 我遇到了同样的问题。你报告问题了吗?如果是这样,你能发布一个链接吗?
  • 不,没有报告。我们为我们的案例解决了这个问题。因为我们要求 bean 已经在上下文中,所以我们首先检查 bean 是否存在。 github.com/camunda/camunda-bpm-spring-boot-starter/commit/…。我不知道这个问题是否与 1.2.x 或 1.3.x 相关。

标签: java spring spring-boot spring-cloud


【解决方案1】:

实际上存在双重调用日志记录的问题。默认情况下,Spring Boot 设置 INFO 级别的日志记录。

Previous version of spring boot before 1.3,当你使用1.2.7 然后它使用

如果你不能用你的 MDC 覆盖 spring boot 的 CONSOLE_LOG_PATTERN 附加值,那么您似乎必须忍受每条日志消息 被写了两次! (一旦使用 Spring Boot 控制台附加程序和 一次将您的控制台附加程序与 MDC 添加到模式中)

但是在 Spring Boot 1.3 中 在较新版本的 Spring Boot 中,您可以轻松地包含 Spring Boot 中的 base.xml 并创建以下 logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />

    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

更多详情,您可以关注:How to disable logback ConsoleAppender in Spring Boot

资源链接:

  1. https://github.com/spring-projects/spring-boot/issues/1788
  2. https://github.com/spring-projects/spring-boot/issues/2558

【讨论】:

  • 我不太清楚这意味着什么。您是在建议将日志本身重复两次?不过,这不是问题。该方法肯定会被调用两次。时间戳也不同,“在 XX 秒内启动 TestApplication”中的“XX”也不同,所以我怀疑这纯粹是一个日志记录问题。它也适用于 Spring cloud + Spring boot,当我单独运行 Spring boot (1.2.7) 时没有任何问题。
  • 同意,同样的问题,在使用spring cloud config时也会发生
  • 问题出在spring cloud commons包中。这已在此处修复:github.com/spring-cloud/spring-cloud-commons/pull/100 并在 1.1.0+ 版本中可用
  • @Ruben 感谢分享。
猜你喜欢
  • 2019-01-16
  • 2022-01-26
  • 2021-10-06
  • 1970-01-01
  • 2019-01-22
  • 2019-04-29
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
相关资源
最近更新 更多