栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot actuator

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot actuator

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

1.加入maven依赖

        
            org.springframework.boot
            spring-boot-starter-actuator
        

加入依赖后就可以用 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中加入以下内容

        
        
            
                src/main/resources
                true
            
        

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 {
    
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        Map 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号