SpringBoot--->>>指标监控-->>定制 Endpoint 信息
定制 Endpoint 信息
之前的 endpoint 反馈信息很多,但是实际上程序肯定有某些流程会反馈数据出来以表示程序正常运行。所以需要自己定义 Endpoint 反馈的信息
1、定制 health Endpoint 信息
根据官网说法定制 health 信息,可以通过实现 HealthIndicator 接口,提供 health() 方法,里面调用 withDetail() 方法,设置详细信息。但是呢他是个接口,就进去看看类结构图,有没有实现类。发现有一个实现类 AbstractHealthIndicator ,它只要实现 doHealthCheck() 方法,判断逻辑并设置 health 信息
继承 AbstractHealthIndicator 类
这个类要是组件,它的 endpointName 就是 HealthIndicator 前面那一段首字母小写,所以要注意命名
@Component
public class MyProjectHealthIndicator extends AbstractHealthIndicator {/*** 健康检查方法,里面对逻辑进行检查,并设置 health 信息* @param builder* @throws Exception*/@Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {Map<Object,Object> mes = new HashMap<>();// 进行判断逻辑if (1==1){//直接反馈 health 状态为 UP
// builder.up();//第二种方式builder.status(Status.UP);mes.put("code","666");mes.put("mess","逻辑通过");}else {//设置 health 状态为 DOWN
// builder.down();//第二种方式builder.status(Status.OUT_OF_SERVICE);mes.put("code","000");mes.put("mess","逻辑不通过");}//结果builder.withDetail("num",789).withDetail("详细信息",mes);}
}
结果
-0-
实现 HealthIndicator 接口
@Component
public class MyHealthIndicator implements HealthIndicator {@Overridepublic Health health() {int errorCode = check();if (errorCode != 0) {return Health.down().withDetail("Error Code", errorCode).build();}return Health.up().build();}private int check() {// 这里写逻辑,结果返回判断return 0;}
}
2、定制 info Endpoint 信息
info 端点的作用是显示程序信息,有两种方式定制 info 信息
1、配置文件
info:env:enabled: true
# 自定义 info 端点的先决条件是 启用 management.info.env.enabled
info:app:name: 菠萝风车version: 1.0.0.0encoding: UTF-8java:resurce: 8
结果
-0-
或者,直接去 pom 文件中拿当前程序的名称、版本
使用 @@ 在中间写想要拿到的内容,根据pom文件的标签层级
info:appName: @project.artifactId@appVersion: @project.version@
-0-
2、实现 InfoContributor 接口
官网:
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.info
实现接口提供自定义应用程序信息
info 的命名跟类名没啥关系
@Component
public class MyProjectInfoContributor implements InfoContributor {@Overridepublic void contribute(Info.Builder builder) {builder.withDetail("appName1","PAPA").withDetail("appVersion1","1,2,2").withDetails(Collections.singletonMap("haha",123));}
}
3、定制 metrics 信息
官网:可以看到 Spring 支持自动适配的 metrics 类型
https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics
自定义 metrics 信息
操作 MeterRegistry 对象可以添加自定义 metrics
@Component
public class MyService {Counter counter;public MyService(MeterRegistry meterRegistry) {counter = meterRegistry.counter("这是自定义的metric");counter.increment();}}
或者使用 MeterBinder 注册 metrics 信息,以配置类方式,添加组件。默认情况下,来自所有MeterBinder
bean 的指标会自动绑定到 Spring-managed MeterRegistry
。
@Configuration
public class MetricsConfiguration {@Beanpublic MeterBinder heihei(Queue queue){return (registry) -> Gauge.builder("this is newMetrics", queue::size).register(registry);}}
4、定制 Endpoint
可以使用注解 @Endpoint 声明类成为一个自定义端点,用注解 @ReadOperation、@WriteOperation 修饰方法。自定义 Endpoint 会自动通过 JMX 公开,并且在 Web 应用程序中,也会通过 HTTP 公开。
类里面
@Component
@Endpoint(id = "myEndpoint")
public class MyProjectEndpoint {@ReadOperationpublic Map getJDBCConnect(){return Collections.singletonMap("read...","success");}@WriteOperationpublic void exceptionHandle(){System.out.println("出现了问题");}}
结果
访问结果
在 jconsole 中对这两个方法执行,会得到不同的反应。操作 exceptionHandle 就会输出内容。
SpringBoot--->>>指标监控-->>定制 Endpoint 信息相关推荐
- SpringBoot指标监控
SpringBoot Actuator可以帮助程序员监控和管理SpringBoot,比如健康检查.内存使用情况.线程使用情况统计.添加Actuator依赖,即可使用Actuator监控项目 1. #开 ...
- SpringBoot 指标监控
1. 简介 未来每一个微服务在云上部署以后,我们都需要对其进行监控.追踪.审计.控制等.SpringBoot就抽取了Actuator场景,使得我们每个微服务快速引用即可获得生产级别的应用监控.审计等功 ...
- 八. springboot 的指标监控 (3、定制 Endpoint )
3.定制 Endpoint 3.1.定制 Health 信息 import org.springframework.boot.actuate.health.Health; import org.spr ...
- SpringBoot - 构建监控体系02_定义度量指标和 Actuator 端点
文章目录 Pre Actuator 中的度量指标 Micrometer 度量库 Meter接口 计量器类型 如何创建这些计量器 扩展 Metrics 端点 自定义 Metrics 指标 使用 Mete ...
- springboot监控服务器信息,面试官:聊一聊SpringBoot服务监控机制
目录 前言 任何一个服务如果没有监控,那就是两眼一抹黑,无法知道当前服务的运行情况,也就无法对可能出现的异常状况进行很好的处理,所以对任意一个服务来说,监控都是必不可少的. 就目前而言,大部分微服务应 ...
- SpringBoot——四大核心之指标监控(actuator)
1.写在前面 首先肯定要说一下SpringBoot的四大核心了: 自动装配:简单配置甚至零配置即可运行项目 起步依赖:场景启动器 Actuator:指标监控 命令行界面 :命令行 这篇文章呢,我来和大 ...
- 002 第一季SpringBoot2核心技术-核心功能:配置文件、Web开发(原生组件)、数据访问、单元测试、指标监控、原理解析:@Value、命令行参数、手动获取bean、自定义starter
三.核心技术之- ->核心功能 1. 配置文件 1.1 文件类型 1.1.1 properties 同以前的properties用法 优先级高于yml的方式. 1.1.2 yaml 1) 简介 ...
- SpringBoot Actuator监控【转】
springboot actuator 监控 springboot1.5和springboot2.0 的actuator在启动日志上的差异就很大了. springboot1.5在启动时会打印很多/XX ...
- SpringBoot和监控管理
Spring Boot 监控管理: 简介 引入spring-boot-starter-actuator,我们可以使用Spring Boot为我们提供的准生产环境下的应用监控和管理功能.我们可以通过HT ...
最新文章
- Study on Android【三】--Intent消息传递
- 新建表维护程序SM30
- HTTPS 建立连接的过程
- yaml 文件格式简介
- oracle 存储过程
- Altiris 7.1 安装
- 文本分类入门(二)文本分类的方法
- 技术分享|手机推送原理剖析指南
- 7-51 两个有序链表序列的合并 (20 分)(vector做法)
- Bootstrap浅色淡雅个人博客
- nyoj 471:好多的树(容斥原理)
- sql server系统数据库,temp库的用途
- EXCEL2007导入SQL生成新表并插入数据
- Commitizen 互联网公民的简单提交惯例
- 让div填满剩下的空间
- 2020-05-21
- 详解Yarn中三种资源调度器(FIFO Scheduler、Capacity Scheduler、Fair Scheduler)和配置自定义队列实现任务提交不同队列
- MySQL 单个数据库备份还原
- OLTP和OLAP的区别;
- 创建一个简单的workflow工作流(WF4)