SpringBoot Actuator — 埋點和監控


項目中看到了有埋點監控、報表、日志分析,有點興趣想慢慢搗鼓一下


1. 數據埋點

監控機器環境的性能和業務流程或邏輯等各項數據,並根據這些數據生成對應的指標,那么我們就稱為數據埋點。比如我們想知道某個接口調用的 TPS、機器 CPU 的使用率,這些都可以用到數據埋點







2. Micrometer

Micrometer 為流行的各種監控系統提供了一個簡單的門面(類似於日志門面) —— 提供了與供應商無關的接口(counters,timers,gauges等),這些接口稱為 meter 接口,其由 MeterRegistry 創建並保存,可理解為 MeterRegistry 是個集合里面存儲了各種 meter 的度量數據,下面展示最簡單的 counter 接口的使用


2.1 簡單使用

其還有 timers、gauges 等接口,自行查閱

// 創建一個 meter 注冊中心
MeterRegistry registry = new SimpleMeterRegistry(); 

// 創建一個名為 test 度量
Counter counter = meterRegistry.counter("test");

// 讓這個度量的計數加 1
counter.increment();

就是如此簡單,比如在調用指定接口的時候,可以使用 counter 接口來度量調用的次數,頻率等等



2.2 命名規范

Micrometer 命名用 . 分隔小寫單詞字符,在接入其他監控系統時會自動將命名轉成其適應的格式(或者可重寫一個 NamingConvention 轉換器來覆蓋默認命名轉換)。而且還支持多標簽來量化,即有了多維度的度量,使統計更加豐富。下面簡單地舉例命名規范:

# 表示 http 請求
Counter counter = meterRegistry.counter("http.server.requests");

# 表示 http 請求中,請求 user 模塊
Counter counter = meterRegistry.counter("http.server.requests", "user");

# 表示 http 請求中,請求 user 模塊中,請求 login 登錄方法
Counter counter = meterRegistry.counter("http.server.requests", "user", "login");






3. SpringBoot Actuator

SpringBoot Actuator 其底層使用了 Mircometer ,可度量 SpringBoot 應用和獲取它的各項指標,可通過 HTTP 或 JMX 來調用 Actuator 暴露的各種端點,然后就可以獲取一個正在運行中的應用的內部狀態


當然內部指標並不是所有都可以向外暴露的,所以我們得有選擇的開放,或者加入權限校驗之后才能獲取如下內容:

  • 有那些可配置的屬性
  • 各依賴包的日志級別
  • 占用了多少內存
  • HTTP 埋點被請求了多少次
  • 應用本身以及協作的外部服務的健康狀態
  • ......

3.1 添加依賴

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


3.2 基礎配置

management:
  server:
    port: 9090							# 一般啟動獨立端口(默認和應用端口一致),啟用后源端口不可查
  endpoints:
    web:
      base-path: /actuator				# 默認前綴路徑,可修改
      exposure:
        include: health,info,metrics	# 向外暴露的端點,可用通配符('*',需要單引號)
        exclude: env,heapdump			# 排除暴露的端點


3.3 查看可消費的端點

可先用 HTTP 訪問 localhost:9090/actuator 來獲取 HATEOAS(可簡單理解為暴露的端點文檔),它是所有可暴露端點的地圖,可通過屬性對應的地址來獲取的指標,內容如下:

{
  "_links": {
    "self": {
      "href": "http://localhost:9090/actuator",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:9090/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "http://localhost:9090/actuator/health",
      "templated": false
    },
    "info": {
      "href": "http://localhost:9090/actuator/info",
      "templated": false
    }
  }
}


3.4 獲取應用的基本信息

消費對應的指標,就在地址后面加上名字即可。應用的基本信息是需要自己配置的,沒有默認值,所以首次訪問 可訪問 localhost:9090/actuator/info 是一個空 json。

可配置 info 開頭的屬性,比如聯系方式,應用的作用等等,其配置和消費結果如下:

info:
  contact:
    email: support@howl.com
    phone: 123456789
  description: actuator test application
  
  
# 消費結果
{
  "contact": {
    "email": "support@howl.com",
    "phone": 123456789
  },
  "description": "actuator test application"
}


3.5 健康指標

首先嘗試訪問 localhost:9090/actuator/health,就可以獲取指標內容了

{
  "status": "UP"
}

這里顯示的是一個或多個健康指示器的聚合狀態,即當前應用和與之交互的外部系統(數據庫,消息隊列,Eureka等等)的健康狀態的聚合狀態。我們可以添加如下配置來獲取健康指示器的內聚狀態

management:
  endpoint:
    health:
      show-details: always
      
      
# 消費結果
{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 493516484608,
        "free": 436332154880,
        "threshold": 10485760,
        "exists": true
      }
    },
    "ping": {
      "status": "UP"
    }
  }
}

SpringBoot 的自動配置功能可以確保只有與應用交互的組件才會顯示到 health 里面



3.6 指標端點 metrics

可訪問如下地址來獲取 Actuator 提供的開箱即用的指標分類,包括了內存、處理器、垃圾收集、HTTP請求等指標

http://localhost:9090/actuator/metrics


# 消費結果
{
  "names": [
    "http.server.requests",
    "jvm.buffer.count",
    "jvm.buffer.memory.used",
    "jvm.buffer.total.capacity",
    "jvm.classes.loaded",
    "jvm.classes.unloaded",
    "jvm.gc.live.data.size",
    "jvm.gc.max.data.size",
    "jvm.gc.memory.allocated",
    "jvm.gc.memory.promoted",
    "jvm.gc.pause",
    "jvm.memory.committed",
    "jvm.memory.max",
    "jvm.memory.used",
    "jvm.threads.daemon",
    "jvm.threads.live",
    "jvm.threads.peak",
    "jvm.threads.states",
    "logback.events",
    "process.cpu.usage",
    "process.start.time",
    "process.uptime",
    "system.cpu.count",
    "system.cpu.usage",
    "tomcat.sessions.active.current",
    "tomcat.sessions.active.max",
    "tomcat.sessions.alive.max",
    "tomcat.sessions.created",
    "tomcat.sessions.expired",
    "tomcat.sessions.rejected"
  ]
}



# 可用標准地址 + 指標端點名字 來消費某個指標端點
http://localhost:9090/actuator/metrics/http.server.requests






4. 實例

統計 /user/login 接口被調用的次數,meterRegistry 注冊中心在自動配置中加入容器可直接使用


4.1 測試接口

啟動應用,然后多次訪問這個接口

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    SimpleMeterRegistry meterRegistry;

    @GetMapping("/login")
    public String userLogin() {
        Counter counter = meterRegistry.counter("http.server.requests", "uri", "/user/login");
        counter.increment();
        return "登錄成功";
    }
}


4.2 消費指標端點

訪問如下地址即可消費端點

http://localhost:9090/actuator/metrics/http.server.requests?tag=uri:/user/login


# 消費結果
# 可以看到這個接口度量(COUNT)了 18次 訪問(數據做了部分清除)
{
  "name": "http.server.requests",
  "baseUnit": "seconds",
  "measurements": [
    {
      "statistic": "COUNT",
      "value": 18.0
    }
  ],
  "availableTags": [
    {
      "tag": "method",
      "values": [
        "GET"
      ]
    }
  ]
}






5. SpringBoot Admin

使用上面的地址訪問指標很不友好,不可能看一堆這樣的數據,得使用一些美化的 UI 界面,SpringBoot Admin 就提供了這樣的框架

SpringBoot Admin 作為簡單的監控,分為 服務器端 和 客戶端


5.1 Admin 服務器端

作為監控的服務端一般是在另外一台服務器上部署的,然后這台服務器會定時去配置好的地址里面拉取監控的指標數據


5.1.1 啟用功能、添加依賴

在初始化應用的時候在 Spring Initializr 也可以選擇

@EnableAdminServer
@SpringBootApplication
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}
<dependency>
     <groupId>de.codecentric</groupId>
     <artifactId>spring-boot-admin-starter-server</artifactId>
     <version>2.4.3</version>
</dependency>

5.1.2 選擇一個端口

server:
  port: 10000

5.1.3 訪問

直接訪問 10000 端口即可,當然現在沒有東西可以監控,主頁面是空白沒應用的





5.2 Client 客戶端


5.2.1 添加依賴

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

<!--  admin-client  -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.4.3</version>
</dependency>

5.2.2添加配置

# 服務端用客戶端的主機名來通信,但在虛擬機上客戶端主機名會解析不正確,導致實例一直下線狀態
# 此時需要如下配置

spring:
  application:
    name: actuatorApplicaton
  boot:
    admin:
      client:
        url: http://admin-serve-ip:10000						# 向服務端定時發送請求
        instance:
          service-url: http://admin-client-ip:8080				# 主頁
          management-base-url: http://admin-client-ip:9090		# 各類指標的基礎地址

5.2.3 訪問 admin-serve

訪問即可發現有個應用的指標被獲取了,然后里面可以看各種已經暴露的端點指標





5.3 Eureka 服務器發現

上面每啟動一個客戶端都要手動進行配置監控的 IP 地址很是麻煩,既然微服務架構有服務發現機制的,那么我們可以在監控的服務端上配置 Eureka 的地址,那么 Admin-Server 就會去注冊中心獲取地址再去拉取指標


5.3.1 加依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.4.3</version>
</dependency>

5.3.2 添加配置

eureka:
  client:
    service-url:
      defaultZone: http://xxx.xxx.xxx.xxx:xxx/eureka/






6 缺點

筆者個人覺得個人小型項目用這個組合來監控埋點已經足夠了,加上警告處理都是很不錯的選擇,但是 SpringBoot Admin 只能監控短時間內的應用信息,如果需要各時間段的監控那么就需要有時序數據庫的支持(比如查看這個月內的統計信息),這些就顯得無能為力了。


當然還是有代替方案的:

  • Actuator:埋點操作
  • Promethus:定期去 actuator 拉取數據並以時序的形式存儲(內部有時序數據庫)
  • Granfan:用戶友好的 UI 數據展示,展示 Promethus 的數據

后面筆者還會寫一篇 Promethus 監控的筆記




免責聲明!

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



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