spring-boot - 具有 spring boot 2.0.0 的样板项目未公开自定义执行器端点

标签 spring-boot spring-boot-actuator

我正在尝试将 spring boot 样板项目升级到 Spring boot 2.0.0。 我遵循了官方迁移指南(thisthis),但它无法公开执行器自定义端点

我用这个虚拟端点进行了测试:

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Endpoint(id="testing-user")
public class ActiveUsersEndpoint {

private final Map<String, User> users = new HashMap<>();

ActiveUsersEndpoint() {
    this.users.put("A", new User("Abcd"));
    this.users.put("E", new User("Fghi"));
    this.users.put("J", new User("Klmn"));
}

@ReadOperation
public List getAll() {
    return new ArrayList(this.users.values());
}

@ReadOperation
public User getActiveUser(@Selector String user) {
    return this.users.get(user);
}

public static class User {
    private String name;

    User(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
}

如果直接从子项目公开端点,则端点工作良好,但如果端点从作为依赖项添加的父样板项目公开,则端点不起作用。

在我的 application.yml 中,我添加了:

management:
    endpoints:
        web:
            base-path: /
            exposure:
                include: '*'

可用的资源不多,而那些可用的资源也无济于事。

最佳答案

找到答案。

不是使用 @Component 创建 bean,而是使用一个配置文件来创建端点的所有 bean。例如,配置文件可能如下所示:

@ManagementContextConfiguration
public class HealthConfiguration {

@Bean
public ActiveUsersEndpoint activeUsersEndpoint() {
    return new ActiveUsersEndpoint();
}
// Other end points if needed...
}

重要的是在资源中有 spring.factories 文件。 该文件将指向您在其中创建所有端点的 bean 的配置文件: org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=com.foo.bar.HealthConfiguration

关于spring-boot - 具有 spring boot 2.0.0 的样板项目未公开自定义执行器端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49446143/

相关文章:

java - 如果应用程序使用不同于 MySQL 的时区,则 Hibernate 保存/检索日期减去天数

spring - 从 Spring boot 1.2 升级到 1.5.2 后,Tomcat 8.5 启动期间出现 FileNotFoundException

spring-security - 在不丢失可配置端点的情况下覆盖 Spring 安全执行器

java - 一个应用程序是否可以使用 Spring 框架的任何功能来检查另一个应用程序的运行状况?

java - 如何拦截自己创建的JdbcTemplate实例

spring-boot - Spring Boot 不加载 application.yml 配置或?

java - spring.jpa.hibernate.hbm2ddl 和 spring.jpa.hibernate.ddl 的区别

spring-boot-actuator - 动态代码评估 : Unsafe Deserialization (Spring Boot 2) - how to avoid actuator related fortify issue, 还是误报?

spring-boot - 如何使用 spring boot 2 将 kafka 指标公开给/actuator/metrics

spring-boot - 将Spring Boot执行器指标(和Dropwizard指标)导出到Statsd