Spring Boot集成Actuator


一、Spring-Boot-Actuator簡介

官網:https://docs.spring.io/spring-boot/docs/2.3.4.BUILD-SNAPSHOT/reference/html/production-ready-features.html#production-ready

Spring-Boot-Actuator是Spring Boot 中的一個模塊,當應用集成了此模塊之后,應用會多出很多可以使用http訪問的端點(endpoint),通過訪問這些endpoint可以監控和管理Spring-Boot應用。

端點(endpoint)可以粗略的理解為一個固定的url,例如,當Spring Boot應用集成了Spring-Boot-Actustor之后,訪問http://localhost:8083/actuator/health 就可以監控到應用的健康狀態。

二、集成

  • 1:pom文件中增加相關依賴

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

  • 2:啟動應用,訪問http://localhost:8083/actuator 返回了一個json的字符串,其中_links下面列出了當前所有可以監控的指標,health是健康狀態,info是基本信息,若沒有在配置文件中配置info.build,info看到的應該是空json字符串。

  • 3:Spring-Boot-Actuator相關配置文件概述

Actusator的大部分配置是圍繞management.endpoints這個配置節點展開的,旗下有web;jmx。
通過web.include=* 可以打開web這類別下面所有的endpoint;通過web.exclude=env可以關閉web類別下面env的endpoint,當然web.exclude=*就是關閉web下面所有的endpoint。

三、通過訪問Actuator提供的endpoint來監控應用

  • 1:配置文件修改為

spring.application.name=order-service
server.port=8083

#json格式化輸出
spring.jackson.serialization.indent_output=true


#配置info信息
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@


#暴露web下所有的端點
management.endpoints.web.exposure.include=*


#展示詳細的健康信息
management.endpoint.health.show-details=always

  • 2:查看所有可監控項訪問http://localhost:8083/actuator/

  • 3:查看詳細的健康信息訪問http://localhost:8083/actuator/health

  • 4:查看beans加載情況訪問http://localhost:8083/actuator/beans(其實我也不知道展示這個beans的信息有什么價值)

  • 5:查看應用運行環境相關參數訪問http://localhost:8083/actuator/env

  • 6:查看引用日志級別訪問http://localhost:8083/actuator/loggers

四、通過訪問Actuator提供的endpoint來管理應用

  • 1:修改com.naylor包下面的日志級別

curl -X POST http://localhost:8083/actuator/loggers/com.naylor.logging -H "Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8" --data '{"configuredLevel":"debug"}'



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM