【问题标题】:Spring Boot 2 Custom Actuator EndpointsSpring Boot 2 自定义执行器端点
【发布时间】:2018-03-20 15:12:09
【问题描述】:

春季菜鸟:好的。我从 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 错误。

我错过了什么?谢谢!

【问题讨论】:

    标签: java spring spring-boot endpoint actuator


    【解决方案1】:

    好的,解决了:

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

    缺少的是“@Component”注解。但是,这在文档中的什么位置?

    【讨论】:

      【解决方案2】:

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

      Source

      @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}:一个命名的配置 logger(使用名称@Selector)。

      ...

      编辑的问题导致结论that it should be a bean

      如果你添加了一个带有@Endpoint注解的@Bean,任何被注解的方法 @ReadOperation、@WriteOperation 或 @DeleteOperation 是 通过 JMX 自动公开,在 Web 应用程序中,通过 HTTP 自动公开为 好。端点可以使用 Jersey、Spring MVC 或 Spring WebFlux。

      或见comments in the announcement of the feature

      【讨论】:

      • 唉,将“/aname”、“?name=aname”添加到 URL 或完全删除 @Selector 引用都没有效果。
      • 我已更新以回答您更新后的问题,并参考了说明应该是 bean 的文档。
      【解决方案3】:

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

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

      spring-boot 2.0.3.RELEASE

      @Component
      @Endpoint(id = "heartbeat")
      public class HeartbeatEndpoint {
      
          @ReadOperation
          public String invoke() {
              return "";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-09-16
        • 2019-03-10
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 2021-04-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多