2015-06-09 21 views
0

我想在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() { 
+0

如何'AppProp'定义? –

+0

你想要处理有关服务状态的信息? – chrylis

回答

0

做到这一点最简单的方法是实现ApplicationRunner接口。

从春天启动文档[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

假设你有在application.properties定义url

@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