All Products
Search
Document Center

Serverless App Engine:Configure health checks for a Spring Boot application

Last Updated:Oct 19, 2023

You can perform health checks on Spring Boot applications based on HTTP or TCP ports. You can also use Spring Boot Actuator to perform custom health checks. This topic describes how to use Spring Boot Actuator to configure health checks for a Spring Boot application.

Background information

Spring Boot Actuator is used to expose the monitoring information of Spring Boot applications. You can use Spring Boot Actuator to view and calculate specific metrics of your application. You can also use Spring Boot Actuator to configure health checks based on your business requirements. For more information, see Spring Boot Actuator official documentation.

Procedure

  1. Add the following dependency to a Maven project:

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  2. Configure the application.properties configuration file to display health check information.

    management.endpoints.web.base-path= /           # The default access path of Spring Boot Actuator whose version is earlier than 2.0 is /. You can specify /health to check whether the application configurations in the / path are healthy. The default access path of Spring Boot Actuator V2.0 or later is /actuator. You can specify /actuator/health to check whether the application configurations in the /actuator path are healthy. 
    management.endpoint.health.show-details=always  # Display the health check information. Default value: never. This value indicates that the health check information is not displayed. 
  3. You can use auto-configured health indicators that are provided by Spring Boot Actuator or configure a custom process to check your application.

    • Use auto-configured health indicators.

      Spring Boot Actuator provides multiple auto-configured health indicators (HealthIndicators). For example, if your application uses Redis and MongoDB databases, the RedisHealthIndicator and MongoHealthIndicator are used to check whether the databases are accessible. For more information about auto-configured health indicators, see Auto-configured HealthIndicators.

      You can disable all or specific auto-configured health indicators.

      management.health.defaults.enable=false  # Disable all default health indicators.   
      management.health.mongo.enable=false     # Disable the MongoHealthIndicator. 
      Note

      We recommend that you enable health indicators based on the requirements of your application. If you enable all health indicators, the health check may consecutively fail.

    • Configure a custom check process. You can use the /health/custom path to obtain the check result of each health indicator.

      Create a CustomHealthIndicator.java file and enter the code to perform specific checks based on your business requirements. For example, you can check whether the database connection is normal and check the thread pool status. Sample code:

      @Component
      public class CustomHealthIndicator extends AbstractHealthIndicator {
          @Override
          protected void doHealthCheck(Health.Builder builder) throws Exception {
             # Perform specific checks based on your business requirements. 
             if (checkSomething()) {  
                 builder.up().withDetail("Item", "xxx").withDetail("error", "null");
             } else {
                 builder.down().withDetail("Item", "xxx").withDetail("error", "xxxErrorCode");
              }
          }
      }
  4. After you configure the settings, run the application and perform the health check.

    • Perform the health check by accessing the default port. Sample command:

      curl 127.0.0.21:8080/health/custom  # custom indicates the prefix of the health indicator. Specify a value based on your business scenario. 
      Note
      • The default access path of Spring Boot Actuator whose version is earlier than 2.0 is /. You can specify /health to perform a health check.

      • The default access path of Spring Boot Actuator V2.0 or later is /actuator. You can specify /actuator/health to perform a health check.

      Sample output:

      {"status":"UP","details":{"custom":{"status":"UP","details":{"Item","xxx","error","null"}}}}

      Valid values of the status field:

      • UP: The HTTP status code 200 is returned, which indicates that the health check was successful.

      • DOWN: The HTTP status code 503 is returned, which indicates that the health check failed.

      Note

      For example, the /health check path contains the results of multiple health indicators. If a result contains DOWN, the HTTPS 503 is returned and the health check fails.

    • Configure a health check in the SAE console. For more information, see Configure application health checks. Best practices for health checks