javascript
Spring Boot Actuator:自定义端点,其顶部具有MVC层
Spring Boot Actuator端點允許您監視應用程序并與之交互。 Spring Boot包含許多內置端點,您也可以添加自己的端點。
添加自定義端點就像創建一個從org.springframework.boot.actuate.endpoint.AbstractEndpoint擴展的類一樣容易。 但是Spring Boot Actuator也提供了用MVC層裝飾端點的可能性。
端點端點
有許多內置端點,但是缺少一個端點是公開所有端點的端點。 默認情況下,終結點通過HTTP公開,其中終結點的ID映射到URL。 在下面的示例中,創建了具有ID endpoints的新端點,并且其invoke方法返回所有可用端點:
@Component public class EndpointsEndpoint extends AbstractEndpoint<List<Endpoint>> {private List<Endpoint> endpoints;@Autowiredpublic EndpointsEndpoint(List<Endpoint> endpoints) {super("endpoints");this.endpoints = endpoints;}@Overridepublic List<Endpoint> invoke() {return endpoints;} }@Component注釋將端點添加到現有端點列表中。 /endpoints URL現在將公開所有具有id , enabled和sensitive屬性的端點:
[{"id": "trace","sensitive": true,"enabled": true},{"id": "configprops","sensitive": true,"enabled": true} ]新端點還將作為MBean在JMX服務器上注冊: [org.springframework.boot:type=Endpoint,name=endpointsEndpoint]
MVC端點
Spring Boot Actuator提供了一項附加功能,該功能是通過org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint接口在終結點之上的MVC層的策略 。 MvcEndpoint可以使用@RequestMapping和其他Spring MVC功能。
請注意, EndpointsEndpoint返回所有可用的端點。 但是,如果用戶可以通過其enabled sensitive屬性過濾端點,那就太好了。
MvcEndpoint必須使用有效的@RequestMapping方法創建一個新的MvcEndpoint 。 請注意,不允許在類級別使用@Controller和@RequestMapping ,因此使用@Component來使端點可用:
@Component public class EndpointsMvcEndpoint extends EndpointMvcAdapter {private final EndpointsEndpoint delegate;@Autowiredpublic EndpointsMvcEndpoint(EndpointsEndpoint delegate) {super(delegate);this.delegate = delegate;}@RequestMapping(value = "/filter", method = RequestMethod.GET)@ResponseBodypublic Set<Endpoint> filter(@RequestParam(required = false) Boolean enabled,@RequestParam(required = false) Boolean sensitive) {} }新方法將在/endpoints/filter URL下可用。 該方法的實現很簡單:它獲取可選的enabled sensitive參數,并過濾委托的invoke方法結果:
@RequestMapping(value = "/filter", method = RequestMethod.GET) @ResponseBody public Set<Endpoint> filter(@RequestParam(required = false) Boolean enabled,@RequestParam(required = false) Boolean sensitive) { Predicate<Endpoint> isEnabled =endpoint -> matches(endpoint::isEnabled, ofNullable(enabled));Predicate<Endpoint> isSensitive =endpoint -> matches(endpoint::isSensitive, ofNullable(sensitive));return this.delegate.invoke().stream().filter(isEnabled.and(isSensitive)).collect(toSet()); }private <T> boolean matches(Supplier<T> supplier, Optional<T> value) {return !value.isPresent() || supplier.get().equals(value.get()); }用法示例:
- 所有啟用的端點: /endpoints/filter?enabled=true
- 所有敏感端點: /endpoints/filter?sensitive=true
- 所有已啟用和敏感的端點: /endpoints/filter?enabled=true&sensitive=true
使端點可發現
EndpointsMvcEndpoint使用MVC功能,但仍返回純終結點對象。 如果Spring HATEOAS在類路徑中,則可以擴展filter方法以返回org.springframework.hateoas.Resource以及指向端點的鏈接:
class EndpointResource extends ResourceSupport {private final String managementContextPath;private final Endpoint endpoint;EndpointResource(String managementContextPath, Endpoint endpoint) {this.managementContextPath = managementContextPath;this.endpoint = endpoint;if (endpoint.isEnabled()) {UriComponentsBuilder path = fromCurrentServletMapping().path(this.managementContextPath).pathSegment(endpoint.getId());this.add(new Link(path.build().toUriString(), endpoint.getId())); }}public Endpoint getEndpoint() {return endpoint;} }EndpointResource將包含指向每個已啟用端點的鏈接。 請注意,構造函數采用managamentContextPath變量。 該變量包含一個Spring Boot Actuator management.contextPath屬性值。 用于設置管理端點的前綴。
EndpointsMvcEndpoint類中所需的更改:
@Component public class EndpointsMvcEndpoint extends EndpointMvcAdapter {@Value("${management.context-path:/}") // default to '/'private String managementContextPath;@RequestMapping(value = "/filter", method = RequestMethod.GET)@ResponseBodypublic Set<Endpoint> filter(@RequestParam(required = false) Boolean enabled,@RequestParam(required = false) Boolean sensitive) {// predicates declarationsreturn this.delegate.invoke().stream().filter(isEnabled.and(isSensitive)).map(e -> new EndpointResource(managementContextPath, e)).collect(toSet());} }我的Chrome瀏覽器中安裝了JSON Formatter的結果:
但是,為什么不直接從EndpointsEnpoint返回資源呢? 在EndpointResource了從HttpServletRequest中提取信息的UriComponentsBuilder ,它將在調用MBean的getData操作時引發異常(除非不需要JMX)。
管理端點狀態
端點不僅可以用于監視,還可以用于管理。 已經有內置的ShutdownEndpoint (默認情況下禁用),可以關閉ApplicationContext 。 在以下(假設的)示例中,用戶可以更改所選端點的狀態:
@RequestMapping(value = "/{endpointId}/state") @ResponseBody public EndpointResource enable(@PathVariable String endpointId) {Optional<Endpoint> endpointOptional = this.delegate.invoke().stream().filter(e -> e.getId().equals(endpointId)).findFirst();if (!endpointOptional.isPresent()) {throw new RuntimeException("Endpoint not found: " + endpointId);}Endpoint endpoint = endpointOptional.get(); ((AbstractEndpoint) endpoint).setEnabled(!endpoint.isEnabled());return new EndpointResource(managementContextPath, endpoint); }呼叫disabled端點用戶時,應收到以下響應:
{"message": "This endpoint is disabled" }更進一步
下一步可能是為自定義(或現有)端點添加用戶界面,但這不在本文討論范圍之內。 如果您有興趣,可以看看Spring Boot Admin ,它是Spring Boot應用程序的簡單管理界面。
摘要
Spring Boot Actuator 提供了Spring Boot的所有生產就緒功能以及許多內置端點。 只需花費很少的精力,即可添加自定義端點,以擴展應用程序的監視和管理功能。
參考文獻
- http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
翻譯自: https://www.javacodegeeks.com/2014/10/spring-boot-actuator-custom-endpoint-with-mvc-layer-on-top-of-it.html
總結
以上是生活随笔為你收集整理的Spring Boot Actuator:自定义端点,其顶部具有MVC层的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么网络测速用电脑(电脑网络怎么测速?)
- 下一篇: 路由器可以再接路由器吗?