springboot actuator

SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等

1.加入maven依赖

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

加入依赖后就可以用 http://localhost:8080/actuator 查看信息

2.修改配置文件

management:
  endpoints:
    enabled-by-default: true  #默认开启所有监控端点 true
    web:
      exposure:
        include: '*'  # 以web方式暴露所有端点

  endpoint:
    health:
      show-details: always  #显示health详细信息
      enabled: true #开启heakth监控端点

info:	#这部分是自己添加的内容
  appName: boot-actuator
  appVersion: 1.0.0
  mavenProjectName: @project.artifactId@	#从pom.xml中获取值
  mavenProjectVersion: @project.version@

3.在配置文件中使用@时出错,需要在pom.xml的build中加入以下内容

        <!--为了能使用@@-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

4.自己添加的信息也可以在代码中实习

@Component
public class AppInfoContributor implements InfoContributor {
    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("msg","hello")
                .withDetails(Collections.singletonMap("world","11111"));
    }
}

5.可以修改原有的内容

@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {
    /**
     * 真实的检查方法
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        Map<String, Object> map = new HashMap<>();
        if (false){
            builder.status(Status.UP);
            map.put("count",1);
            map.put("ms",100);
        }else {
            builder.status(Status.DOWN);
            map.put("err","连接超时");
            map.put("ms",3000);
        }
        builder.withDetail("code",100)
                .withDetails(map);
    }
}

6.测试结果

http://localhost:8080/actuator

在这里插入图片描述

http://localhost:8080/actuator/health

在这里插入图片描述

http://localhost:8080/actuator/info

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值