首頁(yè) 資訊 SpringBoot之旅

SpringBoot之旅

來(lái)源:泰然健康網(wǎng) 時(shí)間:2024年12月21日 23:35

相關(guān)文章

Spring Boot 相關(guān)文章目錄

前言

You build it,You run it, 當(dāng)我們編寫(xiě)的項(xiàng)目上線后,為了能第一時(shí)間知曉該項(xiàng)目是否出現(xiàn)問(wèn)題,常常對(duì)項(xiàng)目進(jìn)行健康檢查及一些指標(biāo)進(jìn)行監(jiān)控。
Spring Boot-Actuator 就是幫助我們監(jiān)控我們的Spring Boot 項(xiàng)目的。

使用

Spring Boot 最主要的特性就是AutoConfig(自動(dòng)配置),而對(duì)于我們這些使用者來(lái)說(shuō)也就是各種starter,
Spring Boot-Actuator 也提供了starter,為我們自動(dòng)配置,在使用上我們只需要添加starter到我們的依賴中,然后啟動(dòng)項(xiàng)目即可。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

常用Endpoint

Spring Boot-actuator,提供了許多有用的EndPoint,對(duì)Spring Boot應(yīng)用提供各種監(jiān)控,下面說(shuō)一下我常用的EndPoint:

/health 應(yīng)用的健康狀態(tài)
/configprops 獲取應(yīng)用的配置信息,因?yàn)镾pring Boot 可能發(fā)布時(shí)是單獨(dú)的Jar包,配置文件可能包含其中, 當(dāng)我們需要檢查配置文件時(shí)可以使用 ConfigpropsEndPoint 進(jìn)行查看一些配置是否正確。
/trace 最近幾次的http請(qǐng)求信息

HealthEndPoint

當(dāng)我們?cè)L問(wèn) http://localhost:8088/health 時(shí),可以看到 HealthEndPoint 給我們提供默認(rèn)的監(jiān)控結(jié)果,包含 磁盤(pán)檢測(cè)和數(shù)據(jù)庫(kù)檢測(cè)。

{ "status": "UP", "diskSpace": { "status": "UP", "total": 398458875904, "free": 315106918400, "threshold": 10485760 }, "db": { "status": "UP", "database": "MySQL", "hello": 1 } }

其實(shí)看 Spring Boot-actuator 源碼,你會(huì)發(fā)現(xiàn) HealthEndPoint 提供的信息不僅限于此,org.springframework.boot.actuate.health 包下 你會(huì)發(fā)現(xiàn) ElasticsearchHealthIndicator、RedisHealthIndicator、RabbitHealthIndicator 等
也就是 HealthEndPoint 也提供 ES, Redis 等組件的健康信息。

自定義Indicator 擴(kuò)展 HealthEndPoint

看源碼 其實(shí) 磁盤(pán)和數(shù)據(jù)庫(kù)健康信息就是 DiskSpaceHealthIndicator、DataSourceHealthIndicator 來(lái)實(shí)現(xiàn)的,當(dāng)我們對(duì)一些我們自定義的組件進(jìn)行監(jiān)控時(shí), 我們也可以實(shí)現(xiàn)個(gè)Indicator :

@Component public class User implements HealthIndicator { /** * user監(jiān)控 訪問(wèn): http://localhost:8088/health * * @return 自定義Health監(jiān)控 */ @Override public Health health() { return new Health.Builder().withDetail("usercount", 10) //自定義監(jiān)控內(nèi)容 .withDetail("userstatus", "up").up().build(); } }

這時(shí)我們?cè)俅卧L問(wèn): http://localhost:8088/health 這時(shí)返回的結(jié)果如下,包含了我們自定義的 User 健康信息。

{ "status": "UP", "user": { "status": "UP", "usercount": 10, "userstatus": "up" }, "diskSpace": { "status": "UP", "total": 398458875904, "free": 315097989120, "threshold": 10485760 }, "db": { "status": "UP", "database": "MySQL", "hello": 1 } }

自定義EndPoint

其實(shí)除了擴(kuò)展 HealthEndPoint 來(lái)添加一些健康檢查, 我們也可以自定定義一些EndPoint 來(lái)提供程序運(yùn)行時(shí)一些信息的展示:

@Configuration public class EndPointAutoConfig { @Bean public Endpoint<Map<String, Object>> customEndPoint() { return new SystemEndPoint(); } } @ConfigurationProperties(prefix="endpoints.customsystem") public class SystemEndPoint extends AbstractEndpoint<Map<String, Object>> { public SystemEndPoint(){ super("customsystem"); } @Override public Map<String, Object> invoke() { Map<String,Object> result= new HashMap<>(); Map<String, String> map = System.getenv(); result.put("username",map.get("USERNAME")); result.put("computername",map.get("COMPUTERNAME")); result.put("userdomain",map.get("USERDOMAIN")); return result; } }

訪問(wèn) http://localhost:8088/customsystem 來(lái)查看我們自定義的EndPoint ,返回結(jié)果如下:

{ "username": "xxx", "userdomain": "DESKTOP-6EAN1H4", "computername": "DESKTOP-6EAN1H4" }

相關(guān)知識(shí)

運(yùn)動(dòng)健康數(shù)據(jù)可視化:基于 Echarts 和 Java SpringBoot 的動(dòng)態(tài)實(shí)時(shí)大屏范例
基于SpringBoot Vue健康健身追蹤系統(tǒng) – 計(jì)算機(jī)源碼
健康之旅(健康之旅最新消息)
基于SpringBoot+Vue健身房管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)(源碼+部署說(shuō)明+演示視頻+源碼介紹)(2)
旅行:心靈的療愈之旅
瑜伽之旅
峽山之旅:發(fā)現(xiàn)當(dāng)下最獨(dú)特的療愈之旅
長(zhǎng)大之前去旅行
徒步之旅:從疲憊到自由的心靈之旅
康養(yǎng)旅游的概念追逐健康之旅體驗(yàn)自然之美

網(wǎng)址: SpringBoot之旅 http://www.u1s5d6.cn/newsview709765.html

推薦資訊