Spring Boot 監控實戰



     


當一個Spring Boot 應用運行的時候,開發者須要對Spring Boot應用進行實時監控,得到項目的報警需求,Spring Boot 提供了,actuator 來幫助開發者獲取應用程序運行時的數據。html

端點配置

在Spring Boot 中添加端點配置至關的簡單。只須要添加 spring-boot-starter-actuator 添加相關的依賴web

  
  
   
   
            
   
   
  1. spring

  2. 跨域

  3. 瀏覽器

  4. 緩存

  5. 微信

  6. app

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.3.1.RELEASE</version> </dependency>

經常使用的端點以下:cors

經常使用端點列舉以下,能夠一個個詳細試一下:spring-boot

/info        應用基本信息 /health       健康度信息 /metrics      運行指標 /env        環境變量信息 /loggers      日誌相關 /dump       線程相關信息 /trace       請求調用軌跡

這些端點大都是默認開啓的,若是想要開啓一個端點,須要在配置文件中,配置如下內容。

  
  
   
   
            
   
   
endpoints: metrics: sensitive: false

此時sensitive 是關閉的。

舉個例子:這裏舉個例子,訪問是否在線的接口

  
  
   
   
            
   
   
localhost:8080/actuator/health

此時瀏覽器的輸出結果爲:

端點響應緩存

對於一些不帶參數的端點將會進行緩存。

  
  
   
   
            
   
   
management: endpoint: auditevents: cache: time-to-live: 100s

上方的配置說明了緩存達到100s

路徑映射

能夠對訪問的路徑進行映射。

  
  
   
   
            
   
   
management: endpoints: web: base-path: / path-mapping: health: healthcheck

此時訪問路徑由原先的 localhost:8080/actuator/health 轉變爲 localhost:8080/healthcheck

CORS

進行跨域操做。能夠經過配置文件,快速的開啓CORS支持。

  
  
   
   
            
   
   
management: endpoints: web: cors: allowed-origins: http://localhost:8091 allowed-methods: *

在上方中,容許處理,來自http://localhost:8091 的任何請求,容許的方法任意。

配置信息可視化

添加相關的依賴。

  
  
   
   
            
   
   
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.3</version> </dependency>

在啓動類上增長相關的註解:

  
  
   
   
            
   
   




package com.example.demo;import de.codecentric.boot.admin.server.config.EnableAdminServer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableAdminServerpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

配置完成之後,輸入連接,進行訪問。

  
  
   
   
            
   
   
http://localhost:8080/index.html

再次添加client端

  
  
   
   
            
   
   
<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client --><dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.3</version></dependency>

書寫配置文件

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

此時查看admin

查看其健康度



小明菜市場


本文分享自微信公衆號 - 小明菜市場(fileGeek)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索