八. springboot 的指标监控 (3、定制 Endpoint )

2023-11-17

 3、定制 Endpoint

   3.1、定制 Health 信息

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }
}


构建Health
Health build = Health.down()
                .withDetail("msg", "error service")
                .withDetail("code", "500")
                .withException(new RuntimeException())
                .build();
management:
    health:
      enabled: true
      show-details: always #总是显示详细信息。可显示每个模块的状态信息
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {

    /**
     * 真实的检查方法
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //mongodb。  获取连接进行测试
        Map<String,Object> map = new HashMap<>();
        // 检查完成
        if(1 == 2){
//            builder.up(); //健康
            builder.status(Status.UP);
            map.put("count",1);
            map.put("ms",100);
        }else {
//            builder.down();
            builder.status(Status.OUT_OF_SERVICE);
            map.put("err","连接超时");
            map.put("ms",3000);
        }

        builder.withDetail("code",100)
                .withDetails(map); 
    }
}

  1.2、定制info信息

常用两种方式

1、编写配置文件

info:
  appName: boot-admin
  version: 2.0.1
  mavenProjectName: @project.artifactId@  #使用@@可以获取maven的pom文件值
  mavenProjectVersion: @project.version@

2、编写InfoContributor

import java.util.Collections;

import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

@Component
public class ExampleInfoContributor implements InfoContributor {

    @Override
    public void contribute(Info.Builder builder) {
        builder.withDetail("example",
                Collections.singletonMap("key", "value"));
    }

}

http://localhost:8080/actuator/info 会输出以上方式返回的所有info信息

  1.3、定制Metrics信息

1、SpringBoot支持自动适配的Metrics

  • JVM metrics, report utilization of:
    • Various memory and buffer pools
    • Statistics related to garbage collection
    • Threads utilization
    • Number of classes loaded/unloaded
  • CPU metrics
  • File descriptor metrics
  • Kafka consumer and producer metrics
  • Log4j2 metrics: record the number of events logged to Log4j2 at each level
  • Logback metrics: record the number of events logged to Logback at each level
  • Uptime metrics: report a gauge for uptime and a fixed gauge representing the application’s absolute start time
  • Tomcat metrics (server.tomcat.mbeanregistry.enabled must be set to true for all Tomcat metrics to be registered)
  • Spring Integration metrics

2、增加定制Metrics

class MyService{
    Counter counter;
    public MyService(MeterRegistry meterRegistry){
         counter = meterRegistry.counter("myservice.method.running.counter");
    }

    public void hello() {
        counter.increment();
    }
}


//也可以使用下面的方式
@Bean
MeterBinder queueSize(Queue queue) {
    return (registry) -> Gauge.builder("queueSize", queue::size).register(registry);
}

4、定制Endpoint

@Component
@Endpoint(id = "container")
public class DockerEndpoint {


    @ReadOperation
    public Map getDockerInfo(){
        return Collections.singletonMap("info","docker started...");
    }

    @WriteOperation
    private void restartDocker(){
        System.out.println("docker restarted....");
    }

}

场景:开发ReadinessEndpoint来管理程序是否就绪,或者LivenessEndpoint来管理程序是否存活;

当然,这个也可以直接使用 Production-ready Features

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

八. springboot 的指标监控 (3、定制 Endpoint ) 的相关文章

随机推荐

  • typora 编辑器基本用法

    Markdown 由 Daring Fireball 创建 原始指南在 这里 但是 它的语法因不同的解析器或编辑器而异 Typora 正在使用 GitHub Flavored Markdown 1 段落换行 按 Shift Return 可
  • Unity3D碰撞后去掉相互之间的反弹力

    最近做一个小游戏的时候发现 小模型碰撞到墙壁之后会有一个小小的反弹力导致模型有一个微弱的回弹位移 这样给人一种不好的感觉 研究了一下 除了 rigidbody Freeze Rotation之外 在FixedUpdate 注意这里是物理特性
  • mysql 正序_请问mysql 中 怎么实现这种排序,按照状态排序正序,再按照开始时间排序正序,...

    展开全部 有两个思路 1 按照各自的活动状态先排序 插入到临时表 最后再union all所有结32313133353236313431303231363533e58685e5aeb931333363353862果集create tempo
  • calico分配网络使k8s节点指定固定网段

    文章目录 calico分配网络使k8s节点指定固定网段 1 配置calicoctl 1 1 下载calicoctl 1 2 配置calicoctl 1 3 测试calicoctl 2 配置ippool 3 添加ippool 4 创建pod测
  • vue3`

    1 ref是一个函数 响应式数据 使用前要引入 impor ref fro vue let name ref 张三 改变数据 name value 李四 vue3对不同对象处理不同 数据劫持才是响应式的根本 getset 处理对象用prox
  • SpringBoot生成二维码

    目录 Zxing原生方式 添加依赖 二维码生成工具类 添加Controller 添加测试页面 使用postman测试效果 Hutool的方式 添加依赖 创建QRCodeService 添加Controller 效果测试 我们使用两种方式 去
  • mutex_init() / mutex_lock() / mutex_unlock()

    请求 1 初始化互斥体 mutex init 2 获得互斥体 mutex lock 3 释放互斥体 mutex unlock 1 mutex init 注意mutex使用之前都需要先init void mutex init struct m
  • 最小交换次数-华为OD

    整数数组nums 整数k 输出将数组A中小于k的整数组合到一起的最小交换次数 组合在一起是指满足条件的数字相邻 不要求相邻后在数组中的位置 样例1 nums 1 3 1 4 0 k 2 输出 1 解析 交换第一个1和4 样例2 nums 0
  • Django跳坑:objects.all()、objects.get()与objects.filter()之间的区别

    文章目录 1 三者之间的区别 2 获取数据 2 1 取单个数据 3 序列化 3 1 QuerySet序列化 3 2 models序列化 1 三者之间的区别 all返回的是QuerySet对象 程序并没有真的在数据库中执行SQL语句查询数据
  • Github账号开启账号双重验证

    Github账号开启账号双重验证 发现问题 解决步骤 插件使用 发现问题 今天在浏览开源项目的时候 突然Github有个提示我要在10月12日前开启双重验证 说是不完成的话 到时候的Github账号会受到限制 如下图 通过设置也可以找到 解
  • win11设置任务栏不合并的方法教程

    win11系统的任务栏窗口默认设置是合并的 有些小伙伴表示用起来还不太习惯 那么win11任务栏怎么设置不合并呢 下面小编为大家分享下win11设置任务栏不合并的方法 感兴趣的小伙伴一起来看看吧 win11设置任务栏不合并的方法教程 1 我
  • Elasticsearch学习笔记2:ES核心概念 -- 索引、倒排索引、类型、文档

    一 ES和关系型数据库的对比 Elasticsearch Relational DB 索引 index 数据库 database 类型 types 表 tables 文档 documents 行 rows 字段 fields 列 colum
  • OLED透明屏报价:实现高质量展示的成本与选择

    引言 OLED透明屏作为商业展示领域的新兴技术 受到了广泛的关注和需求 然而 对于OLED透明屏的报价 人们常常存在疑虑 在这篇文章中 尼伽将详细解析OLED透明屏报价的构成和选择因素 希望能帮助您更好呢地了解OLED透明屏 一 OLED透
  • vue中textarea高度的设置_vue中textarea自适应高度

    HTML data return pltxt 评论 inputText isHeight true minHeight 0 methods autoTextarea var extra 0 设置光标与输入框保持的距离 默认0 maxHeig
  • SQL操作

    一 查询语句 1 基本查询 SELECT FROM lt 表名 gt 查询表的所有行 SELECT FROM students 2 条件查询 SELECT FROM lt 表名 gt WHERE lt 条件表达式 gt 查询分数在80分以上
  • vscode+phpstudy连接使用mysql(解决phpstudy中mysql无法启动的问题)

    vscode phpstudy连接使用mysql 解决phpstudy中mysql无法启动的问题 使用vscode phpstudy配置php开发环境网上很文章都是挺好的 都成功解决了我的问题 但是对于使用mysql方面始终找不到很系统的文
  • 数据结构系列——先进先出队列queue

    本期主题 先进先出队列实现 目录 1 队列定义 2 实现一个简单的队列以及分析 1 代码实现分析 2 code 3 优缺点分析 3 循环队列实现 1 循环队列原理 2 循环队列实现分析 3 code 1 队列定义 队列是什么 定义 一个先进
  • unity,网格碰撞器(Mesh Collider)

    介绍 网格碰撞器 Mesh Collider 在实现物理碰撞检测时 可以自动检测凸面 但是它并不总是能够准确地生成凸多面体 这是因为在将一个网格模型转换为凸多面体的过程中 可能会出现模型内部空洞或者交叉的情况 这些情况会导致凸多面体的生成失
  • H5页面长按识别二维码

    vue 写的H5 内嵌在小程序上 img src 图片路径 style width 200px height 200px 直接在微信访问长按就可以实现 微信原生直接写长按识别二维码 aaa e let img e target datase
  • 八. springboot 的指标监控 (3、定制 Endpoint )

    3 定制 Endpoint 3 1 定制 Health 信息 import org springframework boot actuate health Health import org springframework boot act