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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring Boot Actuator使用

發布時間:2024/9/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring Boot Actuator使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring Boot Actuator使用

Spring Boot 的 Actuator 提供了很多生產級的特性,比如監控和度量Spring Boot 應用程序。

官網參考文檔Spring Boot Actuator: Production-ready Features

maven依賴

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

暴露的endpoints

Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.

根據官網的提示,我們可以得知Actuator endpoints讓我們能跟我們的application進行監控和互動。Spring Boot Actuator本身已經提供的若干個定義好的endpoints。更加這些endpoints,我們對我們的application進行監控。如:health endpoint (監控application的健康信息)

Most applications choose exposure via HTTP, where the ID of the endpoint along with a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.

根據官網的提示,這些endpoint都是以HTTP進行暴露的,并且是以/actuator為前綴的URL,如:/actuator/health是健康監控HTTP端口。

表格鏈接:https://www.jianshu.com/p/d5943e303a1f

Endpoint IDDescription
auditevents顯示應用暴露的審計事件 (比如認證進入、訂單失敗)
info顯示應用的基本信息
health顯示應用的健康狀態
metrics顯示應用多樣的度量信息
loggers顯示和修改配置的loggers
logfile返回log file中的內容(如果logging.file或者logging.path被設置)
httptrace顯示HTTP足跡,最近100個HTTP request/repsponse
env顯示當前的環境特性
flyway顯示數據庫遷移路徑的詳細信息
liquidbase顯示Liquibase 數據庫遷移的纖細信息
shutdown讓你逐步關閉應用
mappings顯示所有的@RequestMapping路徑
scheduledtasks顯示應用中的調度任務
threaddump執行一個線程dump
heapdump返回一個GZip壓縮的JVM堆dump

暴露方式

根據官網的提示這些端口都有默認的暴露方式,分別是JMX和Web。這個是官網的表格:

IDJMXWeb
auditeventsYesNo
beansYesNo
cachesYesNo
conditionsYesNo
configpropsYesNo
envYesNo
flywayYesNo
healthYesYes
heapdumpN/ANo
httptraceYesNo
infoYesNo
integrationgraphYesNo
jolokiaN/ANo
logfileN/ANo
loggersYesNo
liquibaseYesNo
metricsYesNo
mappingsYesNo
prometheusN/ANo
quartzYesNo
scheduledtasksYesNo
sessionsYesNo
shutdownYesNo
startupYesNo
threaddumpYesNo

如果想更改這些默認的暴露方式,可以從配置文件中修改如下四個配置屬性:

management.endpoints.jmx.exposure.excludemanagement.endpoints.jmx.exposure.include=* #默認management.endpoints.web.exposure.excludemanagement.endpoints.web.exposure.include=health #默認

保護 HTTP endpoin

You should take care to secure HTTP endpoints in the same way that you would any other sensitive URL. If Spring Security is present, endpoints are secured by default using Spring Security’s content-negotiation strategy. If you wish to configure custom security for HTTP endpoints, for example, only allow users with a certain role to access them, Spring Boot provides some convenient RequestMatcher objects that can be used in combination with Spring Security.

關于HTTP endpoint的保護,如果你有整合Spring Security,那么endpoints會使用Spring Security默認的安全策略,你也可以自定義一個安全策略。

自定義的SecurityFilterChain(安全過濾鏈),可以幫助我們對Endpoint的請求進行過濾,如下面安全過濾鏈中,對Endpoint進行的請求都必須有ENDPOINT_ADMIN role。

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain;@Configuration(proxyBeanMethods = false) public class MySecurityConfiguration {@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests((requests) -> requests.anyRequest().hasRole("ENDPOINT_ADMIN"));http.httpBasic();return http.build();} }

開啟CORS支持

management:endpoints:web:cors:allowed-origins: "https://example.com"allowed-methods: "GET,POST"

/actuator/health

Spring Boot Actuator有很多預定義的健康指示器,下面列出幾個是:

DataSourceHealthIndicator

DiskSpaceHealthIndicator

MongoHealthIndicator

RedisHealthIndicator

CassandraHealthIndicator

官網API地址:Overview (Spring Boot 2.5.3 API)

除了上面的還有其他支持的指示器,可以去官網API文檔看,我們使用也十分簡單,Spring Boot Actuator已經把這些裝配到上下文中了,默認是開啟監控的。如果想關閉某個健康檢測,在配置文件進行配置即可

management.health.mongo.enabled=false

如果需要詳細的健康檢測數據,只要在配置文件中加

management.endpoint.health.show-details=always

還有兩個Kubernetes常用的健康檢測端口:

/actuator/metrics

/actuator/metrics是度量或者檢測端口,我們可以通過一些方式檢測指定的metrics

http://localhost:8080/actuator/metrics/{MetricName}

可以通過端口/actuator來獲取MetricName

{"names": ["jvm.memory.max","http.server.requests","process.files.max",...] }

/actuator/loggers

該端口用于獲取loggers的等級或者進行修改

http://localhost:8080/actuator/loggers/{name} //如: http://localhost:8080/actuator/loggers/root

/actuator/bus-refres

這個端口一般是在spring cloud config中使用,通過這個端口可以刷新配置文件信息。

curl -v -X POST “http://localhost:8081/actuator/bus-refresh”
curl -X POST “http://localhost:8081/actuator/bus-refresh”

/actuator/bus-refres

這個端口一般是在spring cloud config中使用,通過這個端口可以刷新配置文件信息。

curl -v -X POST “http://localhost:8081/actuator/bus-refresh”
curl -X POST “http://localhost:8081/actuator/bus-refresh”

總結

以上是生活随笔為你收集整理的spring Boot Actuator使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。