0

从 Sprig Boot 应用程序是否可以直接访问执行器/健康数据而不进行休息调用和解析结果?

理想情况下,我可以自动装配一个 bean,然后能够通过方法调用获取健康数据的对象表示。

例如,如果我的健康端点显示如下内容:

{
    "status": "UP",
    "components": {
        "db": {
            "status": "UP",
            "details": {
                "database": "PostgreSQL",
                "result": 1,
                "validationQuery": "SELECT 1"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 499963174912,
                "free": 389081399296,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        },
        "redis": {
            "status": "UP",
            "details": {
                "version": "3.2.12"
            }
        }
    }
}

那么我可以自动装配哪些组件来找出这些信息中的每一个?

4

3 回答 3

1

当然你可以注入相应的Endpoint. 例如,如果您对此感兴趣,HealthEndpoint您可以这样做:

@RestController
public class ActuatorController {

    private final HealthEndpoint healthEndpoint;

    public ActuatorController(HealthEndpoint healthEndpoint) {
        this.healthEndpoint = healthEndpoint;
    }

    @GetMapping("health")
    public String health() {
        return healthEndpoint.health().getStatus().getCode();
    }
}
于 2020-03-18T14:51:24.503 回答
1

可能有更好的方法来获取健康数据,但这对我有用。

@RestController
public class HealthController {
    @Autowired
    HealthContributor[] healthContributors;
    
    
    @GetMapping("/health")
    public Map<String, String> health() {
        Map<String, String> components = new HashMap<String, String>();
        for(HealthContributor healthContributor : healthContributors) {
            String componentName = healthContributor.getClass().getSimpleName().replace("HealthIndicator", "").replace("HealthCheckIndicator", "");
            String status = ((HealthIndicator)(healthContributor)).health().getStatus().toString();
            //To get details
            //Map<String,Object> details = ((HealthIndicator)(healthContributor)).health().getDetails();
            
            components.put(componentName, status);
            
        }
        return components;
    }
    
}

示例输出:

{
  "Mail":"UP",
  "Ping":"UP",
  "Camel":"UP",
  "DiskSpace":"UP"
}

要模拟 HealthContributor[],您可以尝试使用 Mockito,如下所示:

@Profile("test")
@Configuration
public class HealthContributorsMockConfig {


    @Primary
    @Bean(name = "healthContributors")
    public HealthContributor[] healthContributors() {
        HealthContributor[] healthContributors = new HealthContributor[2];
        
        HealthContributor healthContributorA = Mockito.mock(HealthIndicator.class, new Answer<Object>() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                // TODO Auto-generated method stub
                Health health = Health.up().build();
                return health;
            }
            
        });
        
        HealthContributor healthContributorB = Mockito.mock(HealthIndicator.class, new Answer<Object>() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                // TODO Auto-generated method stub
                Health health = Health.down().build();
                return health;
            }
            
        });
        
        healthContributors[0] = healthContributorA;
        healthContributors[1] = healthContributorB;
        return healthContributors;
        
    }
}
于 2020-12-28T21:52:11.883 回答
0

此外,默认情况下,执行器中启用了信息和健康点,您无需手动公开它们。将依赖项添加到 pom.xml 的那一刻,它将被启用,并且您可以访问 url 端点而不暴露它们

于 2020-03-18T16:29:04.850 回答