Spring中的Actuator使用詳解
1 Spring Actuator
在生產(chǎn)環(huán)境中運(yùn)行的程序,并不總是穩(wěn)定、安靜、正確的,往往會(huì)遇到各式各樣的現(xiàn)場(chǎng)狀況,這個(gè)時(shí)候,就需要獲取該程序足夠多的運(yùn)行狀態(tài)信息,然后分析并對(duì)其進(jìn)行有效管理。
Spring Boot Actuator 提供了多種特性來監(jiān)控和管理應(yīng)用程序,可以基于 HTTP,也可以基于 JMX。
將 actuator 依賴包添加到項(xiàng)目中
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
2 Endpoints
endpoint 可以理解為被管理(或被監(jiān)控) 對(duì)象 ,actuator 就是通過這些 endpoint 來實(shí)現(xiàn)對(duì)應(yīng)用程序的監(jiān)控管理。
spring 提供了大量的內(nèi)置 endpoint,比如 health,beans,mappings,endpoint 名稱也稱為 endpoint id:
ID | 描述 | 默認(rèn)開啟 | JMX | WEB |
auditevents | Exposes audit events information for the current application. | Yes | Yes | No |
beans | Displays a complete list of all the Spring beans in your application. | Yes | Yes | No |
caches | Exposes available caches. | Yes | Yes | No |
conditions | Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match. | Yes | Yes | No |
configprops | Displays a collated list of all @ConfigurationProperties. | Yes | Yes | No |
env | Exposes properties from Spring’s ConfigurableEnvironment. | Yes | Yes | No |
flyway | Shows any Flyway database migrations that have been applied. | Yes | Yes | No |
health | Shows application health information. | Yes | Yes | Yes |
httptrace | Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). | Yes | Yes | No |
info | Displays arbitrary application info. | Yes | Yes | Yes |
integrationgraph | Shows the Spring Integration graph. | Yes | Yes | No |
loggers | Shows and modifies the configuration of loggers in the application. | Yes | Yes | No |
liquibase | Shows any Liquibase database migrations that have been applied. | Yes | Yes | No |
metrics | Shows ‘metrics’ information for the current application. | Yes | Yes | No |
mappings | Displays a collated list of all @RequestMapping paths. | Yes | Yes | No |
scheduledtasks | Displays the scheduled tasks in your application. | Yes | Yes | No |
sessions | Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications. | Yes | Yes | No |
shutdown | Lets the application be gracefully shutdown. | No | Yes | No |
threaddump | Performs a thread dump. | Yes | Yes | No |
對(duì)于 Web 應(yīng)用,還有以下 endpoint:
ID | 描述 | 默認(rèn)開啟 | JMX | WEB |
heapdump | Returns a GZip compressed hprof heap dump file. | Yes | N/A | No |
jolokia | Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). | Yes | N/A | No |
logfile | Returns the contents of the logfile (if logging.file or logging.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content. | Yes | N/A | No |
prometheus | Exposes metrics in a format that can be scraped by a Prometheus server. | Yes | N/A | No |
可以通過 endpoint id 來配置是否開啟該 endpoint,也可以通過 management.endpoints.enabled-by-default 屬性來配置改變是否默認(rèn)開啟的方式。
management.endpoint.shutdown.enabled=true management.endpoints.enabled-by-default=false management.endpoint.info.enabled=true
可以通過下面的屬性修改 JMX/Web 的默認(rèn)行為:
management.endpoints.jmx.exposure.include=* management.endpoints.jmx.exposure.exclude= management.endpoints.web.exposure.include=* management.endpoints.web.exposure.exclude=env,beans
可以通過注解的方式來添加自定義的 Endpoint:
- @Endpoint
- @ReadOperation
- @WriteOperatino
- @DeleteOperation
例如:
@Endpoint(id = "hello") @Service public class HelloService{ @ReadOperation public String hello(){ return "Hello Endpoint"; } }
然后通過 JConsole 可以看到新添加的 Endpoint Mbean:
也可以通過以下 url 去訪問: /actuator/jolokia/exec/org.springframework.boot:type=Endpoint,name=Hello/hello
返回結(jié)果
{
"request": {
"mbean": "org.springframework.boot:type=Endpoint,name=Hello",
"type": "exec",
"operation": "hello"
},
"value": "Hello Endpoint",
"timestamp": "xxxx",
"status": 200
}
有關(guān)于 MBean 的詳細(xì)信息的格式,可以通過 JConsole 查看,例如:
3 Jolokia
使用 Jolokia 可以通過 HTTP 的形式來訪問 JMX Beans。
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
通過屬性 management.endpoints.web.exposure.include=* 來將 /actuator/jolokia 添加到 Web Mappings 中。
4 Health
Spring Actuator 默認(rèn)添加了以下 HealthIndicator:
- CassandraHealthIndicator
- CouchbaseHealthIndicator
- DiskSpaceHealthIndicator
- DataSourceHealthIndicator
- ElasticsearchHealthIndicator
- InfluxDbHealthIndicator
- JmsHealthIndicator
- MailHealthIndicator
- MongoHealthIndicator
- Neo4jHealthIndicator
- RabbitHealthIndicator
- RedisHealthIndicator
- SolrHealthIndicator
比如,當(dāng) Spring 容易中有 InfluxDB bean 時(shí),Spring Actuator 就會(huì)自動(dòng)添加對(duì) InfluxDB health 的檢測(cè)。
@Bean public InfluxDB getInfluxDB(){ InfluxDB db = InfluxDBFatory.connect("http://localhost:8086", "root", "root"); db.setDatabase("mydb"); return db; }
會(huì)看到:
自定義 HealthIndicator 也非常的方便,比如:
@Component public class MyHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); // perform some specific health check if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } }
5 Metric
Spring Boot Actuator 為 Micrometer 提供依賴管理和自動(dòng)配置,Micrometer 作用應(yīng)用程序指標(biāo)的 facade,可以支持各種類型的監(jiān)控系統(tǒng),包括:
AppOptics, Atlas, Datadog, Dynatrace, Elastic, Ganglia, Humio, Influx, JMX, KairosDB, New Relic, Prometheus, SignalFx, Simple(in-memory), StatsD, Wavefront.
基本概念:
- Meter,MeterRegistry,Metric
- Meter Type: Timer,Counter,Gauge,DistributionSummary,LongTaskTimer,F(xiàn)unctionCounter,F(xiàn)untionTimer,TImeGauge
- Tag
下面,將應(yīng)用的指標(biāo)信息都輸出到 InfluxDB 數(shù)據(jù)庫中,需要做以下配置:
在配置文件中,添加 micrometer-registry-influx
依賴包
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-influx</artifactId> </dependency>
然后再添加 InfluxmeterRegistry Bean 實(shí)例:
@Bean public InfluxMeterRegistry getMeterRegistry(@Autowired InfluxConfig config){ return new InfluxMeterRegistry(config, Clock.SYSTEM); }
然后會(huì)在本地 InfluxDB 中看到以下 measurements:
Spring Boot 默認(rèn)注冊(cè)的指標(biāo)以下幾類:
- JVM metrics
- CPU metrics
- File descriptor metrics
- Kafka consumer metrics
- Logback/Log4j2 metrics
- Uptime metrics
- Tomcat metrics
- Spring Integration metrics
到此這篇關(guān)于Spring中的Actuator使用詳解的文章就介紹到這了,更多相關(guān)Spring中的Actuator內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot數(shù)據(jù)庫初始化datasource配置方式
這篇文章主要為大家介紹了SpringBoot數(shù)據(jù)庫初始化datasource配置方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能示例
這篇文章主要介紹了Java模擬計(jì)算機(jī)的整數(shù)乘積計(jì)算功能,簡(jiǎn)單分析了計(jì)算機(jī)數(shù)值進(jìn)制轉(zhuǎn)換與通過位移進(jìn)行乘積計(jì)算的原理,并結(jié)合具體實(shí)例給出了java模擬計(jì)算機(jī)成績(jī)運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-09-09SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路
這篇文章主要介紹了SpringBoot如何自定義starter啟動(dòng)器,通過starter的自定義過程,能夠加深大家對(duì)SpringBoot自動(dòng)配置原理的理解,需要的朋友可以參考下2022-10-10Java 獲取Html文本中的img標(biāo)簽下src中的內(nèi)容方法
今天小編就為大家分享一篇Java 獲取Html文本中的img標(biāo)簽下src中的內(nèi)容方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06java數(shù)據(jù)庫操作類演示實(shí)例分享(java連接數(shù)據(jù)庫)
java數(shù)據(jù)庫操作類演示實(shí)例分享,大家參考使用吧2013-12-12JNDI簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了JNDI簡(jiǎn)介,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于springboot使用DynamicDataSource動(dòng)態(tài)切換數(shù)據(jù)源的實(shí)現(xiàn)過程,Spring Boot應(yīng)用中可以配置多個(gè)數(shù)據(jù)源,并根據(jù)注解靈活指定當(dāng)前使用的數(shù)據(jù)源,需要的朋友可以參考下2023-08-08