SpringBoot actuator 一

  本篇文章主要介绍SpringBoot项目actuator配置相关内容。方便大家更好的使用actuator,为项目的维护提供便利。如果文章中有错误或不明确的地方,请大家望指正,谢谢!

介绍

  springboot actuator,可以帮助您在将应用程序推送到生产环境时监视和管理它。您可以选择使用HTTP端点或JMX来管理和监视应用程序。审核、运行状况和度量收集也可以自动应用于您的应用程序。

使用

pom.xml

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

配置后,就可以通过web形式访问来获取一些应用运行信息

启动日志

192.168.xx.xxx-myapp-45516-1321 [main] INFO  com.wm8.logging.Application - Starting Application on XXX with PID 45516 (C:\tool\workspace\logging\target\classes started by XXX in C:\tool\workspace\logging) 
192.168.xx.xxx-myapp-45516-1326 [main] INFO  com.wm8.logging.Application - No active profile set, falling back to default profiles: default 
192.168.xx.xxx-myapp-45516-4161 [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http) 
192.168.xx.xxx-myapp-45516-4185 [main] INFO  o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"] 
192.168.xx.xxx-myapp-45516-4186 [main] INFO  o.a.catalina.core.StandardService - Starting service [Tomcat] 
192.168.xx.xxx-myapp-45516-4186 [main] INFO  o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.37] 
192.168.xx.xxx-myapp-45516-4368 [main] INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext 
192.168.xx.xxx-myapp-45516-4368 [main] INFO  o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 2934 ms 
192.168.xx.xxx-myapp-45516-5311 [main] INFO  o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor' 
192.168.xx.xxx-myapp-45516-5685 [main] INFO  o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator' 
192.168.xx.xxx-myapp-45516-5725 [main] INFO  o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"] 
192.168.xx.xxx-myapp-45516-5780 [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8080 (http) with context path '' 
192.168.xx.xxx-myapp-45516-5806 [main] INFO  com.wm8.logging.Application - Started Application in 5.407 seconds (JVM running for 6.104) 

通过输出日志,可以看到对外暴露了两个端口,且base-path='/actuator'

http://127.0.0.1:8080/actuator/health

{"status":"UP"}

http://127.0.0.1:8080/actuator/info

{}

 

Endpoints介绍

关于这个词的翻译是终端,但是总感觉有些不易理解,我更喜欢把它称为外部访问接口。

  

IDDescription

auditevents

当前应用的审核事件日志. 需要在项目中定义AuditEventRepository的实现类,添加注解@Component

beans

显示应用系统中Spring beans集合.

caches

显示可用的缓存.

conditions

Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.

configprops

Displays a collated list of all @ConfigurationProperties.

env

Exposes properties from Spring’s ConfigurableEnvironment.

flyway

Shows any Flyway database migrations that have been applied. Requires one or more Flyway beans.

health

显示应用健康信息.

httptrace

显示http请求的信息,默认显示最后100条。需要实现HttpTraceRepository 

info

显示应用信息,添加github插件。加载github配置文件信息.

integrationgraph

Shows the Spring Integration graph. Requires a dependency on spring-integration-core.

loggers

显示和修改应用中loggers的配置

liquibase

Shows any Liquibase database migrations that have been applied. Requires one or more Liquibase beans.

metrics

Shows ‘metrics’ information for the current application.

mappings

Displays a collated list of all @RequestMapping paths.

scheduledtasks

Displays the scheduled tasks in your application.

sessions

Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.

shutdown

Lets the application be gracefully shutdown. Disabled by default.

 web项目中,还可以配置如下接口

IDDescription

heapdump

Returns an hprof heap dump file.

jolokia

Exposes JMX beans over HTTP (when Jolokia is on the classpath, not available for WebFlux). Requires a dependency on jolokia-core.

logfile

Returns the contents of the logfile (if logging.file.name or logging.file.path properties have been set). Supports the use of the HTTP Range header to retrieve part of the log file’s content.

prometheus

Exposes metrics in a format that can be scraped by a Prometheus server. Requires a dependency on micrometer-registry-prometheus.

 

Endpoint可用

management:
  endpoint:
    health:
      enabled: true

开放Endpoint

 默认支持情况:

IDJMXWeb

auditevents

Yes

No

beans

Yes

No

caches

Yes

No

conditions

Yes

No

configprops

Yes

No

env

Yes

No

flyway

Yes

No

health

Yes

Yes

heapdump

N/A

No

httptrace

Yes

No

info

Yes

Yes

integrationgraph

Yes

No

jolokia

N/A

No

logfile

N/A

No

loggers

Yes

No

liquibase

Yes

No

metrics

Yes

No

mappings

Yes

No

prometheus

N/A

No

scheduledtasks

Yes

No

sessions

Yes

No

shutdown

Yes

No

threaddump

Yes

No

 通过配置来开放endpoint,多个时用逗号拼接

PropertyDefault

management.endpoints.jmx.exposure.exclude

 

management.endpoints.jmx.exposure.include

*

management.endpoints.web.exposure.exclude

 

management.endpoints.web.exposure.include

info, health

 

示例:

management:
  endpoints:
    web:
      exposure:
        include: '*'
        exclude: env,beans
        

开放所有除了evn,beans

 

推荐使用的配置

management:
  endpoint:
    shutdown:
      enabled: false
    health:
      show-details: always

 

posted @ 2021-01-06 16:44  硬石头  阅读(185)  评论(0)    收藏  举报