Spring中的Actuator使用详解

 更新时间:2023年09月12日 09:26:45   作者:墨城之左  
这篇文章主要介绍了Spring中的Actuator使用详解,在生产环境中运行的程序,并不总是稳定、安静、正确的,往往会遇到各式各样的现场状况,这个时候,就需要获取该程序足够多的运行状态信息,然后分析并对其进行有效管理,需要的朋友可以参考下

1 Spring Actuator

在生产环境中运行的程序,并不总是稳定、安静、正确的,往往会遇到各式各样的现场状况,这个时候,就需要获取该程序足够多的运行状态信息,然后分析并对其进行有效管理。

Spring Boot Actuator 提供了多种特性来监控和管理应用程序,可以基于 HTTP,也可以基于 JMX。

将 actuator 依赖包添加到项目中

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

2 Endpoints

endpoint 可以理解为被管理(或被监控) 对象 ,actuator 就是通过这些 endpoint 来实现对应用程序的监控管理。

spring 提供了大量的内置 endpoint,比如 health,beans,mappings,endpoint 名称也称为 endpoint id:

ID描述默认开启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

对于 Web 应用,还有以下 endpoint:

ID描述默认开启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 属性来配置改变是否默认开启的方式。

management.endpoint.shutdown.enabled=true 
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

可以通过下面的属性修改 JMX/Web 的默认行为:

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

返回结果

{
    "request": {
        "mbean": "org.springframework.boot:type=Endpoint,name=Hello",
        "type": "exec",
        "operation": "hello"
    },
    "value": "Hello Endpoint",
    "timestamp": "xxxx",
    "status": 200    
}

有关于 MBean 的详细信息的格式,可以通过 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 默认添加了以下 HealthIndicator:

  • CassandraHealthIndicator
  • CouchbaseHealthIndicator
  • DiskSpaceHealthIndicator
  • DataSourceHealthIndicator
  • ElasticsearchHealthIndicator
  • InfluxDbHealthIndicator
  • JmsHealthIndicator
  • MailHealthIndicator
  • MongoHealthIndicator
  • Neo4jHealthIndicator
  • RabbitHealthIndicator
  • RedisHealthIndicator
  • SolrHealthIndicator

比如,当 Spring 容易中有 InfluxDB bean 时,Spring Actuator 就会自动添加对 InfluxDB health 的检测。

@Bean
public InfluxDB getInfluxDB(){
	InfluxDB db = InfluxDBFatory.connect("http://localhost:8086", "root", "root");
	db.setDatabase("mydb");
	return db;
}

会看到:

在这里插入图片描述

自定义 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 提供依赖管理和自动配置,Micrometer 作用应用程序指标的 facade,可以支持各种类型的监控系统,包括:

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,FunctionCounter,FuntionTimer,TImeGauge
  • Tag

下面,将应用的指标信息都输出到 InfluxDB 数据库中,需要做以下配置:

在配置文件中,添加 micrometer-registry-influx 依赖包

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-influx</artifactId>
</dependency>

然后再添加 InfluxmeterRegistry Bean 实例:

@Bean
public InfluxMeterRegistry getMeterRegistry(@Autowired InfluxConfig config){
	return new InfluxMeterRegistry(config, Clock.SYSTEM);
}

然后会在本地 InfluxDB 中看到以下 measurements:

在这里插入图片描述

Spring Boot 默认注册的指标以下几类:

  • JVM metrics
  • CPU metrics
  • File descriptor metrics
  • Kafka consumer metrics
  • Logback/Log4j2 metrics
  • Uptime metrics
  • Tomcat metrics
  • Spring Integration metrics

到此这篇关于Spring中的Actuator使用详解的文章就介绍到这了,更多相关Spring中的Actuator内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 都9102年了,你还用for循环操作集合吗

    都9102年了,你还用for循环操作集合吗

    这篇文章主要给大家介绍了关于java中for循环操作集合使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • 详解Java编程中Annotation注解对象的使用方法

    详解Java编程中Annotation注解对象的使用方法

    这篇文章主要介绍了Java编程中Annotation注解对象的使用方法,注解以"@注解名"的方式被编写,与类、接口、枚举是在同一个层次,需要的朋友可以参考下
    2016-03-03
  • java实现多客户聊天功能

    java实现多客户聊天功能

    这篇文章主要为大家详细介绍了java实现多客户聊天功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 关于SpringBoot的自动装配原理详解

    关于SpringBoot的自动装配原理详解

    这篇文章主要介绍了关于SpringBoot的自动装配原理详解,Spring Boot自动装配原理是指Spring Boot在启动时自动扫描项目中的依赖关系,根据依赖关系自动配置相应的Bean,从而简化了Spring应用的配置过程,需要的朋友可以参考下
    2023-07-07
  • flink进阶富函数生命周期介绍

    flink进阶富函数生命周期介绍

    这篇文章主要为大家介绍了flink进阶富函数生命周期的举例介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Java web Hibernate如何与数据库链接

    Java web Hibernate如何与数据库链接

    这篇文章主要介绍了Java web Hibernate如何与数据库链接,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • java 如何判断是否可以ping通某个地址

    java 如何判断是否可以ping通某个地址

    这篇文章主要介绍了java 如何判断是否可以ping通某个地址,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Spring如何集成ibatis项目并实现dao层基类封装

    Spring如何集成ibatis项目并实现dao层基类封装

    这篇文章主要介绍了Spring如何集成ibatis项目并实现dao层基类封装,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • SpringBoot对Filter过滤器中的异常进行全局处理方案详解

    SpringBoot对Filter过滤器中的异常进行全局处理方案详解

    这篇文章主要介绍了SpringBoot对Filter过滤器中的异常进行全局处理,在SpringBoot中我们通过 @ControllerAdvice 注解和 @ExceptionHandler注解注册了全局异常处理器,需要的朋友可以参考下
    2023-09-09
  • Java 锁的知识总结及实例代码

    Java 锁的知识总结及实例代码

    这篇文章主要介绍了Java 锁的知识总结及实例代码,需要的朋友可以参考下
    2016-09-09

最新评论