5分钟了解Actuator的使用

介绍

在你发布系统到生产环境时,SpringBoot 提供了一些额外的特征帮助你监控、管理你的应用。

开始

首先添加依赖到自己的工程

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

actuator使用http来暴露地址时,只有info、health是默认打开的,一个地址(端点)在enabled且exposed情况下才被认为是可用的。

下面是可以配置的地址(端点),配置health可用访问时要使用:/actuator/health

ID描述
info显示任意的程序信息
health显示应用的健康信息
beans显示应用里Spring beans的所有集合
mappings显示所有@RequestMapping路径的列表。

更多配置

使用

使用yml配置可用的地址

server.port: 8080
management:
  server:
    port: 8080
    address: 127.0.0.1
  endpoint:
    info:
      enabled: true
    health:
      enabled: true
    # 将错误的详细信息显示出来,包含自定义的详细信息
      show-details: always
    # 启用mappings功能
    mappings:
      enabled: true
  endpoints:
    web:
      # 把需要的地址暴露出来,实际访问是http://localhost:8080/actuator/mappings
      exposure:
        include: health,info,mappings
  health:
    # 不用检查磁盘空间的健康
    diskspace:
      enabled: false

使用java自定义health的信息

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // 执行健康检查
        if (errorCode != 0) {
            //错误情况下返回
            return Health.down().withDetail("Error Code", errorCode).withDetail("Error Message","出错了").build();
        }
        return Health.up().build();
    }

    private int check() {
        return 500;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值