具有 Spring 引导 2.0.0 的样板项目,不公开自定义执行器端点



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

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

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;
    }
}
}

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

在我的应用程序.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

最新更新