日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

一文帶你了解Spring Actuator

來源: 責編: 時間:2023-11-08 09:10:57 264觀看
導讀服務監控Spring Boot Actuator是一個用于監控和管理Spring Boot應用的子項目,它提供了一組REST端點和命令行工具, 用于查看應用的運行狀態、性能指標和健康狀況等。Actuator還支持應用度量數據的導出,以及自定義端點和安

服務監控

Spring Boot Actuator是一個用于監控和管理Spring Boot應用的子項目,它提供了一組REST端點和命令行工具, 用于查看應用的運行狀態、性能指標和健康狀況等。Actuator還支持應用度量數據的導出,以及自定義端點和安全控制等功能。通過使用Spring Boot Actuator,開發人員可以更加方便地了解應用的運行狀況,及時發現和解決問題。lWz28資訊網——每日最新資訊28at.com

概述

隨著微服務架構的普及,Spring Boot 已經成為Java開發人員的首選框架。然而,隨著應用的規模不斷擴大, 如何有效地監控和管理這些應用成為一個重要的問題。Spring Boot Actuator的出現,為開發人員提供了一個解決方案。本文將詳細介紹Spring Boot Actuator的功能、工作原理、使用場景以及應用示例,幫助讀者更好地理解和掌握這一工具。lWz28資訊網——每日最新資訊28at.com

功能簡介

  • 應用度量數據的導出:Actuator 可以將應用的運行數據導出到各種不同的存儲后端,例如 Prometheus、Datadog、New Relic 等。這樣,開發人員可以方便地使用這些數據來監控應用的性能和健康狀況。
  • REST 端點:Actuator 提供了一組 REST 端點,用于查看應用的運行狀態、健康狀況、度量數據等信息。開發人員可以通過 HTTP 請求來獲取這些數據,并使用各種工具進行可視化展示。
  • 命令行工具:除了 REST 端點之外,Actuator 還提供了一些命令行工具,例如 spring-boot-cli 和 spring-boot-admin。這些工具可以讓開發人員更方便地管理和監控應用。
  • 自定義端點:Actuator 支持自定義端點的開發,讓開發人員可以根據自己的需求來暴露自定義的監控數據。這樣可以更靈活地監控和管理應用。
  • 安全控制:Actuator 支持對監控端點的安全控制,例如限制訪問權限、身份驗證等。這樣可以保護應用的敏感信息不被泄露。

Spring-Actuator

默認監控服務

服務端點
lWz28資訊網——每日最新資訊28at.com

描述
lWz28資訊網——每日最新資訊28at.com

auditevents
lWz28資訊網——每日最新資訊28at.com

公開當前應用程序的審核事件信息。
lWz28資訊網——每日最新資訊28at.com

beans
lWz28資訊網——每日最新資訊28at.com

顯示應用程序中所有Spring bean的完整列表。
lWz28資訊網——每日最新資訊28at.com

caches
lWz28資訊網——每日最新資訊28at.com

公開可用的緩存
lWz28資訊網——每日最新資訊28at.com

conditions
lWz28資訊網——每日最新資訊28at.com

顯示在配置和自動配置類上評估的條件以及它們匹配或不匹配的原因。
lWz28資訊網——每日最新資訊28at.com

configprops
lWz28資訊網——每日最新資訊28at.com

顯示所有@ConfigurationProperties的有序列表。
lWz28資訊網——每日最新資訊28at.com

env
lWz28資訊網——每日最新資訊28at.com

公開Spring的ConfigurableEnvironment中的屬性
lWz28資訊網——每日最新資訊28at.com

flyway
lWz28資訊網——每日最新資訊28at.com

顯示已應用的任何Flyway數據庫遷移。
lWz28資訊網——每日最新資訊28at.com

health
lWz28資訊網——每日最新資訊28at.com

顯示應用健康信息。
lWz28資訊網——每日最新資訊28at.com

httptrace
lWz28資訊網——每日最新資訊28at.com

顯示HTTP跟蹤信息(默認情況下,最后100個HTTP請求 – 響應交換)。
lWz28資訊網——每日最新資訊28at.com

info
lWz28資訊網——每日最新資訊28at.com

顯示任意應用信息。
lWz28資訊網——每日最新資訊28at.com

integrationgraph
lWz28資訊網——每日最新資訊28at.com

顯示Spring集成圖。
lWz28資訊網——每日最新資訊28at.com

loggers
lWz28資訊網——每日最新資訊28at.com

顯示和修改應用程序中日志記錄器的配置。
lWz28資訊網——每日最新資訊28at.com

liquibase
lWz28資訊網——每日最新資訊28at.com

顯示已應用的任何Liquibase數據庫遷移。
lWz28資訊網——每日最新資訊28at.com

metrics
lWz28資訊網——每日最新資訊28at.com

顯示當前應用程序的“指標”信息。
lWz28資訊網——每日最新資訊28at.com

mappings
lWz28資訊網——每日最新資訊28at.com

顯示所有@RequestMapping路徑的有序列表。
lWz28資訊網——每日最新資訊28at.com

scheduledtasks
lWz28資訊網——每日最新資訊28at.com

顯示應用程序中的計劃任務。
lWz28資訊網——每日最新資訊28at.com

sessions
lWz28資訊網——每日最新資訊28at.com

允許從Spring Session支持的會話存儲中檢索和刪除用戶會話。使用Spring Session對響應式Web應用程序的支持時不可用。
lWz28資訊網——每日最新資訊28at.com

shutdown
lWz28資訊網——每日最新資訊28at.com

允許應用程序正常關閉。
lWz28資訊網——每日最新資訊28at.com

http://localhost:8080/actuatorlWz28資訊網——每日最新資訊28at.com

  • 依賴
<dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>
  • 配置
management:  endpoints:    web:      exposure:#        [health, info]        include: "*"
  • 自定義監控

監控端點相關注解:lWz28資訊網——每日最新資訊28at.com

  • @Endpoint:定義一個監控端點,同時支持HTTP和JMX兩種方式。
  • @WebEndpoint:定義一個監控端點,只支持HTTP方式。
  • @JmxEndpoint:定義一個監控端點,只支持JMX方式。
  • @ReadOperation:作用在方法上,可用來返回端點展示的信息(通過 Get 方法請求)。
  • @WriteOperation:作用在方法上,可用來修改端點展示的信息(通過 Post 方法請求)。
  • @DeleteOperation:作用在方法上,可用來刪除對應端點信息(通過 Delete 方法請求)。
  • @Selector:作用在參數上,用來定位一個端點的具體指標路由。

自定義一個端點服務:lWz28資訊網——每日最新資訊28at.com

@Endpoint(id = "custom")public class CustomEndpoint {  /**   * /actuator/custom   */  @ReadOperation  public Map custom() {    return new HashMap();  }  /**   * /actuator/custom/{name}?value={value}   */  @ReadOperation  public Map name(@Selector String name, @Nullable String value) {    return new HashMap();  }}

Spring-Admin

Spring-Actuator主要實現數據的采集,以及提供REST API以及JMX的訪問渠道,那么數據具體如何友好地顯示出來?這時我們需要對應的UI,其中spring-boot-admin就是這樣一款工具。lWz28資訊網——每日最新資訊28at.com

http://localhost:8080/applicationslWz28資訊網——每日最新資訊28at.com

  • 服務端
<dependency>        <groupId>de.codecentric</groupId>        <artifactId>spring-boot-admin-starter-server</artifactId>    </dependency>
@EnableAdminServerpublic class Application{   }
  • 客戶端
<dependency>        <groupId>de.codecentric</groupId>        <artifactId>spring-boot-admin-starter-client</artifactId>        <version>2.6.2</version>    </dependency>

客戶端配置lWz28資訊網——每日最新資訊28at.com

spring:  boot:    admin:      client:        url: http://localhost:8080

Prometheus + Grafana

上面說到,Actuator除了采集指標,提供訪問API外,還提供了“應用度量數據的導出”的功能,這樣就能將我們采集到的指標輸出到指定的存儲服務或終端以便進一步分析。其中Prometheus就是這樣一個應用。lWz28資訊網——每日最新資訊28at.com

  • Prometheus 時序數據庫,用于存儲數據,提供并提供查詢,它存儲了計算機系統在各個時間點上的監控數據
  • Grafana 儀表盤,提供監控指標可視化界面。
  • 依賴
<dependency>    <groupId>io.micrometer</groupId>    <artifactId>micrometer-registry-prometheus</artifactId></dependency>
  • 配置
management:  endpoints:    web:      exposure:        include: "*"  metrics:    export:      prometheus:        enabled: true  prometheus:    enabled: true
  • prometheus配置
scrape_configs:  - job_name: 'spring-boot-actuator'    metrics_path: '/actuator/prometheus'    static_configs:      - targets: ['localhost:8080'] # 使用你的Spring Boot應用程序的實際主機和端口替換
  • 啟動
prometheus.exe --config.file=prometheus.ymlgrafana-server.exe

由于篇幅有限,關于Grafana如何集成Prometheus,網上有很多具體實踐,這里不重復贅述...lWz28資訊網——每日最新資訊28at.com

問題

  • 服務端點

由于項目使用spring-boot版本為2.3.7.RELEASE,而spring-boot-admin-starter-server版本設置設置為2.7.x版本時,UI相關配置一直無法加載,通過源碼可以看到lWz28資訊網——每日最新資訊28at.com

在2.6.x版本中對應spring-boot-admin-server-ui存在META-IN/spring.factories文件lWz28資訊網——每日最新資訊28at.com

org.springframework.boot.autoconfigure.EnableAutoConfiguration=/  de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration

而在2.7.x版本中,spring.factories刪除了且改為了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.importslWz28資訊網——每日最新資訊28at.com

de.codecentric.boot.admin.server.ui.config.AdminServerUiAutoConfiguration

因此如果需要使用2.7.x版本的spring-boot-admin,記得把spring-boot升級到2.7.xlWz28資訊網——每日最新資訊28at.com

  • 參數名稱

參數名稱被解析為arg0,導致請求匹配失敗。通過下面的配置保證編譯后的文件通過反射獲取的參數名稱不變lWz28資訊網——每日最新資訊28at.com

<plugin>          <groupId>org.apache.maven.plugins</groupId>          <artifactId>maven-compiler-plugin</artifactId>          <version>3.11.0</version>          <configuration>              <debug>false</debug>              <!-- 防止方法參數名解析為arg0...  -->              <compilerArgs>                  <arg>-parameters</arg>              </compilerArgs>          </configuration>      </plugin>

如果使用Idea,你可以在應用啟動后,Actuator功能面板的Mappings中看到服務地址的變化lWz28資訊網——每日最新資訊28at.com

結束語

服務監控是為了更好的了解服務運行狀況,及時發現服務可能出現的問題,并在出現故障時能夠有效的定位問題產生的原因。更大層面解決系統運行過程中的維護 成本。關于監控相關的應用還有一些,比如SkyWalking、Zipkin、Elastic APM等等。lWz28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-17662-0.html一文帶你了解Spring Actuator

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 不吃飯也要掌握的Synchronized鎖升級過程

下一篇: 解析幾何:計算兩條線段的交點

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 浙江省| 阿图什市| 正安县| 怀安县| 普宁市| 孙吴县| 房山区| 手游| 昌宁县| 娄底市| 建平县| 井陉县| 平远县| 手游| 九江市| 吉隆县| 区。| 木兰县| 益阳市| 麦盖提县| 宣武区| 山阴县| 南和县| 北票市| 东光县| 霍州市| 迭部县| 惠水县| 镇巴县| 日喀则市| 布尔津县| 商城县| 德州市| 铁岭市| 汽车| 虞城县| 油尖旺区| 平塘县| 玛沁县| 福建省| 保德县|