[英]Spring Boot 2 Custom Actuator Endpoints
春天Noob:好的。 我從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=*
但是當它構建時,沒有參考映射/執行器/ mypoint,和
http:// localhost:8080 / actuator / mypoint和
HTTP://本地主機:8080 /應用/ mypoint
都返回404錯誤。
我錯過了什么? 謝謝!
好的,解決了:
@Endpoint(id="mypoint")
@Component
public class myPointEndPoint {
@ReadOperation
public String mypoint(){
return "Hello" ;
}
}
缺少的是“@Component”注釋。 但是,這在文檔中的位置是什么?
最初的部分問題是代碼沒有添加沒有“選擇器”的端點
@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:所有記錄器的配置(因為它沒有“selector”參數):
GET on / application / loggers / {name}:命名記錄器的配置(使用名稱@Selector)。
...
編輯過的問題得出的結論是它應該是一個bean
如果添加使用@Endpoint注釋的@Bean,則使用@ReadOperation,@ WritOperation或@DeleteOperation注釋的任何方法都將通過JMX自動公開,並且在Web應用程序中也通過HTTP公開。 可以使用Jersey,Spring MVC或Spring WebFlux通過HTTP公開端點。
也許這會對某人有所幫助。
看起來@ReadOperation
不支持返回類型Void
。 您應該在invoke
方法上至少返回一個空字符串。
spring-boot 2.0.3.RELEASE
@Component
@Endpoint(id = "heartbeat")
public class HeartbeatEndpoint {
@ReadOperation
public String invoke() {
return "";
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.