SpringBoot3 整合Prometheus + Grafana

这篇具有很好参考价值的文章主要介绍了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 {

    @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);

    }
}
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 格式的所有指标

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

部署Java应用到服务器

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

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

http://192.168.254.129:8080/actuator

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

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'

配置完记得重启容器

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

4. 配置 Grafana 监控面板

  • 添加数据源(Prometheus)

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

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

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

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

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发

5. 效果

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

SpringBoot3 整合Prometheus + Grafana,云原生,运维,SpringBoot,prometheus,grafana,运维开发文章来源地址https://www.toymoban.com/news/detail-629591.html

到了这里,关于SpringBoot3 整合Prometheus + Grafana的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot整合Mybatis-Plus(SpringBoot3)

    依赖pom.xml: pom.xml resource包下的Application.yml: Aollication.yml pojo包下的实体类User: User mapper包下的接口UserMapper: UserMapper 主启动类DemoPlusApplication DemoPlusApplication 测试类DemoApplicationTest: DemoApplicationTest 实现结果 检测数据库连接: C(Create): D(Delete): U(Update) R(Read)

    2024年03月20日
    浏览(45)
  • SpringBoot3 整合 ElasticSearch7 示例

    做仿牛客项目需要使用 es 做搜索,但是老师示例的是 SpringBoot2 + es6 去做的,然而我用的是 Spring3 + es7.17.10,于是踩了很多的坑。 在 es7 中,配置文件和查询所需的实现类都做了很大的改动,我以能成功运行的代码为例,大概说一下怎么配置和使用。 首先 yml 配置文件发生了变

    2024年02月07日
    浏览(46)
  • springboot3.2 整合 mybatis-plus

    springboot3.2 正式发布了 迫不及待地的感受了一下 结果在整个mybatis-plus 的时候遇到了如下报错 主要是由于 mybatis-plus 中 mybatis 的整合包版本不够导致的 排除 mybatis-plus 中自带的 mybatis 整合包,单独引入即可 修改依赖后正常

    2024年02月04日
    浏览(40)
  • SpringBoot3整合OpenAPI3(Swagger3)

    swagger2 更新到3后,再使用方法上发生了很大的变化,名称也变为 OpenAPI3 。 官方文档 openapi3 使用十分方便,做到这里后,你可以直接通过以下网址访问 swagger 页面。 1. @OpenAPIDefinition + @Info 用于定义整个 API 的信息,通常放在主应用类上。可以包括 API 的标题、描述、版本等信

    2024年01月22日
    浏览(45)
  • SpringBoot3整合SpringSecurity,实现自定义接口权限过滤

    接口权限过滤是指对于某些接口或功能,系统通过设定一定的权限规则,只允许经过身份认证且拥有相应权限的用户或应用程序进行访问和操作 。这种技术可以有效地保护系统资源和数据安全,防止未授权的用户或程序进行恶意操作或非法访问。通常情况下,接口权限过滤需

    2024年02月08日
    浏览(41)
  • 解决SpringBoot3整合Druid的兼容性问题

    本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 截止目前,Druid对于SpringBoot3的支持不够全面和友好;存在一些兼容性的问题,导致项目报错。 在此,针对该问题提供可行的解决方案;以供各位参考。 请您使用以下依赖: 图示如下: 请勿使用以下依赖: 请

    2024年02月03日
    浏览(35)
  • SpringBoot3.0 整合 ElasticSearch8.5.0 及使用

    这两个版本都是目前较新的版本,本文选用的依赖是 spring-boot-starter-data-elasticsearch:3.0 ,这个新版本也是改用了es的 elasticsearch-java API,全面推荐使用Lambda语法;另外SpringData本身推出了 Repository 功能(有些类似Mybatis-Plus)的功能,也支持注解简化开发。 Docker 快速部署 单机 ela

    2024年02月11日
    浏览(53)
  • Springboot3.0整合swagger,废弃Springfox改用Springdoc

    Automated JSON API documentation for API\\\'s built with Spring 官网地址:springfox.io springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. 官网地址:https://springdoc.org/v2/ 注意 :使用的是V2版本,这个版本支持springboot3.0 之前springboot3.0之前我用的都是Springfox来集

    2023年04月09日
    浏览(33)
  • SpringBoot3整合Druid数据源的解决方案

    druid-spring-boot-3-starter目前最新版本是1.2.20,虽然适配了SpringBoot3,但缺少自动装配的配置文件,会导致加载时报加载驱动异常。 需要手动在resources目录下创建 META-INF/spring/ 目录,并且在 META-INF/spring/ 创建 org.springframework.boot.autoconfigure.AutoConfiguration.imports , 文件中添加如下内容

    2024年03月09日
    浏览(89)
  • Springboot3整合Mybatis-plus3.5.3报错

    ✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉 🍎个人主页:Leo的博客 💞当前专栏: 报错以及Bug ✨特色专栏: MySQL学习 🥭本文内容:记录一次Docker与Redis冲突 🖥️个人小站 :个人博客,欢迎大家访问 📚个人知识库: 知识库,

    2024年02月05日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包