2016-04-28 48 views
1

我必須創建這樣一個PostgreSQL健康指標:春季啓動驅動器健康端點

@Component 
public class PostgresHealthIndicator extends AbstractHealthIndicator { 

    @Autowired 
    DataSource postgresDataSource; 

    public DataSourceHealthIndicator dbHealthIndicator() { 
     DataSourceHealthIndicator indicator = new DataSourceHealthIndicator(postgresDataSource); 
     return indicator; 
    } 

    @Override 
    protected void doHealthCheck(Health.Builder builder) throws Exception { 
     Health h = dbHealthIndicator().health(); 
     Status status = h.getStatus(); 
     if (status != null && "DOWN".equals(status.getCode())) { 
      builder.down(); 
     } else { 
      builder.up(); 
     } 
    } 
} 

在我Application.java我掃描這個包這個組件:

@ComponentScan({"com.bp.health"}) 

在我的應用程序。屬性我有以下設置:

endpoints.health.sensitive=false 
endpoints.health.id=health 
endpoints.health.enabled=true 

當我打{url}/health我看到:

{ 「地位」: 「DOWN」}

什麼,我需要做的就是自定義的健康指示器顯示?

回答

6

您需要進行身份驗證才能看到所有的細節。 另外,您可以設置

management.security.enabled=false 
endpoints.health.sensitive=false 

看到完整內容未經認證

有關的更多信息可以在這裏找到: Production ready monitoring

2

你爲什麼要這麼做?該代碼甚至沒有編譯,Spring Boot默認已經檢查了你的數據源。如果您只看到狀態,那是因爲您未通過身份驗證,請查看此表in the doc瞭解更多詳情。