【问题标题】:Spring Boot - How to check specific application URLs for status 200Spring Boot - 如何检查特定应用程序 URL 的状态 200
【发布时间】:2021-06-17 01:46:39
【问题描述】:

我在 Spring Boot Admin 下监控了几个应用程序。 Spring Boot Admin 非常擅长告诉我应用程序是启动还是关闭以及其他各种指标。

我还想知道这些应用程序公开的某些 URL 返回的 HTTP 状态为 200。具体来说,我想每天向这些 URL 发送一次 GET 请求。如果它从其中任何一个接收到非 200 状态,它会发送一封电子邮件,说明哪些 URL 报告非 200。

Spring Boot Admin 可以做些什么吗?我知道自定义HealthIndicators,但不确定是否可以安排或是否适合。

在我构建自己的应用程序以进行 GET 调用和发送电子邮件之前,我只是想看看 Spring Boot Admin 是否提供了支持这样做的东西。

更新

URL 被暴露为 Eureka 服务,我通过 Spring Cloud OpenFeign 从其他服务调用服务。

更新 2

我继续构建自己的自定义应用程序来处理这个问题。详细信息如下,但如果 Spring 提供开箱即用的功能来执行此操作,您仍然感兴趣。

application.yml

app:
  serviceUrls: 
    - "http://car-service/cars?category=sedan"
    - "http://truck-service/trucks"
cron: "0 0 10 * * *"

网址被读入:

@Component
@ConfigurationProperties(prefix = "app")
@Getter
@Setter
public class ServiceUrls {
    private String[] serviceUrls;
}

通过 cron,计划每天运行一次:

@Component
@RequiredArgsConstructor
@Slf4j
public class ServiceCheckRunner {
    
    private final ServiceHealth serviceHealth;
    
    @Scheduled(cron = "${cron}")
    public void runCheck() {
        serviceHealth.check();
    }
}

这是检查 URL 是否返回错误的代码:

@Service
@RequiredArgsConstructor
@Slf4j
public class ServiceHealth {

    private final ServiceUrls serviceUrls;
    private final RestTemplate rest;
    
    public void check() {

        List<String> failedServiceUrls = new ArrayList<>();
        for (String serviceUrl : serviceUrls.getServiceUrls()) {
            try {

                ResponseEntity<String> response = rest.getForEntity(serviceUrl, String.class);
                
                if (!response.getStatusCode().is2xxSuccessful()) {
                    failedServiceUrls.add(serviceUrl);
                }

            } catch (Exception e){
                failedServiceUrls.add(serviceUrl);
            }
            
        }

        // code to send an email with failedServiceUrls.
    }   
}

【问题讨论】:

  • 您可以为每个下游 API 注册 HealthIndicators 并调用 /health 端点,该端点将以 JSON 格式返回每个 API 的状态。这会解决您的用例吗?
  • @dkb 感谢您的建议。您对每个应用程序调用 /health 端点有何看法? Spring Boot 管理员?
  • 您可以参考stackoverflow.com/q/44849568/2987755baeldung.com/spring-boot-health-indicators,一旦您为所有下游API注册了健康端点,那么您的应用程序/health端点将与其余的所有健康检查相关联。
  • 为什么不在服务健康检查中集成下游服务健康?
  • 我可以实现HealthIndicators。什么叫那些/health 端点? Spring Boot 管理员?如果我正在构建调用那些 /health 端点的应用程序,那么它似乎并没有比仅构建自定义应用程序和调用 URL 更好,正如我已经在 OP 中指出的那样。

标签: java spring spring-boot spring-boot-admin


【解决方案1】:

您可以使用 Spring Boot Admin 以便在注册客户将其状态从 UP 更改为 OFFLINE 或其他情况时发送电子邮件通知。

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.4.0</version>
</dependency>

application.properties

spring.mail.host=smtp.example.com
spring.mail.username=smtp_user
spring.mail.password=smtp_password
spring.boot.admin.notify.mail.to=admin@example.com

但是,如果您确实需要每天检查一次客户端状态,则需要实施自定义解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2018-07-10
    • 2020-05-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多