当前位置: 首页 > news >正文

SpringBoot3 整合Prometheus + Grafana

通过Prometheus + Grafana对线上应用进行观测、监控、预警…

  • 健康状况【组件状态、存活状态】Health
  • 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率…】Metrics

1. SpringBoot Actuator

1. 基本使用

1. 场景引入

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

2. 暴露指标

management:endpoints:enabled-by-default: true #暴露所有端点信息web:exposure:include: '*'  #以web方式暴露

3. 访问数据

  • 访问 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

1. 常用端点

ID描述
auditevents暴露当前应用程序的审核事件信息。需要一个AuditEventRepository组件
beans显示应用程序中所有Spring Bean的完整列表
caches暴露可用的缓存
conditions显示自动配置的所有条件信息,包括匹配或不匹配的原因
configprops显示所有@ConfigurationProperties
env暴露Spring的属性ConfigurableEnvironment
flyway显示已应用的所有Flyway数据库迁移。需要一个或多个Flyway组件。
health显示应用程序运行状况信息
httptrace显示HTTP跟踪信息(默认情况下,最近100个HTTP请求-响应)。需要一个HttpTraceRepository组件
info显示应用程序信息
integrationgraph显示Spring integrationgraph 。需要依赖spring-integration-core
loggers显示和修改应用程序中日志的配置
liquibase显示已应用的所有Liquibase数据库迁移。需要一个或多个Liquibase组件
metrics显示当前应用程序的“指标”信息
mappings显示所有@RequestMapping路径列表
scheduledtasks显示应用程序中的计划任务
sessions允许从Spring Session支持的会话存储中检索和删除用户会话。需要使用Spring Session的基于Servlet的Web应用程序
shutdown使应用程序正常关闭。默认禁用
startup显示由ApplicationStartup收集的启动步骤数据。需要使用SpringApplication进行配置BufferingApplicationStartup
threaddump执行线程转储
heapdump返回hprof堆转储文件
jolokia通过HTTP暴露JMX bean(需要引入Jolokia,不适用于WebFlux)。需要引入依赖jolokia-core
logfile返回日志文件的内容(如果已设置logging.file.namelogging.file.path属性)。支持使用HTTP Range标头来检索部分日志文件的内容
prometheus以Prometheus服务器可以抓取的格式公开指标。需要依赖micrometer-registry-prometheus

2. 定制端点

  • 健康监控:返回存活、死亡
  • 指标监控:次数、率
1. HealthEndpoint
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();
management:health:enabled: trueshow-details: always #总是显示详细信息。可显示每个模块的状态信息
@Component
public class MyComHealthIndicator extends AbstractHealthIndicator {/*** 真实的检查方法* @param builder* @throws Exception*/@Overrideprotected 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);}
}
2. MetricsEndpoint
class MyService{Counter counter;//默认一个构造时,参数会从ioc中拿public MyService(MeterRegistry meterRegistry){counter = meterRegistry.counter("myservice.method.running.counter");}public void hello() {counter.increment();}
}

2. 监控落地

基于 Prometheus + Grafana

1. 安装 Prometheus + Grafana

安装 Prometheus + Grafana

2. 导入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.10.6</version>
</dependency>
management:endpoints:web:exposure: #暴露所有监控的端点include: '*'

访问: http://localhost:8001/actuator/prometheus 验证,返回 prometheus 格式的所有指标

在这里插入图片描述

部署Java应用到服务器

确保可以访问到部署好的服务,http://192.168.254.129:8080/actuator/prometheus

在这里插入图片描述

http://192.168.254.129:8080/actuator

在这里插入图片描述

3. 配置 Prometheus 拉取数据

## 修改 prometheus.yml 配置文件
scrape_configs:- job_name: 'spring-boot-actuator-exporter'metrics_path: '/actuator/prometheus' #指定抓取的路径static_configs:- targets: ['192.168.254.129:8080']labels:nodename: 'app-demo'

配置完记得重启容器

在这里插入图片描述

4. 配置 Grafana 监控面板

  • 添加数据源(Prometheus)

在这里插入图片描述

  • 添加面板。可去 grafana dashboard 市场找一个自己喜欢的面板,也可以自己开发面板
    • 市场直接搜索springboot,注意看面板支持的数据源,复制面板ID

在这里插入图片描述

填入面板id,选择刚刚创建好的数据源

在这里插入图片描述

5. 效果

等待应用运行一会后,就会显示出对应的监控数据

在这里插入图片描述

http://www.lryc.cn/news/114403.html

相关文章:

  • Python实现GA遗传算法优化LightGBM回归模型(LGBMRegressor算法)项目实战
  • 【基于IDEA + Spark 3.4.1 + sbt 1.9.3 + Spark MLlib 构建逻辑回归鸢尾花分类预测模型】
  • 资深测试老鸟整理,性能测试-常见调优详细,卷起来...
  • 【第五章 flutter学习之flutter进阶组件-上篇】
  • 鸿蒙边缘计算网关正式开售
  • Bytebase 2.5.0 - VCS 集成支持 Azure DevOps,支持达梦数据库
  • tomcat通过systemctl启动时报错Cannot find /usr/local/tomcat/bin/setclasspath.sh
  • Django架构图
  • vue- 创建wms-web项目
  • 集成学习:机器学习模型如何“博采众长”
  • 排序算法(二)
  • CVPR 2023 | 无监督深度概率方法在部分点云配准中的应用
  • HTTP隧道识别与防御:机器学习的解决方案
  • 【MMU】认识 MMU 及内存映射的流程
  • Clion开发Stm32之存储模块(W25Q64)驱动编写
  • SpringBoot动态切换数据源
  • [C++项目] Boost文档 站内搜索引擎(4): 搜索的相关接口的实现、线程安全的单例index接口、cppjieba分词库的使用、综合调试...
  • SAP ABAP元素域值描述通过函数(DD_DOMVALUE_TEXT_GET)获取
  • 原型模式与享元模式:提升系统性能的利器
  • uniapp封装手写签名
  • 掌握 JVM 调优命令
  • 扩增子分析流程——Lotus2: 一行命令完成所有分析
  • 微服务 云原生:搭建 Harbor 私有镜像仓库
  • Ceph入门到精通-远程开发Windows下使用SSH密钥实现免密登陆Linux服务器
  • APP外包开发的开发语言对比
  • 基于Python++PyQt5马尔科夫模型的智能AI即兴作曲—深度学习算法应用(含全部工程源码+测试数据)
  • Android中简单封装Livedata工具类
  • 国内大模型在局部能力上已超ChatGPT
  • 监控设置ip地址怎么设置
  • 力扣:56. 合并区间(Python3)