SpringBoot运行状况端点被覆盖

时间:2018-07-05 11:56:26

标签: spring-boot spring-boot-actuator

我正在尝试为执行器运行状况端点编写扩展。 按照https://github.com/spring-projects/spring-boot/wiki/Migrating-a-custom-Actuator-endpoint-to-Spring-Boot-2

的说明进行操作

但是我没有看到我的扩展被调用。 我确实看到此消息,使用不同的定义覆盖了bean'healthEndpointWebExtension'的bean定义: 因此,我创建的扩展名被Spring提供的默认版本覆盖

4 个答案:

答案 0 :(得分:1)

使用此代码,并记住您的班级名称必须必须完全是HealthEndpointWebExtension

@Component
@EndpointWebExtension(endpoint = HealthEndpoint.class)
public class HealthEndpointWebExtension {

@Autowired
private HealthEndpoint delegate;

@ReadOperation
public WebEndpointResponse<Health> getHealth() {
    Health health = this.delegate.health();
    Integer status = getStatus(health);
    return new WebEndpointResponse<>(health, status);
}
}

答案 1 :(得分:1)

Akkave是正确的。但是,作为补充,您需要将软件包设置为与spring一:package org.springframework.boot.actuate.health;相同,以确保它可以覆盖spring的bean!

答案 2 :(得分:0)

仅通过2.1.1.RELEASE测试:提供您自己的@WebEndpoint,如

@Component
@WebEndpoint(id = "acmehealth")
public class AcmeHealthEndpoint {

    @ReadOperation
    public String hello() {
      return "hello health";
    }
}

  1. 包括
  2. 将原始的/health映射到/internal/health
  3. 将您的自定义端点映射到/health

通过application.properties

management.endpoints.web.exposure.include=acmehealth
management.endpoints.web.path-mapping.health=internal/health
management.endpoints.web.path-mapping.acmehealth=/health

这里的重要部分是将原始端点映射到其他地方。这样可以防止碰撞。

答案 3 :(得分:0)

您实际上无法编写EndpointWebExtension作为预定义的裁切器URL。但是您可以做的是可以覆盖EndpointWebExtension的现有定义,如下所示。您可以根据要求编写自定义逻辑。

@Component

@EndpointWebExtension(endpoint = HealthEndpoint.class)

public class CustomeEndpointWebExtension extends HealthEndpointWebExtension {

    private static final String[] NO_PATH = {};
    public HealthWebEndpointWebExtension(HealthContributorRegistry registry, HealthEndpointGroups groups) {
        super(registry, groups);

    }

//To write custom logic for /acuator/health
    @ReadOperation
    public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext) {
        System.out.println("#@Start");
        WebEndpointResponse<HealthComponent> health = health(apiVersion, securityContext, false, NO_PATH);
        //Can write customer logic here as per requirment
        System.out.println("#@End"+health.getBody().getStatus());
        return health;
    }

//To write custom logic for /acuator/health/db
    @ReadOperation
    public WebEndpointResponse<HealthComponent> health(ApiVersion apiVersion, SecurityContext securityContext,
            @Selector(match = Match.ALL_REMAINING) String... path) {
        WebEndpointResponse<HealthComponent> health = health(apiVersion, securityContext, false, path);
        System.out.println("#Status"+health.getBody().getStatus());
        return health;
    }

}

相关问题