Spring Boot 检查应用程序中所有API的运行状况,并在响应中提供HTTP状态代码

ego6inou  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(144)

在我的应用程序中,有10个API我想使用 Spring Boot 执行器创建一个运行状况检查,以便对所有API进行详细检查(包括所有剩余控制器)。目前,我可以轻松地展示单个API(Rest控制器)运行状况,方法是编写customHealthIndicator类并覆盖运行状况那么有没有办法创建一个customeHealthIndicator类,并测试所有API是UP还是DOWN,或者为每个API(RestController)创建单独的customeHealthIndicator类。

@Component
public class AlarmLogHealthIndicator
    implements HealthIndicator {

 @Autowired
  private AlarmLogService alarmLogService;


  private final String message_key = "v2/alarms/cumulativeDetails";

  @Override
  public Health health() {
    if (!isRunningServiceA()) {
      return Health.down().withDetail(message_key, "DOWN").build();
    }
    return Health.up().withDetail(message_key, "UP").build();
  }
  private Boolean isRunningServiceA() {
    Boolean isRunning = true;
   
    // Logic to check API is UP or DOWN

      RestTemplate restTemplate = new RestTemplate();
     
      HttpHeaders headers = new HttpHeaders();
      HttpEntity<ToolModuleRequestDTO> entity = new HttpEntity<>(getRqstData(), headers);

      ResponseEntity<List<CumulativeDetailsDTO>> responseEntity = restTemplate.exchange(
             ("http://localhost:7072/v2/alarms/cumulativeDetails"),
              HttpMethod.POST, entity, new ParameterizedTypeReference<List<CumulativeDetailsDTO>>() {} );

      if (responseEntity.getStatusCodeValue() ==500) {
          isRunning = false;
      } 
//           
    return isRunning;
  }
    private ToolModuleRequestDTO getRqstData(){
       ToolModuleRequestDTO toolModuleRequestDTO = new ToolModuleRequestDTO();
        
        LocalDate  startTime = LocalDate.now();
        LocalDate  endTime = LocalDate.now();
        LocalDateTime now = LocalDateTime.now();
        List   severity = new ArrayList<>();
        severity.add("Error");
        severity.add("Fatal");
        severity.add("Critical");
        String  exclude1 = "WAC";
        Map<String, String>  mapValue = new HashMap<>();
        mapValue.put(toolId, "9c9b8482-3ad6-4c2d-99e1-de2cda68a2d7");
      
               
        DateTimeFormatter formatterStart = DateTimeFormatter.ofPattern(Constants.SqlConstants.DATE_FORMAT_START);
        DateTimeFormatter formatterEnd = DateTimeFormatter.ofPattern(Constants.SqlConstants.DATE_FORMAT_END);
        String startTimeStr = formatterStart.format(startTime);
        String endTimeStr = formatterEnd.format(endTime);
//      startTimeStamp = Timestamp.valueOf(startTimeStr);
//      endTimeStamp = Timestamp.valueOf(endTimeStr);
        toolModuleRequestDTO.setStartDate(LocalDate.parse("2022-04-01"));
        toolModuleRequestDTO.setEndDate(LocalDate.parse("2022-04-30"));
        toolModuleRequestDTO.setSeverity("Critical,Error,Fatal");
        return toolModuleRequestDTO;
    }
}
2hh7jdfx

2hh7jdfx1#

我认为最好为每个API创建一个单独的自定义指示器类。

相关问题