如何使`@Endpoint(id =" health")`在Spring Boot 2.0中工作?

时间:2017-10-17 18:30:08

标签: java spring spring-boot spring-boot-actuator

我已尝试在Spring Boot 2.0.0.M5中自定义健康执行器的新方法,如下所述:https://spring.io/blog/2017/08/22/introducing-actuator-endpoints-in-spring-boot-2-0

@Endpoint(id = "health")
public class HealthEndpoint {
    @ReadOperation
    public Health health() {
        return new Health.Builder()
            .up()
            .withDetail("MyStatus", "is happy")
            .build();
    }
}

但是,当我将HTTP GET运行到localhost:port/application/health时,我仍然会获得标准的默认健康信息。我的代码完全被忽略了。

当我使用"传统方式"通过HealthIndicator的实施来定制健康信息,它按预期工作,健康信息用给定的细节装饰:

@Component
public class MyHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return new Health.Builder()
            .up()
            .withDetail("MyStatus 1.1", "is happy")
            .withDetail("MyStatus 1.2", "is also happy")
            .build();
    }
}

问题:我还需要配置和/或实施什么才能使@Endpoint(id = "health")解决方案有效?

我的目的是来创建自定义执行器myhealth,而是自定义现有的health执行器。基于文档,我希望通过实现HealthIndicator获得相同的结果。我错在那个假设吗?

Maven配置pom.xml包含:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M5</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

Spring Boot配置application.properties包含:

endpoints.health.enabled=true
endpoints.autoconfig.enabled=true
endpoints.autoconfig.web.enabled=true

2 个答案:

答案 0 :(得分:5)

更新

  • 新的Spring Actuator端点上的documentation不是很清晰。它试图以现有的健康端点为例解释新的端点基础设施。
  • 新的端点ID必须唯一,并且不应与现有的执行器端点相同。如果尝试将下面显示的示例的ID更改为health,则会出现以下异常:

    java.lang.IllegalStateException: Found two endpoints with the id 'health'
    
  • 上述关于使用@Bean注释声明端点类的注释是正确的。

  • 自定义health端点在Spring Boot 2.0中没有更改。您仍然需要实施HealthIndicator来添加自定义值。

自定义执行器端点

以下是在Spring Boot 2.0中创建自定义Actuator端点所需的更改。

模型

包含您的自定义信息的域。

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class MyHealth {

    private Map<String, Object> details;

    @JsonAnyGetter
    public Map<String, Object> getDetails() {
        return this.details;
    }
}

我的健康终点

声明myhealth端点,

@Endpoint(id = "myhealth")
public class MyHealthEndpoint {

    @ReadOperation
    public MyHealth health() {
        Map<String, Object> details = new LinkedHashMap<>();
        details.put("MyStatus", "is happy");
        MyHealth health = new MyHealth();
        health.setDetails(details);

        return health;
    }
}

我的健康延伸

myhealth端点的扩展名

@WebEndpointExtension(endpoint = MyHealthEndpoint.class)
public class MyHealthWebEndpointExtension {

    private final MyHealthEndpoint delegate;

    public MyHealthWebEndpointExtension(MyHealthEndpoint delegate) {
        this.delegate = delegate;
    }

    @ReadOperation
    public WebEndpointResponse<MyHealth> getHealth() {
        MyHealth health = delegate.health();
        return new WebEndpointResponse<>(health, 200);
    }
}

执行器配置

配置将两个新创建的执行器类暴露为bean,

@Configuration
public class ActuatorConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    public MyHealthEndpoint myHealthEndpoint() {
        return new MyHealthEndpoint();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    @ConditionalOnBean({MyHealthEndpoint.class})
    public MyHealthWebEndpointExtension myHealthWebEndpointExtension(
            MyHealthEndpoint delegate) {
        return new MyHealthWebEndpointExtension(delegate);
    }
}

应用程序属性

application.yml

的更改
endpoints:
  myhealth:
    enabled: true

启动应用程序后,您应该能够在http://<host>:<port>/application/myhealth访问新的执行器端点。

您应该期待类似于下面所示的响应,

{
  "MyStatus": "is happy"
}

可以找到一个完整的工作示例here

答案 1 :(得分:-2)

像这样提供您自己的@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

这将完全覆盖/health,而不仅仅是像自定义HealthIndicator那样将信息添加到现有/health中。问题是,您想要什么,因为@Endpoint(id = "health")和“我的意图不是创建自定义执行器myhealth,而是自定义现有的健康执行器”。但是您可以 在AcmeHealthEndpoint中使用现有的HealthEndpoint并完成这两项操作:

@Component
@WebEndpoint(id = "prettyhealth")
public class PrettyHealthEndpoint {

    private final HealthEndpoint healthEndpoint;
    private final ObjectMapper objectMapper;

    @Autowired
    public PrettyHealthEndpoint(HealthEndpoint healthEndpoint, ObjectMapper objectMapper) {
        this.healthEndpoint = healthEndpoint;
        this.objectMapper = objectMapper;
    }

    @ReadOperation(produces = "application/json")
    public String getHealthJson() throws JsonProcessingException {
        Health health = healthEndpoint.health();
        ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
        return writer.writeValueAsString(health);
    }

    @ReadOperation
    public String prettyHealth() throws JsonProcessingException {
        return "<html><body><pre>" + getHealthJson() + "</pre></body></html>";
    }
}