我正在尝试将 spring boot 样板项目升级到 Spring boot 2.0.0。 我遵循了官方迁移指南(this 和 this),但它无法公开执行器自定义端点。
我用这个虚拟端点进行了测试:
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/