为什么Spring开机使用Eureka时会执行两次SpringApplication RunListener
Why does Spring boot execute SpringApplicationRunListener twice when using Eureka
我使用以下方法定义了一个简单的 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>
然后 "It's finished" 行被记录两次,一次在末尾(就像没有 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$8d5fc8] 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 ..." 两次。
这种行为是否有特殊原因?我正在尝试编写一个依赖于已创建的特定 bean 的 SpringApplicationRunListener
。但是,如果我添加 spring 云,它第一次到达 finished()
方法时,上下文尚未创建,因此它当前会抛出一个错误。
实际上存在一个双重调用记录的问题。默认情况下 Spring Boot 设置 INFO 级别的日志记录。
Previous version of spring boot before 1.3
,因为你正在使用 1.2.7
然后它使用
if you can't override spring boot's CONSOLE_LOG_PATTERN with your MDC
value added, then it seems you have to live with every log message
being written twice! (once with the spring boot console appender and
once with your console appender with MDC added to the pattern)
但在 spring 引导 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
资源Link:
我使用以下方法定义了一个简单的 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>
然后 "It's finished" 行被记录两次,一次在末尾(就像没有 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$8d5fc8] 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 ..." 两次。
这种行为是否有特殊原因?我正在尝试编写一个依赖于已创建的特定 bean 的 SpringApplicationRunListener
。但是,如果我添加 spring 云,它第一次到达 finished()
方法时,上下文尚未创建,因此它当前会抛出一个错误。
实际上存在一个双重调用记录的问题。默认情况下 Spring Boot 设置 INFO 级别的日志记录。
Previous version of spring boot before 1.3
,因为你正在使用 1.2.7
然后它使用
if you can't override spring boot's CONSOLE_LOG_PATTERN with your MDC value added, then it seems you have to live with every log message being written twice! (once with the spring boot console appender and once with your console appender with MDC added to the pattern)
但在 spring 引导 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
资源Link: