日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

020-Spring Boot 监控和度量

發(fā)布時(shí)間:2023/11/29 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 020-Spring Boot 监控和度量 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、概述

  通過(guò)配置使用actuator查看監(jiān)控和度量信息

二、使用

2.1、建立web項(xiàng)目,增加pom

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

啟動(dòng)項(xiàng)目,查看日志,發(fā)現(xiàn)能夠訪問(wèn)地址如下

2.2、增加actuator的pom依賴

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

啟動(dòng)項(xiàng)目,查看日志,發(fā)現(xiàn)能夠訪問(wèn)地址如下

  

  可以看到,可訪問(wèn)地址增加了

三、詳解

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

3.1、增加配置

  關(guān)閉權(quán)限限制,application.properties

management.security.enabled=false

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

3.2、訪問(wèn)以下網(wǎng)址

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

HTTP方法路徑?描述鑒權(quán)
GET/auditevents?審計(jì)事件true
GET/autoconfig配置

查看自動(dòng)配置的使用情況

提供了一份自動(dòng)配置報(bào)告,記錄哪些自動(dòng)配置條件通過(guò)了,哪些沒(méi)通過(guò)

true
GET/configprops配置

查看配置屬性,包括默認(rèn)配置

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

true
GET/beans配置

查看bean及其關(guān)系列表

描述應(yīng)用程序上下文里全部的Bean,以及它們的關(guān)系

true
GET/dump?打印線程棧,獲取線程活動(dòng)的快照true
GET/env配置查看所有環(huán)境變量true
GET/env/{name}配置根據(jù)名稱獲取特定的環(huán)境屬性值true
GET/health配置

查看應(yīng)用健康指標(biāo),這些值由HealthIndicator的實(shí)現(xiàn)類提供

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

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

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

3.3、源碼查看

  在spring-boot-actuator-1.5.9.RELEASE.jar包中org.springframework.boot.actuate.endpoint下,查看具體類實(shí)現(xiàn),

  可以設(shè)置某一項(xiàng)是否顯示

endpoints.beans.enabled=false

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

sensitive 敏感信息 enabled 啟用

3.3.1、org.springframework.boot.actuate.health

  除原有支持的健康檢查外,還支持?jǐn)U展。HealthIndicator

  步驟:

  1》實(shí)現(xiàn)HealthIndicator接口,實(shí)現(xiàn)邏輯,納入spring容器管理中

@Component public class MyHealthIndicator implements HealthIndicator {@Overridepublic Health health() {//return Health.down().withDetail("error", "spring test error").build();return Health.up().withDetail("success", "spring test success").build();} }

  actuator暴露的health接口權(quán)限是由兩個(gè)配置:?management.security.enabled?和?endpoints.health.sensitive組合的結(jié)果進(jìn)行返回的。

management.security.enabledendpoints.health.sensitiveUnauthenticatedAuthenticated
falsefalseFull contentFull content
falsetrueStatus onlyFull content
truefalseStatus onlyFull content
truetrueNo contentFull content

3.3.2、info

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

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

同時(shí)也會(huì)顯示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:計(jì)數(shù)服務(wù),可以直接使用

    如查看上文中的user/home訪問(wèn)次數(shù)    

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

    此時(shí)查看即可:http://127.0.0.1:8080/metrics

  GaugeService:用來(lái)統(tǒng)計(jì)某個(gè)值,查看某個(gè)監(jiān)控點(diǎn)的值

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

  此時(shí)查看即可:http://127.0.0.1:8080/metrics

3.3.4、監(jiān)控信息輸出其他位置

?1》添加配置類,如下

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

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

  

這里使用了Jmx,

3.4、安全方式驗(yàn)證

1》增加security的pom

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

配置文件設(shè)置

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

綜合以上可作如下配置:

security.basic.enabled=true security.basic.path=/admin #針對(duì)/admin路徑進(jìn)行認(rèn)證 security.user.name=admin #認(rèn)證使用的用戶名 security.user.password=password #認(rèn)證使用的密碼 management.security.roles=SUPERUSERmanagement.port=11111 #actuator暴露接口使用的端口,為了和api接口使用的端口進(jìn)行分離 management.context-path=/admin #actuator暴露接口的前綴 management.security.enabled=true #actuator是否需要安全保證endpoints.metrics.sensitive=false #actuator的metrics接口是否需要安全保證 endpoints.metrics.enabled=trueendpoints.health.sensitive=false #actuator的health接口是否需要安全保證 endpoints.health.enabled=true

四、JDK工具使用

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

JConsole方式

  

Jvisualvm方式

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

  

jmc方式

  

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

  與上面配置的JMX沒(méi)有關(guān)系,配置jmx只是增加了MetricWriter 項(xiàng)

?

總結(jié)

以上是生活随笔為你收集整理的020-Spring Boot 监控和度量的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。