javascript
SpringBoot--->>>指标监控-->>定制 Endpoint 信息
定制 Endpoint 信息
之前的 endpoint 反饋信息很多,但是實(shí)際上程序肯定有某些流程會反饋數(shù)據(jù)出來以表示程序正常運(yùn)行。所以需要自己定義 Endpoint 反饋的信息
1、定制 health Endpoint 信息
根據(jù)官網(wǎng)說法定制 health 信息,可以通過實(shí)現(xiàn) HealthIndicator 接口,提供 health() 方法,里面調(diào)用 withDetail() 方法,設(shè)置詳細(xì)信息。但是呢他是個接口,就進(jìn)去看看類結(jié)構(gòu)圖,有沒有實(shí)現(xiàn)類。發(fā)現(xiàn)有一個實(shí)現(xiàn)類 AbstractHealthIndicator ,它只要實(shí)現(xiàn) doHealthCheck() 方法,判斷邏輯并設(shè)置 health 信息
繼承 AbstractHealthIndicator 類
這個類要是組件,它的 endpointName 就是 HealthIndicator 前面那一段首字母小寫,所以要注意命名
@Component public class MyProjectHealthIndicator extends AbstractHealthIndicator {/*** 健康檢查方法,里面對邏輯進(jìn)行檢查,并設(shè)置 health 信息* @param builder* @throws Exception*/@Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {Map<Object,Object> mes = new HashMap<>();// 進(jìn)行判斷邏輯if (1==1){//直接反饋 health 狀態(tài)為 UP // builder.up();//第二種方式builder.status(Status.UP);mes.put("code","666");mes.put("mess","邏輯通過");}else {//設(shè)置 health 狀態(tài)為 DOWN // builder.down();//第二種方式builder.status(Status.OUT_OF_SERVICE);mes.put("code","000");mes.put("mess","邏輯不通過");}//結(jié)果builder.withDetail("num",789).withDetail("詳細(xì)信息",mes);} }結(jié)果
-0-
實(shí)現(xiàn) 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() {// 這里寫邏輯,結(jié)果返回判斷return 0;} }2、定制 info Endpoint 信息
info 端點(diǎn)的作用是顯示程序信息,有兩種方式定制 info 信息
1、配置文件
info:env:enabled: true # 自定義 info 端點(diǎn)的先決條件是 啟用 management.info.env.enabled info:app:name: 菠蘿風(fēng)車version: 1.0.0.0encoding: UTF-8java:resurce: 8結(jié)果
-0-
或者,直接去 pom 文件中拿當(dāng)前程序的名稱、版本
使用 @@ 在中間寫想要拿到的內(nèi)容,根據(jù)pom文件的標(biāo)簽層級
info:appName: @project.artifactId@appVersion: @project.version@-0-
2、實(shí)現(xiàn) InfoContributor 接口
官網(wǎng):
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.info
實(shí)現(xiàn)接口提供自定義應(yīng)用程序信息
info 的命名跟類名沒啥關(guān)系
@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 信息
官網(wǎng):可以看到 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 信息,以配置類方式,添加組件。默認(rèn)情況下,來自所有MeterBinderbean 的指標(biāo)會自動綁定到 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 聲明類成為一個自定義端點(diǎn),用注解 @ReadOperation、@WriteOperation 修飾方法。自定義 Endpoint 會自動通過 JMX 公開,并且在 Web 應(yīng)用程序中,也會通過 HTTP 公開。
類里面
@Component @Endpoint(id = "myEndpoint") public class MyProjectEndpoint {@ReadOperationpublic Map getJDBCConnect(){return Collections.singletonMap("read...","success");}@WriteOperationpublic void exceptionHandle(){System.out.println("出現(xiàn)了問題");}}結(jié)果
訪問結(jié)果
在 jconsole 中對這兩個方法執(zhí)行,會得到不同的反應(yīng)。操作 exceptionHandle 就會輸出內(nèi)容。
總結(jié)
以上是生活随笔為你收集整理的SpringBoot--->>>指标监控-->>定制 Endpoint 信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity——网络协议
- 下一篇: 3dsmax2010 快捷键