深入理解spring boot 監(jiān)控
一、指標(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@ 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,監(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,會定時(shí)去獲取各個(gè)服務(wù)的相關(guān)內(nèi)容。
<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
到此這篇關(guān)于spring boot 監(jiān)控的文章就介紹到這了,更多相關(guān)spring boot 監(jiān)控內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
細(xì)致解讀希爾排序算法與相關(guān)的Java代碼實(shí)現(xiàn)
這篇文章主要介紹了希爾排序算法與相關(guān)的Java代碼實(shí)現(xiàn),希爾排序的時(shí)間復(fù)雜度根據(jù)步長序列的不同而不同,需要的朋友可以參考下2016-05-05
java組件smartupload實(shí)現(xiàn)上傳文件功能
這篇文章主要為大家詳細(xì)介紹了java組件smartupload實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
springboot學(xué)習(xí)之Thymeleaf模板引擎及原理介紹
本文主要介紹一下SpringBoot給我們推薦的Thymeleaf模板引擎,這模板引擎呢,是一個(gè)高級語言的模板引擎,他的這個(gè)語法更簡單而且功能更強(qiáng)大,對springboot?Thymeleaf模板引擎相關(guān)知識感興趣的朋友一起看看吧2022-02-02
詳細(xì)說明關(guān)于Java的數(shù)據(jù)庫連接(JDBC)
這篇文章主要介紹了詳細(xì)說明關(guān)于Java的數(shù)據(jù)庫連接JDBC,JDBC是用Java語言向數(shù)據(jù)庫發(fā)送SQL語句,需要的朋友可以參考下面文章內(nèi)容2021-09-09
SpringBoot+Ajax+redis實(shí)現(xiàn)隱藏重要接口地址的方法
這篇文章主要介紹了SpringBoot+Ajax+redis實(shí)現(xiàn)隱藏重要接口地址,本篇文章主要講訴使用SpringBoot項(xiàng)目配合Ajax和redis實(shí)現(xiàn)隱藏重要接口地址,這里我以隱藏秒殺地址為例,需要的朋友可以參考下2024-03-03
Spring中使用自定義ThreadLocal存儲導(dǎo)致的坑及解決
這篇文章主要介紹了Spring中使用自定義ThreadLocal存儲導(dǎo)致的坑及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn)
這篇文章主要介紹了Java日期工具類操作字符串Date和LocalDate互轉(zhuǎn),文章首先通過需要先引入坐標(biāo)展開主題的相關(guān)內(nèi)容介紹,需要的朋友可以參一下2022-06-06

