5

春天菜鸟:好的。我从 STS Spring Starter Project / Maven / Java 8 / Spring Boot 2.0 开始,然后选择 Web 和 Actuator 依赖项。它构建并运行良好,并响应http://localhost:8080/actuator/health。我在主应用程序类中添加了一个“端点”,使其看起来像这样。

package com.thumbsup;

import org.springframework.boot.SpringApplication;
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.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class YourStash11Application {

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

    @Endpoint(id="mypoint")
    public class CustomPoint {
        @ReadOperation
        public String getHello(){
            return "Hello" ;
        }
    }

}

我尝试启用 application.properties 中的所有内容:

management.endpoints.enabled-by-default=true
management.endpoint.conditions.enabled=true
management.endpoint.mypoint.enabled=true
management.endpoints.web.exposure.include=*

但是当它构建时,没有引用映射/actuator/mypoint,并且
http://localhost:8080/actuator/mypoint
http://localhost:8080/application/mypoint
都返回404错误。

我错过了什么?谢谢!

4

3 回答 3

12

好的,解决了:

    @Endpoint(id="mypoint")
    @Component
    public class myPointEndPoint {
        @ReadOperation
        public String mypoint(){
            return "Hello" ;
        }
    }

缺少的是“@Component”注释。但是,这是在文档中的什么地方?

于 2018-03-20T15:46:54.963 回答
0

也许这会对某人有所帮助。

看起来@ReadOperation不支持返回类型Voidinvoke您应该在您的方法中至少返回一个空字符串。

spring-boot 2.0.3.RELEASE

@Component
@Endpoint(id = "heartbeat")
public class HeartbeatEndpoint {

    @ReadOperation
    public String invoke() {
        return "";
    }
}
于 2018-07-10T15:45:44.810 回答
0

最初的部分问题是代码没有添加没有“选择器”的端点

资源

@Endpoint(id = "loggers")
@Component
public class LoggersEndpoint {

    @ReadOperation
    public Map<String, Object> loggers() { ... }

    @ReadOperation
    public LoggerLevels loggerLevels(@Selector String name) { ... }

    @WriteOperation
    public void configureLogLevel(@Selector String name, LogLevel configuredLevel) { ... }

}

此端点公开三个操作:

GET on /application/loggers:所有记录器的配置(因为它没有“选择器”参数):

GET on /application/loggers/{name}:命名记录器的配置(使用名称@Selector)。

...

编辑后的问题得出的结论是它应该是一个 bean

如果添加带有@Endpoint 注释的@Bean,则任何带有@ReadOperation、@WriteOperation 或@DeleteOperation 注释的方法都会自动通过JMX 公开,并且在Web 应用程序中,也可以通过HTTP 公开。端点可以使用 Jersey、Spring MVC 或 Spring WebFlux 通过 HTTP 公开。

或查看功能公告中的评论

于 2018-03-20T15:24:58.317 回答