设计类专业网站,建设银行网站注销,谷歌网站建设代理,开发流程图参考: https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn
作用: 对线上应用进行观测、监控、预警… 比如: ● 健康状况【组件状态、存活状态】Health ● 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率…】Metrics ● 链路追踪
1.使用
1.场景引入
…参考: https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn
作用: 对线上应用进行观测、监控、预警… 比如: ● 健康状况【组件状态、存活状态】Health ● 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率…】Metrics ● 链路追踪
1.使用
1.场景引入
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependency2.暴露指标 在application.properties里面进行设置
#暴露所有端点信息
management.endpoints.web.exposure.include*
#以web方式暴露
management.endpoint.health.enabledtrue3.访问数据 ● 访问 http://localhost:8080/actuator展示出所有可以用的监控端点 ● http://localhost:8080/actuator/beans ● http://localhost:8080/actuator/configprops ● http://localhost:8080/actuator/metrics ● http://localhost:8080/actuator/metrics/jvm.gc.pause ● http://localhost:8080/actuator/endpointName/detailPath
2.Endpoint
常用端点: 默认提供了各种端点来展示应用的详细信息 包括看有多少线程及线程运行情况生成堆dump等各种端点 参考: https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn?inneru8Ebe
3.如何自定义端点
自定义HealthEndpoint和MetricsEndpoint
3.1 HealthEndpoint
1.自定义健康端点 例如监控一个组件的状态存活还是死亡 方法: 实现HealthIndicator接口或者继承AbstractHealthIndicator来实现 1)实现HealthIndicator接口–类名必须以HealthIndicator结尾并且实现HealthIndicator接口spring boot就能够确定这个组件是用来监控的
一句话总结下面的代码: 实现HealthIndicator接口的health方法返回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 {Overridepublic Health health() {int errorCode check(); // perform some specific health checkif (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();需要下面的配置来开启健康管理以及展示详细信息: application.properties里面进行配置:
management.endpoint.health.show-detailsalways2)继承AbstractHealthIndicator来实现
例子: 自定义一个健康监控的端点来监控某个组件的健康状态 一句话总结下面的代码: 继承AbstractHealthIndicator实现里面的doHealthCheck方法通过builder创建好Health对象
/*** author lfy* Description* create 2023-05-08 22:59** 1、实现 HealthIndicator 接口来定制组件的健康状态对象Health 返回* 2、*/
Component
public class MyHahaHealthIndicator extends AbstractHealthIndicator {AutowiredMyHahaComponent myHahaComponent; // 要检查的组件会从容器中找自动注入进来/*** 健康检查* param builder* throws Exception*/Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {//调用组件自定义的检查方法来检查应用的状态int check myHahaComponent.check();if(check 1){//存活builder.up().withDetail(code,1000).withDetail(msg,活的很健康).withDetail(data,我的名字叫haha).build();}else {//下线builder.down().withDetail(code,1001).withDetail(msg,死的很健康).withDetail(data,我的名字叫haha完蛋).build();}}
}同样需要下面的配置来开启健康管理以及展示详细信息: application.properties里面进行配置:
management.endpoint.health.show-detailsalways3.2 MetricsEndpoint
例子:自定义一个metric来监控某个方法被调用了多少次 总结:自定义metric是通过注入的MeterRegistry实现实现一个有参构造方法参数是MeterRegistry然后通过MeterRegistry里面提供的方法来实现比如计数器counter
Component
public class MyHahaComponent {Counter counter null;/*** 注入 meterRegistry 来保存和统计所有指标* param meterRegistry*/public MyHahaComponent(MeterRegistry meterRegistry){ // 只有一个有参构造方法参数自动从容器中找然后注入meterRegistry//得到一个名叫 myhaha.hello 的计数器//这个myhaha.hello会被展示在metrics中通过这个指标就能得到hello方法被访问了多少次counter meterRegistry.counter(myhaha.hello);}public int check(){//业务代码判断这个组件是否该是存活状态return 1;}// hello被调用一次就记一次数public void hello(){System.out.println(hello);// 调用一次就记一次数counter.increment();}
}4.还可以整合Prometheus Grafana来展示这些端点提供的数据
大概得步骤是: 引入Prometheus 和 Grafana的场景分别做一些配置设置Prometheus数据源配置Grafana监控面板 https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn