官术网_书友最值得收藏!

  • Mastering Spring Boot 2.0
  • Dinesh Rajput
  • 450字
  • 2021-06-25 21:29:20

Writing custom health indicators

Spring Boot's Actuator allows you to write a custom health indicator for your application. The Actuator's default /health endpoint provides information about your application status and disk space as follows:

{
   status: "UP",
   diskSpace: {
         status: "UP",
         total: 290391584768,
         free: 209125543936,
         threshold: 10485760
   }
} 

As you can see in the preceding JSON, the /health endpoint returns default health indicator data for common needs such as reporting the health of a disk or database. But you can also provide custom health information—you can register Spring Beans that implement the HealthIndicator interface. You need to provide an implementation of the health() method and return a Health response. The Health response should include a status and can optionally include additional details to be displayed. The following code shows a sample HealthIndicator implementation:

package com.dineshonjava.sba;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class DineshonjavaHealth implements HealthIndicator{

   @Override
   public Health health() {
         try {
               RestTemplate rest = new RestTemplate();
               rest.getForObject("https://www.dineshonjava.com", 
String.class); return Health.up().build(); } catch (Exception e) { return Health.down().build(); } } }

As you can see in the preceding code, we are going to plug in a custom health indicator that will check the health of the linking application website https://www.dineshonjava.com and it will return a response with the health status of this website as follows:

{
   status: "UP",
   dineshonjavaHealth: {
         status: "UP"
   },
   diskSpace: {
         status: "UP",
         total: 290391584768,
         free: 209125003264,
         threshold: 10485760
   }
} 

The DineshonjavaHealth class overrides the health() method of the HealthIndicator interface and simply uses Spring's RestTemplate to perform a GET request to the https://www.dineshonjava.com page. If it works, it returns a Health object indicating that Dineshonjava is UP. Otherwise, it will throw an exception and returns a Health object indicating that Dineshonjava is DOWN. Let's see what the following response will return if https://www.dineshonjava.com is down:

{
   status: "DOWN",
   dineshonjavaHealth: {
         status: "DOWN"
   },
   diskSpace: {
         status: "UP",
         total: 290391584768,
         free: 209124999168,
         threshold: 10485760
   }
}
 

As you can see, the status is DOWN but you can also add more details about its failure to access this website by using the withDetail() method on the Health builder as follows:

return Health.down().withDetail("reason", e.getMessage()).build();

Let's see the response of the /health endpoint again.
 {
   status: "DOWN",
   dineshonjavaHealth: {
         status: "DOWN",
         reason: "I/O error on GET request for "https://www.dineshonjava.com": www.dineshonjava.com; nested exception is java.net.UnknownHostException: www.dineshonjava.com"
   },
   diskSpace: {
         status: "UP",
         total: 290391584768,
         free: 209124995072,
         threshold: 10485760
   }
} 

As shown in the preceding example you can add additional details, whatever you want, with success or failure, by calling the withDetail() method of the Health builder class.

Now let's see how to create a custom endpoint.

主站蜘蛛池模板: 体育| 江门市| 玛纳斯县| 灵山县| 沈丘县| 通海县| 商河县| 门源| 长白| 柏乡县| 嘉义市| 津南区| 龙岩市| 阿拉善盟| 肥城市| 梨树县| 松溪县| 林口县| 孝义市| 启东市| 黔西县| 定襄县| 奉化市| 大新县| 乌拉特前旗| 曲阜市| 康平县| 安多县| 鄯善县| 新郑市| 崇仁县| 安陆市| 四平市| 中江县| 唐海县| 滁州市| 梁山县| 枣庄市| 治县。| 元阳县| 双牌县|