一、指標監控
引入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=*
二、常用的監控端點
看這個:傳送門
最常用的:
health:健康狀況,查看應用是否可用
metrics:
運行時指標,JVM、線程等相關內容(重要)
loggers:
日志記錄
三、定制EndPoint
定制組件健康信息,比較簡單,同時也可以實現接口方式:
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 { /** * 真實的檢查方法 * @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", "超時"); } builder.withDetail("code", 100) .withDetails(map); } }
INFO Endpoint 的定義:
1、配置文件直接定義:
info.mavenProjectName = @project.artifactId@ info.mavenProjectVersion=@project.version@
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,監控端點:
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("累"); } }
訪問自定義的指標的時候,訪問的就是read方法
四、spring boot admin(可以使用)
準備一個 server,會定時去獲取各個服務的相關內容。
<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> </dependency>
客戶端注冊:
<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注冊 prefer-ip: ture server: port: 8768 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
到此這篇關于spring boot 監控的文章就介紹到這了,更多相關spring boot 監控內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/liming0025/article/details/120980720