一、指標(biāo)監(jiān)控
引入jar包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency>
以web方式開啟:
#開啟全部的 management.endpoints.enabled-by-default=true #web 方式暴露 management.endpoints.web.exposure.include=*
二、常用的監(jiān)控端點(diǎn)
看這個(gè):傳送門
最常用的:
health:健康狀況,查看應(yīng)用是否可用
metrics:
運(yùn)行時(shí)指標(biāo),JVM、線程等相關(guān)內(nèi)容(重要)
loggers:
日志記錄
三、定制EndPoint
定制組件健康信息,比較簡單,同時(shí)也可以實(shí)現(xiàn)接口方式:
package com.example.demo; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; /** * @author Administrator */ @Component public class MyComHealthIndicator extends AbstractHealthIndicator { /** * 真實(shí)的檢查方法 * @param builder * @throws Exception */ @Override protected void doHealthCheck(Health.Builder builder) throws Exception { Map<String, Object> map = new HashMap<>(); if(1==1){ builder.up(); map.put("count", 1); map.put("msg", "健康"); }else{ builder.down(); map.put("msg", "超時(shí)"); } builder.withDetail("code", 100) .withDetails(map); } }
INFO Endpoint 的定義:
1、配置文件直接定義:
info.mavenProjectName = @project.artifactId@ [email protected]@
2、寫代碼:
package com.example.demo; import org.springframework.boot.actuate.info.Info; import org.springframework.boot.actuate.info.InfoContributor; import org.springframework.stereotype.Component; @Component public class AppInfo implements InfoContributor { @Override public void contribute(Info.Builder builder) { builder.withDetail("msg", "真他嗎帥!"); } }
metrics定制endpoint,直接使用MeterRegistry。
自定義Endpoint,監(jiān)控端點(diǎn):
package com.example.demo; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.Map; @Component @Endpoint(id = "myEndPoint") public class EndPoint { @ReadOperation public Map<String, Object> read(){ return Collections.singletonMap("MG", "MG GOGO"); } @WriteOperation public void write(){ System.out.println("累"); } }
訪問自定義的指標(biāo)的時(shí)候,訪問的就是read方法
四、spring boot admin(可以使用)
準(zhǔn)備一個(gè) server,會(huì)定時(shí)去獲取各個(gè)服務(wù)的相關(guān)內(nèi)容。
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency>
客戶端注冊(cè):
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency>
配置屬性文件:
spring: application: name: admin-client boot: admin: client: url: http://localhost:8769 interface:#使用IP注冊(cè) prefer-ip: ture server: port: 8768 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
到此這篇關(guān)于spring boot 監(jiān)控的文章就介紹到這了,更多相關(guān)spring boot 監(jiān)控內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/liming0025/article/details/120980720