SoFunction
Updated on 2025-03-04

How to start health checks in SpringBoot project

1. The importance of health checks

Health checks are a mechanism to ensure that the application in the container is running normally. It can help us discover and deal with problems in our applications in a timely manner, such as service crashes, resource exhaustion, etc. By regularly checking the health of the application, we can intervene before the problem affects the user experience, thereby improving the reliability and stability of the system.

2. Basic configuration of Docker health check

Docker allows us to configure health checks by adding the HEALTHCHECK directive to the Dockerfile or using the --health-* option in the docker run command. The basic configuration of health checks includes test commands, check intervals, timeouts, retry times, and startup periods.

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

#Health interfacemanagement:
  endpoints:
    web:
      exposure:
        include: health

-compose file

healthcheck:
  test: [ "CMD", "curl", "-f", "http://localhost:80/actuator/health" ]
  interval: 10s
  timeout: 10s
  retries: 3
  start_period: 40s
  • test: This is a health check test command, which can be any command or script. In this example, we usecurlCome and check/actuator/healthEndpoint. ifcurlIf the command fails (i.e., return a non-200 status code), the service is considered unhealthy.
  • interval: This is the interval time between health check tests, the default unit is seconds.
  • timeout: This is the timeout time for a single health check test, the default unit is seconds.
  • retries: If the container state is unhealthy after several consecutive failures.
  • start_period: How long will it take to start a health check after the Docker container is started. This ensures that the container has enough time to start and initialize.

4. Test command

Test commands are at the heart of health checks, which define how to determine whether the application within a container is healthy. In the above configuration, the test command iscurl -f http://localhost:80/actuator/health. This command attempts to use the curl tool to send a request to port 80 inside the container to check the health status of the application. If the request fails (i.e., return a non-200 status code), the application is considered unhealthy.

The examination interval defines the frequency at which health checks are performed. In the above configuration, the interval is set to 10 seconds. This means that every 10 seconds, Docker performs a health check.

5. Timeout time

The timeout defines the maximum time for the health check command to be executed. If the command does not complete within this time period, Docker will consider the health check to fail. In the above configuration, the timeout time is also set to 10 seconds.

6. Number of retry times

The number of retries defines how many times Docker will try to recheck after a health check fails. If the retry is exhausted, Docker will consider the container unhealthy and may take appropriate measures, such as restarting the container. In the above configuration, the number of retry times is set to 3.

7. During startup

During startup, it defines how long it takes to wait for the health check to begin after the container is started. This setting prevents health checks from being performed before the in-container application is fully started, thus avoiding misjudgment. In the above configuration, the startup period is set to 40 seconds.

3. Practical application of health examination

In practical applications, health checks can help us achieve a variety of functions, such as automatically restarting unhealthy containers, back-end health checks of load balancers, etc.

1. Automatic restart

When an application inside the container fails, Docker can detect this through a health check and automatically restart the container. This can reduce manual intervention and improve the system's self-healing ability.

2. Load balancing

In a load balancer, health checks can be used to determine which backend containers are healthy, thus forwarding traffic only to healthy containers. This can improve system availability and performance.

4. Advanced configuration of health checks

In addition to basic configurations, Docker also supports some advanced configurations, such as customizing health status codes, performing multiple health checks, etc.

1. Customize health status code

In some cases, the application may return a specific status code to indicate a healthy state. Docker allows us to customize these status codes through the --health-status option.

2. Multiple health checks

In complex applications, multiple health checks may be required to ensure that all aspects of the application are normal. Docker supports this through multiple HEALTHCHECK directives.

in conclusion

Health checks are an important feature of Docker containerization technology. They can help us ensure the health of applications in containers and improve the reliability and stability of the system. By placing health checks reasonably, we can intervene before the problem affects the user experience, thereby improving the service quality of the system. With the continuous development of containerization technology, the importance of health checks will become increasingly obvious, and it will become a key component in building highly available systems.

The above is the detailed content of how to start health checks in SpringBoot project. For more information about health checks in SpringBoot project, please follow my other related articles!