国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

SpringBoot實現項目健康檢查與監控

2024-07-14 08:41:09
字體:
來源:轉載
供稿:網友

Spring Boot 最主要的特性就是AutoConfig(自動配置),而對于我們這些使用者來說也就是各種starter,

Spring Boot-Actuator 也提供了starter,為我們自動配置,在使用上我們只需要添加starter到我們的依賴中,然后啟動項目即可。

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

常用Endpoint

Spring Boot-actuator,提供了許多有用的EndPoint,對Spring Boot應用提供各種監控,下面說一下我常用的EndPoint:

/health 應用的健康狀態

/configprops 獲取應用的配置信息,因為Spring Boot 可能發布時是單獨的Jar包,配置文件可能包含其中, 當我們需要檢查配置文件時可以使用 ConfigpropsEndPoint 進行查看一些配置是否正確。

/trace 最近幾次的http請求信息

HealthEndPoint

當我們訪問 http://localhost:8088/health 時,可以看到 HealthEndPoint 給我們提供默認的監控結果,包含 磁盤檢測和數據庫檢測。

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

其實看 Spring Boot-actuator 源碼,你會發現 HealthEndPoint 提供的信息不僅限于此,org.springframework.boot.actuate.health 包下 你會發現 ElasticsearchHealthIndicator、RedisHealthIndicator、RabbitHealthIndicator 等

也就是 HealthEndPoint 也提供 ES, Redis 等組件的健康信息。

自定義Indicator 擴展 HealthEndPoint

看源碼 其實 磁盤和數據庫健康信息就是 DiskSpaceHealthIndicator、DataSourceHealthIndicator 來實現的,當我們對一些我們自定義的組件進行監控時, 我們也可以實現個Indicator :

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

這時我們再次訪問: http://localhost:8088/health 這時返回的結果如下,包含了我們自定義的 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

其實除了擴展 HealthEndPoint 來添加一些健康檢查, 我們也可以自定定義一些EndPoint 來提供程序運行時一些信息的展示:

@Configurationpublic 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; }}

訪問 http://localhost:8088/customsystem 來查看我們自定義的EndPoint ,返回結果如下:

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

我們在為Spring Boot應用添加actuator后,期望的health接口返回結果應該是類似下面的結果:

{ status: "UP", diskSpace:  { status: "UP", total: 250182889472, free: 31169568768, threshold: 10485760 }, db:  { status: "UP", database: "H2", hello: 1 }}

如果只是返回了status

{ status: "UP"}

則需要為應用新增配置,以yml配置文件為例,需要添加如下配置:

management: security: enabled: falseendpoints: health: sensitive: false
management.endpoint.health.show-details=always

總結

以上所述是小編給大家介紹的SpringBoot實現項目健康檢查與監控,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 清新县| 都江堰市| 兴业县| 纳雍县| 平乐县| 保德县| 昂仁县| 正宁县| 东台市| 朔州市| 松潘县| 芜湖市| 黔西县| 新营市| 天全县| 临夏县| 东阳市| 陆河县| 阿合奇县| 监利县| 永靖县| 汉阴县| 丰县| 武功县| 志丹县| 哈巴河县| 乐东| 鄱阳县| 凌海市| 曲沃县| 会昌县| 寿阳县| 德安县| 佛坪县| 九台市| 望城县| 夏津县| 东乡族自治县| 美姑县| 伊通| 左贡县|