Spring Boot Actuator

Actuator简介

Actuator是springboot中,用于对应用程序进行监视和管理的功能,能够获取到应用的运行情况和运行环境.:健康检查、审计、统计、HTTP追踪、环境信息。

Maven依赖:

<!--springboot依赖省略-->
<!--Acuator依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>

使用简介

在sping boot 项目中要使用监控功能,最简单的只需要一步,就是添加Actuator的依赖,至于配置,可以使用默认的,就是不做任何配置,添加依赖后直接使用。启动application然后访问 http://localhost:8080/actuator
在这里插入图片描述
这里的health和info 就是默认的监控信息,也就是默认端点(endpoint) ,访问其中的url 就可以看到状态。至于更多的端点,请看下图(全部端点请查最下方官网链接)
在这里插入图片描述
上面是最简单的一个例子,导入依赖直接使用,但是通常我们要做一下配置,以下是端点(endpoint)的配置:application.yml

management: 
  server: 
    point: 8888    #不设置的话是默认端口springboot的端口,现在是 8888
  endpoints:
    # 暴露 EndPoint 以供访问,有jmx和web两种方式,
    jmx:
      exposure:
        exclude: '*'  //!!!注意!!!如果是yml配置文件一定要是"*"或者'*'
                      //!!!注意!!!如果是.properties一定是*不能加引号或者双引号
        include: '*'
    web: #大部分应用是web应用
      exposure:
      # exclude: '*'   exclude 的优先级高于 include,
        include: ["health","info","beans","mappings","logfile","metrics","shutdown","env"] #或者 '*'
      base-path: /actuator  # 配置 Endpoint 的基础路径不配置是/actuator
      path-mapping: 
         health: healthcheck #配置health 映射为/actuator/healthcheck 默认是/actuator/health
      cors: # 配置跨域资源共享
        allowed-origins: http://example.com
        allowed-methods: GET,POST
    enabled-by-default: true # 修改全局 endpoint 默认设置,如果开启false,
                        #表示全部端点禁用,需要在endpoint中配置需要开启的端点
  endpoint:
    auditevents: # 显示当前引用程序的审计事件信息,默认开启
      enabled: true
      cache:
        time-to-live: 10s # 配置端点缓存响应的时间
    beans: #显示一个应用中所有 Spring Beans 的完整列表,默认开启
      enabled: true
    conditions: # 显示配置类和自动配置类的状态及它们被应用和未被应用的原因,默认开启
      enabled: true
    configprops: # 显示一个所有@ConfigurationProperties的集合列表,默认开启
      enabled: true
    env: #显示来自Spring的 ConfigurableEnvironment的属性,默认开启
      enabled: true
    flyway: # 显示数据库迁移路径,如果有的话,默认开启
      enabled: true
    health: # 显示健康信息,默认开启
      enabled: true
      show-details: always   #这个设置会显示所有health指标中的详细信息,
                             #不设置只显示UP或者DOWN状态
    info: # 显示任意的应用信息,默认开启
      enabled: true
    liquibase: # 展示任何Liquibase数据库迁移路径,如果有的话,默认开启
      enabled: true
    metrics: # 展示当前应用的metrics信息,默认开启
      enabled: true
    mappings: # 显示一个所有@RequestMapping路径的集合列表,默认开启
      enabled: true
    scheduledtasks: #显示应用程序中的计划任务,默认开启
      enabled: true
    sessions: #允许从Spring会话支持的会话存储中检索和删除用户会话。
           #使用Spring Session对反应性Web应用程序的支持时不可用。默认开启。
      enabled: true
    shutdown: # 允许应用以优雅的方式关闭,默认关闭
      enabled: true
    threaddump: # 执行一个线程dump
      enabled: true
    # web 应用时可以使用以下端点
    heapdump: # 返回一个GZip压缩的hprof堆dump文件,默认开启
      enabled: true
    jolokia: # 通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用),默认开启
      enabled: true
    logfile: # 返回日志文件内容(如果设置了logging.file或logging.path属性的话),
             # 支持使用HTTP Range头接收日志文件内容的部分信息,默认开启
      enabled: true
    prometheus: # 以可以被Prometheus服务器抓取的格式显示metrics信息,默认开启
      enabled: true

Endpoint映射默认前缀是/actuator,但是可以通过base-path 自定义。
每个endpoint光开启是不够的,还需要先通过jmx或者web去暴露他们,通过exclude(排除)和include(包含)属性配置,大多数的应用程序选择web(http)公开,大白话就是先要对endpoints进行一个统一的设置,先设置暴露(exposure)只有暴露了才可以在页面中访问,暴露了以后再去endpoint中进行单个端点进行设置,不暴露的话在端点中设置了也访问不了。

下面做一个简单实例
导入依赖后在applicatio.yml中添加入如下内容

server:
  port: 8080
management:
  endpoints:
    web:
      exposure:
        include: "*"  #所有的默认端点都暴露,只有一个shutdown端点需要另外配置
      base-path: /actuator
  endpoint:  #端点中对health进行配置
    health:
      show-details: ALWAYS

写一个最简单的启动类:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

访问 http:localhost:8080/actuator 返回的信息如下

{
“_links”: {
“self”: {
“href”: “http://localhost:8080/actuator”,
“templated”: false
},
“auditevents”: {
“href”: “http://localhost:8080/actuator/auditevents”,
“templated”: false
},
“beans”: {
“href”: “http://localhost:8080/actuator/beans”,
“templated”: false
},
“caches-cache”: {
“href”: “http://localhost:8080/actuator/caches/{cache}”,
“templated”: true
},
“caches”: {
“href”: “http://localhost:8080/actuator/caches”,
“templated”: false
},
“health”: {
“href”: “http://localhost:8080/actuator/health”,
“templated”: false
},
“health-component”: {
“href”: “http://localhost:8080/actuator/health/{component}”,
“templated”: true
},
“health-component-instance”: {
“href”: “http://localhost:8080/actuator/health/{component}/{instance}”,
“templated”: true
},
“conditions”: {
“href”: “http://localhost:8080/actuator/conditions”,
“templated”: false
},
“configprops”: {
“href”: “http://localhost:8080/actuator/configprops”,
“templated”: false
},
“env”: {
“href”: “http://localhost:8080/actuator/env”,
“templated”: false
},
“env-toMatch”: {
“href”: “http://localhost:8080/actuator/env/{toMatch}”,
“templated”: true
},
“info”: {
“href”: “http://localhost:8080/actuator/info”,
“templated”: false
},
“loggers”: {
“href”: “http://localhost:8080/actuator/loggers”,
“templated”: false
},
“loggers-name”: {
“href”: “http://localhost:8080/actuator/loggers/{name}”,
“templated”: true
},
“heapdump”: {
“href”: “http://localhost:8080/actuator/heapdump”,
“templated”: false
},
“threaddump”: {
“href”: “http://localhost:8080/actuator/threaddump”,
“templated”: false
},
“metrics”: {
“href”: “http://localhost:8080/actuator/metrics”,
“templated”: false
},
“metrics-requiredMetricName”: {
“href”: “http://localhost:8080/actuator/metrics/{requiredMetricName}”,
“templated”: true
},
“scheduledtasks”: {
“href”: “http://localhost:8080/actuator/scheduledtasks”,
“templated”: false
},
“httptrace”: {
“href”: “http://localhost:8080/actuator/httptrace”,
“templated”: false
},
“mappings”: {
“href”: “http://localhost:8080/actuator/mappings”,
“templated”: false
}
}
}

可以看到返回了很多的端点信息,至于想要暴露哪些端点信息,可以通过上方的全部配置中选择配置。

总结:

1 导入依赖
2 application.yml 添加端点配置(前提要了解对应端点的意思)

对于有自定义端点或者需要了解每个端点详解的请看下方链接

Actuator官网
自定义端点
端点详解
Actuator整合Security

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值