欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中的Actuator使用詳解

 更新時(shí)間:2023年09月12日 09:26:45   作者:墨城之左  
這篇文章主要介紹了Spring中的Actuator使用詳解,在生產(chǎn)環(huán)境中運(yùn)行的程序,并不總是穩(wěn)定、安靜、正確的,往往會(huì)遇到各式各樣的現(xiàn)場(chǎng)狀況,這個(gè)時(shí)候,就需要獲取該程序足夠多的運(yùn)行狀態(tài)信息,然后分析并對(duì)其進(jìn)行有效管理,需要的朋友可以參考下

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)開啟JMXWEB
auditeventsExposes audit events information for the current application.YesYesNo
beansDisplays a complete list of all the Spring beans in your application.YesYesNo
cachesExposes available caches.YesYesNo
conditionsShows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.YesYesNo
configpropsDisplays a collated list of all @ConfigurationProperties.YesYesNo
envExposes properties from Spring’s ConfigurableEnvironment.YesYesNo
flywayShows any Flyway database migrations that have been applied.YesYesNo
healthShows application health information.YesYesYes
httptraceDisplays HTTP trace information (by default, the last 100 HTTP request-response exchanges).YesYesNo
infoDisplays arbitrary application info.YesYesYes
integrationgraphShows the Spring Integration graph.YesYesNo
loggersShows and modifies the configuration of loggers in the application.YesYesNo
liquibaseShows any Liquibase database migrations that have been applied.YesYesNo
metricsShows ‘metrics’ information for the current application.YesYesNo
mappingsDisplays a collated list of all @RequestMapping paths.YesYesNo
scheduledtasksDisplays the scheduled tasks in your application.YesYesNo
sessionsAllows 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.YesYesNo
shutdownLets the application be gracefully shutdown.NoYesNo
threaddumpPerforms a thread dump.YesYesNo

對(duì)于 Web 應(yīng)用,還有以下 endpoint:

ID描述默認(rèn)開啟JMXWEB
heapdumpReturns a GZip compressed hprof heap dump file.YesN/ANo
jolokiaExposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux).YesN/ANo
logfileReturns 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.YesN/ANo
prometheusExposes metrics in a format that can be scraped by a Prometheus server.YesN/ANo

可以通過 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)文章

最新評(píng)論