Spring Boot Actuator中的自定义端点

时间:2015-08-30 07:50:13

标签: spring-boot spring-boot-actuator

我正在尝试为Spring Boot应用程序编写自定义端点。我已经编写了自定义端点实现,如下所示。我还没有包含像导入这样的额外内容来减少代码的大小。

@Component
public class TestendPoint implements Endpoint<List<String>>{

    public String getId() {
        return "test";      
    }

    public List<String> invoke() {
        List<String> test= new ArrayList<String>();
        test.add("Details 1");
        test.add("Details 2");
        return serverDetails;
    }

    public boolean isEnabled() {        
        return true;
    }

    public boolean isSensitive() {      
        return false;
    }

}

一旦我编写了上面的代码,我就重新启动了应用程序并从/ test访问端点。但是,端点不可用。以下是Spring Boot启动应用程序。

@Configuration
@EnableAutoConfiguration
public class Application{

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

除此之外,我还有一切可以运行Spring Boot Actuator。我可以访问默认端点,如/ info,/ metrics等。

如果我在这里遗漏任何东西,你能否分享你的知识?我假设将加载自定义端点类,而无需来自开发人员的进一步配置。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。首先,我没有在应用程序类中包含@ComponentScan注释。或者要么我应该有@SpringBoot应用程序。当我添加注释时,我开始得到以下异常:

Caused by: java.lang.IllegalStateException: Could not evaluate condition owing to internal class not found. This can happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:50)
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:92)
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:79)
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:62)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:361)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:345)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:278)
    ... 18 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
    at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:308)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:129)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.anyMatches(SpringBootCondition.java:112)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$DatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:333)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:44)
    ... 24 more

这是由于包裹。我试过从root包中执行main类。我不确定这是否是Spring启动应用程序中的错误,我会在他们的站点中记录JIRA。一旦我添加了一个更高级别的包结构,它就开始正常工作了。