日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot Actuator:在其顶部具有MVC层的自定义端点

發(fā)布時間:2023/12/3 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot Actuator:在其顶部具有MVC层的自定义端点 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Spring Boot Actuator端點(diǎn)允許您監(jiān)視應(yīng)用程序并與之交互。 Spring Boot包含許多內(nèi)置端點(diǎn),您也可以添加自己的端點(diǎn)。

添加自定義端點(diǎn)就像創(chuàng)建一個從org.springframework.boot.actuate.endpoint.AbstractEndpoint擴(kuò)展的類一樣容易。 但是Spring Boot Actuator也提供了用MVC層裝飾端點(diǎn)的可能性。

端點(diǎn)端點(diǎn)

有許多內(nèi)置端點(diǎn),但是缺少一個端點(diǎn)是公開所有端點(diǎn)的端點(diǎn)。 默認(rèn)情況下,端點(diǎn)通過HTTP公開,其中端點(diǎn)ID映射到URL。 在下面的示例中,創(chuàng)建了具有ID endpoints的新端點(diǎn),并且其invoke方法返回所有可用端點(diǎn):

@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注釋將端點(diǎn)添加到現(xiàn)有端點(diǎn)列表中。 /endpoints URL現(xiàn)在將公開所有具有id , enabled和sensitive屬性的端點(diǎn):

[{"id": "trace","sensitive": true,"enabled": true},{"id": "configprops","sensitive": true,"enabled": true} ]

新端點(diǎn)還將作為MBean在JMX服務(wù)器上注冊: [org.springframework.boot:type=Endpoint,name=endpointsEndpoint]

MVC端點(diǎn)

Spring Boot Actuator提供了一項(xiàng)附加功能,該功能是通過org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint接口在終結(jié)點(diǎn)之上的MVC層的策略 。 MvcEndpoint可以使用@RequestMapping和其他Spring MVC功能。

請注意, EndpointsEndpoint返回所有可用的端點(diǎn)。 但是,如果用戶可以通過其enabled sensitive屬性過濾端點(diǎn),那就太好了。

MvcEndpoint必須使用有效的@RequestMapping方法創(chuàng)建一個新的MvcEndpoint 。 請注意,不允許在類級別使用@Controller和@RequestMapping ,因此使用@Component來使端點(diǎn)可用:

@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下可用。 該方法的實(shí)現(xiàn)很簡單:它獲取可選的enabled sensitive參數(shù),并過濾委托的invoke方法結(jié)果:

@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()); }

用法示例:

  • 所有啟用的端點(diǎn): /endpoints/filter?enabled=true
  • 所有敏感端點(diǎn): /endpoints/filter?sensitive=true
  • 所有已啟用和敏感的端點(diǎn): /endpoints/filter?enabled=true&sensitive=true

使端點(diǎn)可發(fā)現(xiàn)

EndpointsMvcEndpoint使用MVC功能,但仍返回普通端點(diǎn)對象。 如果Spring HATEOAS在類路徑中,則可以擴(kuò)展filter方法以返回org.springframework.hateoas.Resource以及指向端點(diǎn)的鏈接:

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將包含指向每個已啟用端點(diǎn)的鏈接。 請注意,構(gòu)造函數(shù)采用managamentContextPath變量。 該變量包含一個Spring Boot Actuator management.contextPath屬性值。 用于設(shè)置管理端點(diǎn)的前綴。

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());} }

在裝有JSON Formatter的Chrome瀏覽器中的結(jié)果:

但是,為什么不直接從EndpointsEnpoint返回資源呢? 在EndpointResource了從HttpServletRequest中提取信息的UriComponentsBuilder ,它將在調(diào)用MBean的getData操作時引發(fā)異常(除非不需要JMX)。

管理端點(diǎn)狀態(tài)

端點(diǎn)不僅可以用于監(jiān)視,還可以用于管理。 已經(jīng)有內(nèi)置的ShutdownEndpoint (默認(rèn)情況下禁用),可以關(guān)閉ApplicationContext 。 在以下(假設(shè)的)示例中,用戶可以更改所選端點(diǎn)的狀態(tài):

@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端點(diǎn)時,用戶應(yīng)收到以下響應(yīng):

{"message": "This endpoint is disabled" }

更進(jìn)一步

下一步可能是為自定義(或現(xiàn)有)端點(diǎn)添加用戶界面,但這不在本文討論范圍之內(nèi)。 如果您有興趣,可以看看Spring Boot Admin ,它是Spring Boot應(yīng)用程序的簡單管理界面。

摘要

Spring Boot Actuator 提供了Spring Boot的所有生產(chǎn)就緒功能以及許多內(nèi)置端點(diǎn)。 只需花費(fèi)很少的精力,即可添加自定義端點(diǎn),以擴(kuò)展應(yīng)用程序的監(jiān)視和管理功能。

參考資料

  • 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

總結(jié)

以上是生活随笔為你收集整理的Spring Boot Actuator:在其顶部具有MVC层的自定义端点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。