020-Spring Boot 監控和度量


一、概述

  通過配置使用actuator查看監控和度量信息

二、使用

2.1、建立web項目,增加pom

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

啟動項目,查看日志,發現能夠訪問地址如下

2.2、增加actuator的pom依賴

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

啟動項目,查看日志,發現能夠訪問地址如下

  

  可以看到,可訪問地址增加了

三、詳解

  建議安裝jsonview插件方便查看json

3.1、增加配置

  關閉權限限制,application.properties

management.security.enabled=false

  除開health接口還依賴endpoints.health.sensitive的配置外,其他接口都不需要輸入用戶名和密碼。

3.2、訪問以下網址

  Spring Boot Actuator 的關鍵特性是在應用程序里提供眾多 Web 接口,通過它們了解應用程序運行時的內部狀況。Actuator 提供了如下接口,可以分為三大類:配置接口、度量接口和其它接口,具體如下表所示。

HTTP方法 路徑   描述 鑒權
GET /auditevents   審計事件 true
GET /autoconfig 配置

查看自動配置的使用情況

提供了一份自動配置報告,記錄哪些自動配置條件通過了,哪些沒通過

true
GET /configprops 配置

查看配置屬性,包括默認配置

描述配置屬性(包含默認值)如何注入Bean

true
GET /beans 配置

查看bean及其關系列表

描述應用程序上下文里全部的Bean,以及它們的關系

true
GET /dump   打印線程棧,獲取線程活動的快照 true
GET /env 配置 查看所有環境變量 true
GET /env/{name} 配置 根據名稱獲取特定的環境屬性值 true
GET /health 配置

查看應用健康指標,這些值由HealthIndicator的實現類提供

包括:Cassandra、Composite、Couchbase、DataSource、DiskSpace、

Elasticsearch、Jms、Ldap、Mail、Mongo、Ordered、Rabbit、Redis、solr

false
GET /heapdump     true
GET /info 配置 查看應用信息,這些信息由info打頭的屬性提供 false
GET /loggers     true
GET /loggers/{name}     true
POST /loggers/{name}     true
GET /mappings   查看所有url映射,以及它們和控制器(包含Actuator端點)的映射關系 true
GET /metrics 度量 報告各種應用程序度量信息,比如內存用量和HTTP請求計數 true
GET /metrics/{name} 度量 報告指定名稱的應用程序度量值 true
POST /shutdown   關閉應用,要求endpoints.shutdown.enabled設置為true true
GET /trace   查看基本追蹤信息,提供基本的HTTP請求跟蹤信息(時間戳、HTTP頭等) true

3.3、源碼查看

  在spring-boot-actuator-1.5.9.RELEASE.jar包中org.springframework.boot.actuate.endpoint下,查看具體類實現,

  可以設置某一項是否顯示

endpoints.beans.enabled=false

  查看代碼這里的endpoints,均繼承自AbstractEndpoint,其中AbstractEndpoint含有屬性如下

sensitive 敏感信息
enabled  啟用

3.3.1、org.springframework.boot.actuate.health

  除原有支持的健康檢查外,還支持擴展。HealthIndicator

  步驟:

  1》實現HealthIndicator接口,實現邏輯,納入spring容器管理中

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        //return Health.down().withDetail("error", "spring test error").build();
        return Health.up().withDetail("success", "spring test success").build();
    }
}

  actuator暴露的health接口權限是由兩個配置: management.security.enabled 和 endpoints.health.sensitive組合的結果進行返回的。

management.security.enabled endpoints.health.sensitive Unauthenticated Authenticated
false false Full content Full content
false true Status only Full content
true false Status only Full content
true true No content Full content

3.3.2、info

在所有加載的配置文件中以info開頭的配置,均可以顯示在這里,如

info.name=myinfo
info.version=1.0.0
info.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot

同時也會顯示git信息git.properties

git.branch=master

顯示如下

{
  datasource: {
    url: "jdbc:mysql://127.0.0.1:3306/springboot",
    name: "root",
    password: "root",
    driverClassName: "com.mysql.jdbc.Driver"
  },
  name: "myinfo",
  version: "1.0.0",
  git: {
    branch: "master"
  }
}

3.3.3、metrics查看度量信息

  CounterService:計數服務,可以直接使用

    如查看上文中的user/home訪問次數    

    @Autowired
    private CounterService counterService;//引入

    @GetMapping("/user/home")
    public String home(@RequestParam("error") String error) {
        counterService.increment("user.home.request.count");//埋點
        if(error.equals("test")) {
            throw new NullPointerException();
        }
        return "home";
    }

    此時查看即可:http://127.0.0.1:8080/metrics

  GaugeService:用來統計某個值,查看某個監控點的值

    @Autowired
    private GaugeService gaugeService;

    @GetMapping("/user/create")
    public String create(int age) {
        gaugeService.submit("user.create.age", age);
        return "create";
    }

  此時查看即可:http://127.0.0.1:8080/metrics

3.3.4、監控信息輸出其他位置

 1》添加配置類,如下

@Configuration
public class ExportConfiguration {
    @Bean
    @ExportMetricWriter
    public MetricWriter createMetricWriter(MBeanExporter exporter) {
        return new JmxMetricWriter(exporter);
    }
}

查看MetricWriter 支持如下幾種,也可自行定義 

  

這里使用了Jmx,

3.4、安全方式驗證

1》增加security的pom

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

配置文件設置

security.basic.enabled=true
security.user.name=admin
security.user.password=password

綜合以上可作如下配置:

security.basic.enabled=true
security.basic.path=/admin    #針對/admin路徑進行認證
security.user.name=admin     #認證使用的用戶名
security.user.password=password   #認證使用的密碼
management.security.roles=SUPERUSER

management.port=11111   #actuator暴露接口使用的端口,為了和api接口使用的端口進行分離
management.context-path=/admin   #actuator暴露接口的前綴
management.security.enabled=true   #actuator是否需要安全保證

endpoints.metrics.sensitive=false   #actuator的metrics接口是否需要安全保證
endpoints.metrics.enabled=true

endpoints.health.sensitive=false  #actuator的health接口是否需要安全保證
endpoints.health.enabled=true

四、JDK工具使用

查看Jmx方式JDK有三種在bin下,jConsole、jmc、jvisualVM

JConsole方式

  

Jvisualvm方式

  注意jvisualvm默認不支持MBEAn,Jconsole等需要自己安裝插件,在 工具→插件中安裝插件

  

jmc方式

  

注意:這三種工具不僅僅能查看Mbean,其他信息也能查看,和頁面內容查看一致。

  與上面配置的JMX沒有關系,配置jmx只是增加了MetricWriter 項

 


免責聲明!

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



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