【问题标题】:Spring Cloud Config Server: Listen for ApplicationFailedEvent when invalid Yaml config file throws a ParserExceptionSpring Cloud Config Server:当无效的 Yaml 配置文件抛出 ParserException 时侦听 ApplicationFailedEvent
【发布时间】:2020-07-03 20:00:34
【问题描述】:

当 Spring Cloud Config Server 无法解析我们的 YAML 配置文件时,我正在尝试侦听 ApplicationFailedEvent。发生这种情况时,我想发出应用程序警报。我已经使用 Annotation 注册了所有 ApplicationFailedEvent 类型:

@Component
public class ApplicationFailedEventListener {
    private static final Logger LOGGER =
        LoggerFactory.getLogger( ApplicationFailedEventListener.class );

    @EventListener
    public void handleContextRefreshEvent( ContextStartedEvent ctxStartEvt) {
        System.out.println("Context Start Event received.");
    }

    @EventListener( ApplicationFailedEvent.class )
    public void handleApplicationFailedEvent( ApplicationFailedEvent event ) {
        LOGGER.info( "Received event: {}", event );
    }

}

使用界面

public class SpringBuiltInEventsListener implements ApplicationListener<SpringApplicationEvent>,
    ApplicationContextAware {
...
}

并将侦听器添加到 SprintApplication:

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
@EnableDiscoveryClient
@EnableTask
public class SpringCloudConfigServerApplication {
    private static final Logger LOGGER =
        LoggerFactory.getLogger( SpringCloudConfigServerApplication.class );

    public static void main( String[] args ) {
        SpringApplication springApplication =
            new SpringApplication( SpringCloudConfigServerApplication.class );
        springApplication.addListeners(( ApplicationFailedEvent event)->{
            System.out.println("Executing ApplicationFailedEvent...");
        });
        springApplication.addListeners( new CustomEventHandler() );
        springApplication.addListeners(new SpringBuiltInEventsListener());
        ConfigurableApplicationContext context = springApplication.run( args );
    }
}

但我的监听器永远不会被 ApplicationFailedEvent 调用。它确实会为所有其他 SpringApplicationEvent 调用。

如何处理 Spring Cloud Config Server 发送的 ApplicationFailedEvent?似乎当它发送它时,SimpleApplicationEventMulticaster.multicastEvent() 中的侦听器列表为空???

感谢任何帮助。

【问题讨论】:

    标签: spring server cloud config


    【解决方案1】:

    我找到了一种无需使用 ApplicationFailedEvent 即可获取 Yaml ParseException 的方法。

    创建您自己的自定义 SpringApplicationRunListener:

    public class CustomSpringApplicationRunListener
        implements SpringApplicationRunListener {
        private static final Logger LOGGER =
            LoggerFactory.getLogger( CustomSpringApplicationRunListener.class );
    
        public CustomSpringApplicationRunListener( SpringApplication application,
            String[] args ) {
            super();
        }
    
        @Override
        public void failed( ConfigurableApplicationContext context,
            Throwable exception ) {
            LOGGER.info( "Received exception: {}", exception.getMessage() );
            LOGGER.info( "Received exception cause: {}",
                exception.getCause().getMessage() );
        }
    }
    

    在 main() 中无事可做

    @SpringBootApplication
    @Configuration
    @EnableAutoConfiguration
    @EnableConfigServer
    @EnableDiscoveryClient
    public class SpringCloudConfigServerApplication {
    
        public static void main( String[] args ) {
            SpringApplication.run( SpringCloudConfigServerApplication.class, args );
        }
    
    }
    

    在资源中创建文件 META-INF/spring.factories 并添加以下内容:

    org.springframework.boot.SpringApplicationRunListener=com.springcloudconfigserver.CustomSpringApplicationRunListener
    

    现在我收到实际异常的调用,我可以使用详细的解析错误信息发出警报。

    【讨论】:

      猜你喜欢
      • 2016-02-27
      • 2015-08-25
      • 2018-03-22
      • 2020-06-03
      • 2022-12-04
      • 2017-06-23
      • 2016-07-06
      • 2017-10-12
      • 1970-01-01
      相关资源
      最近更新 更多