javascript
SpringBoot--->>>指标监控-->>定制 Endpoint 信息
定制 Endpoint 信息
之前的 endpoint 反饋信息很多,但是實際上程序肯定有某些流程會反饋數據出來以表示程序正常運行。所以需要自己定義 Endpoint 反饋的信息
1、定制 health Endpoint 信息
根據官網說法定制 health 信息,可以通過實現 HealthIndicator 接口,提供 health() 方法,里面調用 withDetail() 方法,設置詳細信息。但是呢他是個接口,就進去看看類結構圖,有沒有實現類。發現有一個實現類 AbstractHealthIndicator ,它只要實現 doHealthCheck() 方法,判斷邏輯并設置 health 信息
繼承 AbstractHealthIndicator 類
這個類要是組件,它的 endpointName 就是 HealthIndicator 前面那一段首字母小寫,所以要注意命名
@Component public class MyProjectHealthIndicator extends AbstractHealthIndicator {/*** 健康檢查方法,里面對邏輯進行檢查,并設置 health 信息* @param builder* @throws Exception*/@Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {Map<Object,Object> mes = new HashMap<>();// 進行判斷邏輯if (1==1){//直接反饋 health 狀態為 UP // builder.up();//第二種方式builder.status(Status.UP);mes.put("code","666");mes.put("mess","邏輯通過");}else {//設置 health 狀態為 DOWN // builder.down();//第二種方式builder.status(Status.OUT_OF_SERVICE);mes.put("code","000");mes.put("mess","邏輯不通過");}//結果builder.withDetail("num",789).withDetail("詳細信息",mes);} }結果
-0-
實現 HealthIndicator 接口
@Component public class MyHealthIndicator implements HealthIndicator {@Overridepublic Health health() {int errorCode = check();if (errorCode != 0) {return Health.down().withDetail("Error Code", errorCode).build();}return Health.up().build();}private int check() {// 這里寫邏輯,結果返回判斷return 0;} }2、定制 info Endpoint 信息
info 端點的作用是顯示程序信息,有兩種方式定制 info 信息
1、配置文件
info:env:enabled: true # 自定義 info 端點的先決條件是 啟用 management.info.env.enabled info:app:name: 菠蘿風車version: 1.0.0.0encoding: UTF-8java:resurce: 8結果
-0-
或者,直接去 pom 文件中拿當前程序的名稱、版本
使用 @@ 在中間寫想要拿到的內容,根據pom文件的標簽層級
info:appName: @project.artifactId@appVersion: @project.version@-0-
2、實現 InfoContributor 接口
官網:
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.info
實現接口提供自定義應用程序信息
info 的命名跟類名沒啥關系
@Component public class MyProjectInfoContributor implements InfoContributor {@Overridepublic void contribute(Info.Builder builder) {builder.withDetail("appName1","PAPA").withDetail("appVersion1","1,2,2").withDetails(Collections.singletonMap("haha",123));} }3、定制 metrics 信息
官網:可以看到 Spring 支持自動適配的 metrics 類型
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics
自定義 metrics 信息
操作 MeterRegistry 對象可以添加自定義 metrics
@Component public class MyService {Counter counter;public MyService(MeterRegistry meterRegistry) {counter = meterRegistry.counter("這是自定義的metric");counter.increment();}}或者使用 MeterBinder 注冊 metrics 信息,以配置類方式,添加組件。默認情況下,來自所有MeterBinderbean 的指標會自動綁定到 Spring-managed MeterRegistry。
@Configuration public class MetricsConfiguration {@Beanpublic MeterBinder heihei(Queue queue){return (registry) -> Gauge.builder("this is newMetrics", queue::size).register(registry);}}4、定制 Endpoint
可以使用注解 @Endpoint 聲明類成為一個自定義端點,用注解 @ReadOperation、@WriteOperation 修飾方法。自定義 Endpoint 會自動通過 JMX 公開,并且在 Web 應用程序中,也會通過 HTTP 公開。
類里面
@Component @Endpoint(id = "myEndpoint") public class MyProjectEndpoint {@ReadOperationpublic Map getJDBCConnect(){return Collections.singletonMap("read...","success");}@WriteOperationpublic void exceptionHandle(){System.out.println("出現了問題");}}結果
訪問結果
在 jconsole 中對這兩個方法執行,會得到不同的反應。操作 exceptionHandle 就會輸出內容。
總結
以上是生活随笔為你收集整理的SpringBoot--->>>指标监控-->>定制 Endpoint 信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity——网络协议
- 下一篇: gradle idea java ssm