spring-boot-starter-actuator(健康监控)配置和使用

spring-boot-starter-actuator(健康监控)配置和使用

添加POM依赖:

<!-- spring-boot-监控-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.yml中指定监控的HTTP端口(如果不指定,则使用和Server相同的端口);指定去掉某项的检查(比如不监控health.mail):

server:
  port: 8083
management:
    port: 8083
    security:
      enabled: false  #

监控和管理端点

端点名描述
autoconfig所有自动配置信息( positiveMatches :运行的, negativeMatches 未运行组件)
auditevents审计事件
beans所有Bean的信息
configprops所有配置属性
dump线程状态信息
env当前环境信息
health应用健康状况
info当前应用信息
metrics应用的各项指标
mappings应用@RequestMapping映射路径
shutdown关闭当前应用(默认关闭)
trace追踪信息(最新的http请求)
heapdump下载内存快照

http://localhost:8083/info 读取配置文件application.properties的 info.*属性

在InfoProperties 读取

application.properties :

info.app.version=v1.2.0
info.app.name=abc

在GitProperties 获取git.properties 的信息

info.app.version=v1.2.0
info.app.name=abc
#远程关闭开启
endpoints.shutdown.enabled=true  
#访问:http://localhost:8083/shutdown   关闭服务

metrics

{
mem: 573549,   //内存大小
mem.free: 388198,  //内存剩余大小
processors: 4,  //处理器数量
instance.uptime: 338426,
uptime: 345091,
systemload.average: -1,
heap.committed: 489984,
heap.init: 131072,
heap.used: 101785,
heap: 1842688,
nonheap.committed: 85056,
nonheap.init: 2496,
nonheap.used: 83566,
nonheap: 0,
threads.peak: 46,
threads.daemon: 36,
threads.totalStarted: 72,
threads: 39,  //线程
classes: 12109,
classes.loaded: 12109,  //加载的类
classes.unloaded: 0,  //没加载的类
gc.ps_scavenge.count: 10,
gc.ps_scavenge.time: 103,
gc.ps_marksweep.count: 3,
gc.ps_marksweep.time: 219,
httpsessions.max: -1,
httpsessions.active: 0,
gauge.response.mappings: 3,
gauge.response.autoconfig: 4,
gauge.response.trace: 167,
counter.status.200.mappings: 1,
counter.status.200.autoconfig: 2,
counter.status.200.trace: 1
}

自定义配置说明:

#关闭metrics功能
endpoints.metrics.enabled=false
#开启shutdown远程关闭功能
endpoints.shutdown.enabled=true
#设置beansId
endpoints.beans.id=mybean
#设置beans路径
endpoints.beans.path=/bean
#关闭beans 功能
endpoints.beans.enabled=false
#关闭所有的
endpoints.enabled=false 
#开启单个beans功能
endpoints.beans.enabled=true
#所有访问添加根目录
management.context-path=/manage

management.port=8181

org.springframework.boot.actuate.health 包下对于所有的健康状态检查例如:RedisHealthIndicator ,当有redis的starter 时候就会检查

 {
    status: "DOWN", //状态
    diskSpace: {
    status: "UP",
    total: 395243941888,
    free: 367246643200,
    threshold: 10485760
    },
    rabbit: {
    status: "DOWN",
    error: "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect"
    },
    redis: {
    status: "UP",
    version: "4.0.9"
    },
    db: {
    status: "UP",
    database: "MySQL",
    hello: 1
    }
}

自定义health

  • 自定义健康状态指示器

  • 1、编写一个指示器 实现 HealthIndicator 接口

  • 2、指示器的名字 xxxxHealthIndicator

  • 3、加入容器中

    import org.springframework.boot.actuate.health.Health;
    import org.springframework.boot.actuate.health.HealthIndicator;
    import org.springframework.stereotype.Component;
    @Component
    public class MyAppHealthIndicator implements HealthIndicator {
    
        @Override
        public Health health() {
    
            //自定义的检查方法
            //Health.up().build()代表健康
            return Health.down().withDetail("msg","服务异常").build();
        }
    }

### Spring Boot Actuator 的基本配置 为了实现对 Spring Boot 应用的监控管理,`spring-boot-starter-actuator` 是一个非常重要的模块。以下是其最基本的配置方法: #### 1. 引入依赖 要在项目中使用 `Spring Boot Actuator`,首先需要在项目的 `pom.xml` 文件中添加以下 Maven 依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 通过上述方式可以轻松集成 Actuator 功能到应用程序中[^4]。 #### 2. 配置文件设置 完成依赖引入后,在 `application.properties` 或 `application.yml` 中进行必要的配置。默认情况下,Actuator 提供了一些端点(Endpoints),这些端点可以通过 HTTP 接口访问以获取应用的状态信息或其他数据。 ##### application.properties 示例 ```properties management.endpoints.web.exposure.include=health,info management.endpoint.health.show-details=always ``` 解释: - **`management.endpoints.web.exposure.include`**: 定义哪些端点可以通过 Web 访问,默认只暴露 `/actuator/health` `/actuator/info` 端点。 - **`management.endpoint.health.show-details`**: 控制健康状态详情是否显示给调用者,通常建议开发环境设为 `always` 而生产环境中更谨慎地控制权限[^5]。 如果希望开放更多端点,则可以在该属性中追加其他名称,例如 `metrics`, `env`, `beans` 等。 ##### application.yml 示例 对于 YAML 格式的配置文件,可按如下方式进行定义: ```yaml management: endpoints: web: exposure: include: health, info, metrics endpoint: health: show-details: always ``` 此部分同样用于指定允许公开的具体端点及其行为细节[^3]。 #### 3. 启动并测试服务 当以上两步完成后重启您的 Spring Boot 应用程序即可生效。此时可通过浏览器或者命令行工具像 Postman/cURL 来验证各个已开启的功能接口是否正常工作。例如尝试访问地址 http://localhost:<port>/actuator/health 将返回 JSON 形式的健康状况报告。 --- ### 注意事项 尽管 Actuator 极大地简化了运维操作流程,但也需要注意安全性方面的问题——尤其是涉及敏感信息泄露风险较高的某些特定端点时更是如此。因此推荐仅限内部网络环境下部署此类特性;另外还可以借助框架自带的安全机制如 Shiro 实现细粒度授权校验来进一步保护资源不被非法利用[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值