代码之家  ›  专栏  ›  技术社区  ›  Yore

SpringBoot Actuator 1.X版就绪性和活性探针

  •  1
  • Yore  · 技术社区  · 1 年前

    我正在探索为SpringBoot1.X激活就绪性和活跃性探针的选项。为无缝的Kubernetes部署启用这两个端点是至关重要的。对实现这一目标有什么见解吗?

    /actuator/health/liveness
    /actuator/health/readiness
    

    在即将到来的Spring版本中获得这些功能是否可行,或者我应该自己实现它们吗?

    提前谢谢。

    1 回复  |  直到 1 年前
        1
  •  1
  •   deikyb    1 年前

    对于SpringBoot1.X,您不会有这些开箱即用的端点。但是,您可以实现自定义健康检查端点以实现类似的功能。

    创建自定义健康指标:通过扩展Spring Boot提供的HealthIndicator接口来实现自定义健康指标。您可以编写逻辑来确定应用程序的不同组件的运行状况。

        import org.springframework.boot.actuate.health.Health;
        import org.springframework.boot.actuate.health.HealthIndicator;
        import org.springframework.stereotype.Component;
    
        @Component
        public class CustomHealthIndicator implements HealthIndicator {
    
        @Override
        public Health health() {
            // Logic to check the health of your application
            if (isApplicationHealthy()) {
                return Health.up().build();
            } else {
                return Health.down().build();
            }
        }
    
        private boolean isApplicationHealthy() {
            // Check the health of your application components
            return true; // Return true if the application is healthy, false otherwise
        }
    }
    
    

    公开自定义端点:在应用程序控制器中公开自定义端点以公开运行状况信息。

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.boot.actuate.health.Health;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.RestController;
    
        @RestController
        public class HealthCheckController {
    
        @Autowired
        private CustomHealthIndicator customHealthIndicator;
    
        @GetMapping("/custom/health/liveness")
        public Health liveness() {
            return customHealthIndicator.health();
        }
    
        @GetMapping("/custom/health/readiness")
        public Health readiness() {
            return customHealthIndicator.health();
        }
    }
    

    在Kubernetes中配置探测器:一旦您实现了这些端点,您就可以配置Kubernete,使其在部署配置中用于就绪和活跃探测器。

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: your-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: your-app
      template:
        metadata:
          labels:
            app: your-app
        spec:
          containers:
            - name: your-app
              image: your-app-image:tag
              ports:
                - containerPort: 8080
              readinessProbe:
                httpGet:
                  path: /custom/health/readiness
                  port: 8080
              livenessProbe:
                httpGet:
                  path: /custom/health/liveness
                  port: 8080
    
        2
  •  0
  •   Yore    1 年前

    如果有人需要同样的东西,这就是我想出的最后一个代码:

    对于 Kubernetes健康指标:

    import lombok.RequiredArgsConstructor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.actuate.health.CompositeHealthIndicator;
    import org.springframework.boot.actuate.health.Health;
    import org.springframework.boot.actuate.health.HealthAggregator;
    import org.springframework.boot.actuate.health.HealthIndicator;
    import org.springframework.stereotype.Component;
    import org.springframework.util.Assert;
    
    import java.util.Locale;
    import java.util.Map;
    
    @Component
    @RequiredArgsConstructor(onConstructor = @__(@Autowired))
    public class KubernetesHealthIndicator implements HealthIndicator {
      
      private final HealthAggregator healthAggregator;
      
      private final Map<String, HealthIndicator> healthIndicators;
      
      @Override
      public Health health() {
        Assert.notNull(healthAggregator, "HealthAggregator must not be null");
        Assert.notNull(healthIndicators, "HealthIndicators must not be null");
        CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator);
        for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
          healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
        }
        
        return healthIndicator.health();
      }
      
      private String getKey(String name) {
        int index = name.toLowerCase(Locale.ENGLISH).indexOf("healthindicator");
        if (index > 0) {
          return name.substring(0, index);
        }
        
        return name;
      }
    }
    

    对于 KubernetsHeathCheckController :

    import lombok.RequiredArgsConstructor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.actuate.health.Health;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequiredArgsConstructor(onConstructor = @__(@Autowired))
    @RequestMapping(value = "/actuator/health", produces = {MediaType.APPLICATION_JSON_VALUE})
    public class KubernetesHealthCheckController {
      
      private final KubernetesHealthIndicator kubernetesHealthIndicator;
      
      @GetMapping("/liveness")
      public Health liveness() {
        return kubernetesHealthIndicator.health();
      }
      
      @GetMapping("/readiness")
      public Health readiness() {
        return kubernetesHealthIndicator.health();
      }
    }