詳解spring-boot actuator(監(jiān)控)配置和使用
在生產(chǎn)環(huán)境中,需要實(shí)時(shí)或定期監(jiān)控服務(wù)的可用性。spring-boot 的actuator(監(jiān)控)功能提供了很多監(jiān)控所需的接口。簡(jiǎn)單的配置和使用如下:
1、引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
如果使用http調(diào)用的方式,還需要這個(gè)依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、配置:
application.yml中指定監(jiān)控的HTTP端口(如果不指定,則使用和server相同的端口);指定去掉某項(xiàng)的檢查(比如不監(jiān)控health.mail):
server: port: 8082 management: port: 54001 health: mail: enabled: false
3、使用:
查看health指標(biāo):http://localhost:54001/health
{"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}
4、自定義指標(biāo):
4.1 /health:在某個(gè)類中implements HealthIndicator接口,然后實(shí)現(xiàn)其中的health()方法即可:
代碼:
@SpringBootApplication
@EnableScheduling
public class MySpringBootApplication implements HealthIndicator{
private static Logger logger = LoggerFactory.getLogger(MySpringBootApplication.class);
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
logger.info("My Spring Boot Application Started");
}
/**
* 在/health接口調(diào)用的時(shí)候,返回多一個(gè)屬性:"mySpringBootApplication":{"status":"UP","hello":"world"}
*/
@Override
public Health health() {
return Health.up().withDetail("hello", "world").build();
}
}
/health 運(yùn)行結(jié)果(注意第二個(gè)指標(biāo)):
{"status":"UP","mySpringBootApplication":{"status":"UP","hello":"world"},"diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}
4.2 /info:配置如下,可以直接給一個(gè)字符串,也可以從pom.xml配置中獲取
info: app: name: "@project.name@" #從pom.xml中獲取 description: "@project.description@" version: "@project.version@" spring-boot-version: "@project.parent.version@"
/info的結(jié)果如下:
{"app":{"name":"my-spring-boot","description":"Test Project for Spring Boot","version":"1.0","spring-boot-version":"1.3.6.RELEASE"}}
官網(wǎng):http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
源代碼參考:https://github.com/xujijun/my-spring-boot
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot中的 6 種API請(qǐng)求參數(shù)讀取方式示例詳解
使用Spring Boot開發(fā)API的時(shí)候,讀取請(qǐng)求參數(shù)是服務(wù)端編碼中最基本的一項(xiàng)操作,Spring Boot中也提供了多種機(jī)制來滿足不同的API設(shè)計(jì)要求,這篇文章主要介紹了Spring Boot中的 6 種API請(qǐng)求參數(shù)讀取方式示例詳解,需要的朋友可以參考下2024-05-05
SpringBoot整合websocket實(shí)現(xiàn)即時(shí)通信聊天
這篇文章主要介紹了SpringBoot整合websocket實(shí)現(xiàn)即時(shí)通信聊天,實(shí)時(shí)通信是一個(gè)實(shí)時(shí)通信系統(tǒng),允許兩人或多人使用網(wǎng)絡(luò)實(shí)時(shí)的傳遞文字消息、文件、語音與視頻交流,需要的朋友可以參考下2022-05-05
Spring案例打印機(jī)的實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Spring案例打印機(jī)的實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
使用Sentinel實(shí)現(xiàn)流控和服務(wù)降級(jí)的代碼示例
Sentinel是面向分布式、多語言異構(gòu)化服務(wù)架構(gòu)的流量治理組件,本文將詳細(xì)為大家介紹如何使用Sentinel實(shí)現(xiàn)流控和服務(wù)降級(jí),文中有相關(guān)的代碼示例,需要的朋友可以參考下2023-05-05
Spring Boot快速實(shí)現(xiàn) IP地址解析的示例詳解
這篇文章主要介紹了Spring Boot快速實(shí)現(xiàn)IP地址解析,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Spring Boot 中實(shí)現(xiàn)跨域的多種方式小結(jié)
Spring Boot提供了多種方式來實(shí)現(xiàn)跨域請(qǐng)求,開發(fā)者可以根據(jù)具體需求選擇適合的方法,在配置時(shí),要確保不僅考慮安全性,還要兼顧應(yīng)用的靈活性和性能,本文給大家介紹Spring Boot 中實(shí)現(xiàn)跨域的多種方式,感興趣的朋友一起看看吧2024-01-01
MySQL 新增字段但 Java 實(shí)體未更新存在潛在問題與解決方案
在 Java + MySQL 的開發(fā)中,我們通常使用 ORM 框架(如 MyBatis、MyBatis-Plus、Hibernate)來映射數(shù)據(jù)庫表與 Java 對(duì)象,這篇文章主要介紹了MySQL 新增字段但 Java 實(shí)體未更新:潛在問題與解決方案,需要的朋友可以參考下2025-04-04

