SpringBoot监控组件Actuator

329 阅读3分钟

前言

Spring Boot 自1.5.X中引入的一个新的actuator端点:/loggers ,该端点可以查看日志级别的情况,也可以通过发送http请求的方式不重启动态修改日志级别。 该功能的使用非常简单,它依然延续了Spring Boot自动化配置的实现,所以只需要在引入了spring-boot-starter-actuator依赖 , 就会自动开启该端点的功能 ,SpringBoot2的话只需要通过简单的配置就可以开启。这里只简单介绍了actuator中的loggers端点,更多actuator的监控功能请自行查阅spring官方文档。

引入依赖

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

编写配置

1.5.x版

#禁用认证
management:
  security:
    enabled: false
endpoints:
  #关闭所有端点
  enabled: false
  loggers:
    enabled: true

2.x版

2.x版默认只开启了health与info端点,并且不再需要认证,我们可以通过include与exclude来设定我们开启的范围,也可以通过默认禁用所有,再自定义需要开启端点的形式来实现我们的需求。这里使用自定义开启端点的形式

management:
  endpoints:
    #web能访问的范围
    web:
     exposure:
        include: "*"
    #默认是否开启(必须要配置了web能访问的范围开启关闭才有效)
    enabled-by-default: false
  #开启的端点,按需打开(语法management.endpoint.端点.enabled=true)
  endpoint:
    loggers:
      enabled: true

动态修改日志级别

查看日志级别

请求方式路径说明
GET/loggers查看所有日志级别
GET/loggers/包名查看某个包的日志级别,如/loggers/com.test

发起查询请求

###
GET {{baseUrl}}/actuator/loggers/com.test
Accept: */*
Cache-Control: no-cache

响应结果

HTTP/1.1 200
Content-Disposition: inline;filename=f.txt
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Wed, 16 Dec 2020 07:33:27 GMT
Keep-Alive: timeout=60
Connection: keep-alive

{
  "configuredLevel": null,
  "effectiveLevel": "INFO"
}

修改日志级别

请求方式路径请求头入参说明
POST/loggers/包名application/json{"configuredLevel":"${logLevel}"}修改某个包的日志级别,如/loggers/com.zatech.gaia,报文中configuredLevel后面写需要变更为哪个日志级别

发起修改请求

###
POST {{baseUrl}}/actuator/loggers/com.test
Accept: */*
Cache-Control: no-cache
Content-Type: application/json

{"configuredLevel":"DEBUG"}

再次发起查询请求

###
GET {{baseUrl}}/actuator/loggers/com.test
Accept: */*
Cache-Control: no-cache

响应结果

HTTP/1.1 200
Content-Disposition: inline;filename=f.txt
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Wed, 16 Dec 2020 07:38:03 GMT
Keep-Alive: timeout=60
Connection: keep-alive

{
  "configuredLevel": "DEBUG",
  "effectiveLevel": "DEBUG"
}

常用端点

IDDescription
auditeventsExposes audit events information for the current application. Requires an AuditEventRepository bean.
beansDisplays a complete list of all the Spring beans in your application.
cachesExposes available caches.
conditionsShows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.
configpropsDisplays a collated list of all @ConfigurationProperties.
envExposes properties from Spring’s ConfigurableEnvironment.
flywayShows any Flyway database migrations that have been applied. Requires one or more Flyway beans.
healthShows application health information.
httptraceDisplays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.
infoDisplays arbitrary application info.
integrationgraphShows the Spring Integration graph. Requires a dependency on spring-integration-core.
loggersShows and modifies the configuration of loggers in the application.
liquibaseShows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.
metricsShows ‘metrics’ information for the current application.
mappingsDisplays a collated list of all @RequestMapping paths.
scheduledtasksDisplays the scheduled tasks in your application.
sessionsAllows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.
shutdownLets the application be gracefully shutdown. Disabled by default.
startupShows the docs.spring.io/spring-boot… collected by the ApplicationStartup. Requires the SpringApplication to be configured with a BufferingApplicationStartup.
threaddumpPerforms a thread dump.

关于actuator更多请查看spring官方