Spring boot SpringBoot运行状况终结点已覆盖

Spring boot SpringBoot运行状况终结点已覆盖,spring-boot,spring-boot-actuator,Spring Boot,Spring Boot Actuator,我正在尝试为acutator health端点编写扩展。 按照 但是,我没有看到调用my扩展。 我确实看到这条消息使用不同的定义覆盖bean“healthEndpointWebExtension”的bean定义: 因此,我创建的扩展将被Spring提供的默认版本覆盖。请使用此代码,并记住类的名称必须是HealthEndpointWebExtension @Component @EndpointWebExtension(endpoint = HealthEndpoint.class) public

我正在尝试为acutator health端点编写扩展。 按照

但是,我没有看到调用my扩展。 我确实看到这条消息使用不同的定义覆盖bean“healthEndpointWebExtension”的bean定义:
因此,我创建的扩展将被Spring提供的默认版本覆盖。请使用此代码,并记住类的名称必须是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);
}
}
@组件
@EndpointWebExtension(endpoint=HealthEndpoint.class)
公共类HealthEndpointWebExtension{
@自动连线
私人代表;
@再手术
公共WebEndpointResponse getHealth(){
Health Health=this.delegate.Health();
整数状态=getStatus(运行状况);
返回新的WebEndpointResponse(运行状况、状态);
}
}

Akkave是正确的。但是,作为补充,您需要将包设置为与SpringOne相同:
package org.springframework.boot.actuate.health以确保它超越了spring的bean

刚刚用2.1.1.RELEASE进行了测试:提供您自己的
@WebEndpoint

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

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

  • 包括它
  • 将原始数据映射到,比如
  • 将自定义端点映射到
  • 通过
    application.properties

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

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

    您实际上无法为预定义的acutator URL编写EndpointWebExtension。但您可以做的是覆盖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;
        }
    
    }
    
    
    @组件
    @EndpointWebExtension(endpoint=HealthEndpoint.class)
    公共类CustomeEndpointWebExtension扩展了HealthEndpointWebExtension{
    私有静态最终字符串[]NO_PATH={};
    公共HealthWebEndpointWebExtension(HealthContributorRegistry注册表,HealthEndpointGroups组){
    超级(登记处、小组);
    }
    //为/acuator/health编写自定义逻辑的步骤
    @再手术
    公共WebEndpointResponse运行状况(ApiVersion ApiVersion、SecurityContext SecurityContext){
    System.out.println(“#@Start”);
    WebEndpointResponse health=health(apiVersion,securityContext,false,无路径);
    //可以根据需要在此处写入客户逻辑
    System.out.println(“#@End”+health.getBody().getStatus());
    恢复健康;
    }
    //为/acuator/health/db编写自定义逻辑
    @再手术
    公共WebEndpointResponse运行状况(ApiVersion ApiVersion、SecurityContext SecurityContext、,
    @选择器(匹配=匹配。所有剩余)字符串…路径){
    WebEndpointResponse health=health(apiVersion,securityContext,false,path);
    System.out.println(“#Status”+health.getBody().getStatus());
    恢复健康;
    }
    }
    
    仍然不起作用。这和我做的差不多。调试时也没有异常这不起作用,给了我一个重复的bean定义异常:
    已经定义了一个同名的bean
    你能解决这个问题吗?我在尝试扩展env端点时遇到了类似的问题。标准执行器端点实际上并不意味着要被覆盖,本页旨在解释如何为您自己的端点执行此操作,而不是Spring Boot默认提供的端点。