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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JavaWeb中Servlet的深入理解

    JavaWeb中Servlet的深入理解

    Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层
    2021-10-10
  • SpringBoot数据库初始化datasource配置方式

    SpringBoot数据库初始化datasource配置方式

    这篇文章主要为大家介绍了SpringBoot数据库初始化datasource配置方式,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • Java模拟计算机的整数乘积计算功能示例

    Java模拟计算机的整数乘积计算功能示例

    这篇文章主要介绍了Java模拟计算机的整数乘积计算功能,简单分析了计算机数值进制转换与通过位移进行乘积计算的原理,并结合具体实例给出了java模拟计算机成绩运算的相关操作技巧,需要的朋友可以参考下
    2017-09-09
  • SpringBoot自定义starter启动器的实现思路

    SpringBoot自定义starter启动器的实现思路

    这篇文章主要介绍了SpringBoot如何自定义starter启动器,通过starter的自定义过程,能够加深大家对SpringBoot自动配置原理的理解,需要的朋友可以参考下
    2022-10-10
  • SpringMVC中的拦截器详解及代码示例

    SpringMVC中的拦截器详解及代码示例

    这篇文章主要介绍了SpringMVC中的拦截器详解及代码示例,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Java 获取Html文本中的img标签下src中的内容方法

    Java 获取Html文本中的img标签下src中的内容方法

    今天小编就为大家分享一篇Java 获取Html文本中的img标签下src中的内容方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • java数据库操作类演示实例分享(java连接数据库)

    java数据库操作类演示实例分享(java连接数据库)

    java数据库操作类演示实例分享,大家参考使用吧
    2013-12-12
  • JNDI简介_动力节点Java学院整理

    JNDI简介_动力节点Java学院整理

    这篇文章主要介绍了JNDI简介,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • springboot使用DynamicDataSource动态切换数据源的实现过程

    springboot使用DynamicDataSource动态切换数据源的实现过程

    这篇文章主要给大家介绍了关于springboot使用DynamicDataSource动态切换数据源的实现过程,Spring Boot应用中可以配置多个数据源,并根据注解灵活指定当前使用的数据源,需要的朋友可以参考下
    2023-08-08
  • Java POI库从入门到精通举例详解

    Java POI库从入门到精通举例详解

    Apache POI是一个开源项目,能够让Java程序员读取和写入Microsoft Office格式的文件,包括Excel、Word和PowerPoint等,本文详细介绍了POI库的安装、结构与功能,以及如何在Java中进行基本操作和进阶应用,需要的朋友可以参考下
    2024-10-10

最新评论