Spring启动时检查外部服务状态

时间:2015-06-09 10:53:12

标签: spring spring-boot

我想在Spring Boot准备好之前检查一些外部http服务。

外部Web服务的url存储在带有@ConfigurationProperties类的属性文件中。

我如何使用带有ping方法的springApplication.addListner()进行检查。但是属性类还没有被初始化。

public class ApplicationStartListener implements ApplicationListener<ApplicationPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent event) {
String url = AppProp.getURL();
inet = InetAddress.getByName(url );
inet.isReachable(5000)

...

application.yml

tops:
    http://service.com

@Component
@ConfigurationProperties("tops")
public class AppProp{

    private static String url;


    public static String getUrl() {

1 个答案:

答案 0 :(得分:0)

实现此目的的最简单方法是实现ApplicationRunner接口。

从Spring Boot文档[1]

  

如果您需要在SpringApplication启动后运行某些特定代码,则可以实现ApplicationRunnerCommandLineRunner接口。两个界面都以相同的方式工作,并提供单个run方法,该方法将在SpringApplication.run(…​)完成之前调用

[1] https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-command-line-runner

假设您在url中定义了application.properties

@SpringBootApplication
public class MyApplication implements ApplicationRunner
{

    @Inject
    private AppConfig appConfig;

    @Inject
    private ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) 
    {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception 
    {
        InetAddress inetAddress = InetAddress.getByName(appConfig.getUrl());
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Component
    @ConfigurationProperties
    public static class AppConfig 
    {
        private String url;

        public String getUrl() 
        {
            return url;
        }

        public void setUrl(String url) 
        {
            this.url = url;
        }
    }
}

如果您需要更多控制权,可以使用SpringApplicationRunListener [2]

[2] http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplicationRunListener.html

@SpringBootApplication
public class MyApplication implements SpringApplicationRunListener
{

    public MyApplication() { }

    public MyApplication(SpringApplication springApplication, String[] args) { }

    public static void main(String[] args) 
    {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void started() { }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) 
    {
        // 1st opportunity
        InetAddress inetAddress = InetAddress.getByName(environment.getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) 
    {
        // 2nd opportunity
        InetAddress inetAddress = InetAddress.getByName(context.getEnvironment().getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) 
    {
        // 3rd opportunity
        InetAddress inetAddress = InetAddress.getByName(context.getEnvironment().getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Override
    public void finished(ConfigurableApplicationContext context, Throwable exception) 
    {
        // 4th opportunity
        InetAddress inetAddress = InetAddress.getByName(context.getEnvironment().getProperty("url"));
        if (!inetAddress.isReachable(5000)) 
        {
            // Stop the application or do other things
        }
    }

    @Component
    @ConfigurationProperties
    public static class AppConfig {
        private String url;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}

然后创建META-INF\spring.factories并添加

org.springframework.boot.SpringApplicationRunListener=com.foobar.MyApplication