使用Prometheus+Grafana实现监控

news/2024/5/19 1:19:46 标签: prometheus, spring boot

使用Prometheus+Grafana实现监控

我们用 actuator 暴露应用本身的线程、bean 等信息,但是这些信息还是独立于 Prometheus 之外的。下面我们

将介绍如何将 SpringBoot Actuator 与 Prometheus 结合起来。

我们同样从 Spring Initializr 创建一个名为 spring-web-prometheus-demo 的项目,选取的依赖包括:

  • Spring Web
  • Spring Boot Actuator
  • Prometheus

这里增加了一个 Prometheus 包。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-web-prometheus-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-web-prometheus-demo</name>
    <description>使用 Prometheus + Grafana 实现监控</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.5</version>
        </dependency>

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

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

项目打开后,在 application.properties 中加入如下配置,打开相关的端口。

management.endpoint.metrics.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

启动类:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringWebPrometheusDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringWebPrometheusDemoApplication.class, args);
	}

}

接着启动项目,访问 localhost:8080/actuator/prometheus 可以看到 SpringBoot 的应用信息都以

Prometheus 的标准形式输出了。

在这里插入图片描述

下面我们使用 Grafana 官网 - Dashboards 模块 中的「JVM(Micrometer)」图表模板来展示应用的各项指标。

点击JVM (Micrometer) dashboard for Grafana | Grafana Labs 可以获取到 dashboard 的 ID 为:4701。

在这里插入图片描述

接着我们在 Grafana 页面点击「Import」菜单进入导入设置页面。

我们进入「Import」页面,填入模板的 ID,并点击「Load」按钮。

系统会自动读取模板 ID 对应的信息并显示在页面上。你需要选择模板的数据源,这里我选择了「Prometheus」

数据源,也就是本文应用所在的数据源。

在这里插入图片描述

在这里插入图片描述

设置完毕后点击「Import」按钮,则进入到看板页面。

在这里插入图片描述

我们还需要配置下 prometheus.yml 文件,让其去拉取 Node Exporter 的数据。

我们配置一下 Prometheus 的配置文件,让 Prometheus 服务器定时去业务数据源拉取数据。编辑

prometheus.yml并在 scrape_configs 节点下添加以下内容:

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  # 采集node exporter监控数据
  - job_name: 'node'
    static_configs:
      - targets: ['192.168.2.186:8080']
  # 采集JVM监控数据
  - job_name: 'jvm-node'
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ['192.168.2.185:8080']

新增一个任务,是名为 jvm-node 的任务,其从「192.168.2.185:8080」地址读取数据。

配置完成后,我们重新启动 Prometheus。

$ ./prometheus --config.file=prometheus.yml

在这里插入图片描述

最后的效果:
在这里插入图片描述

从看板我们可以看到许多信息,例如:应用启动持续时间、应用启动时间、堆的使用率、CPU 使用率等信息。

总结:

我们通过 Spring Boot Actuator 进行监控指标收集的,使用一个 Grafana 的模板将这些信息都展示在 Grafana 面

板上。看到这里,我们已经掌握了 Prometheus 监控的 80% 内容了。但是如果我们有一些业务指标需要监控,我

们应该如何实现呢?可以通过自定义监控指标。


http://www.niftyadmin.cn/n/1006094.html

相关文章

【大数据之路10】分布式消息队列系统 Kafka

分布式消息队列系统 Kafka 消息系统概述1. 架构体系1. 核心概念2. 高性能原理1. 磁盘顺序写2. 日志分段存储3. 冗余备份4. 二分查找5. 零拷贝3. 相关说明2. 生产案例需求分析1. 场景需求分析2. 规划资源分析3. 场景案例总结3. Kafka 部署模式4. Kafka 的使用1. 启停 Kafka2. to…

调教ChatGPT常用指令词【收藏版】

ChatGPT有哪些指令 当你发现 ChatGPT 回答的内容不如你的预期时&#xff0c;你可以通过一些指令来纠正和拓展他的答案&#xff0c;使得最终答案更趋向于你的理想答案。下面是一些觉见的指令和方法&#xff0c;对于的中英文都可以使用。 常用的指令 继续生成 继续&#xff0c;…

Spring Boot 中的 @RabbitListener 注解是什么,原理,如何使用

Spring Boot 中的 RabbitListener 注解是什么&#xff0c;原理&#xff0c;如何使用 在 RabbitMQ 中&#xff0c;消息的接收需要通过监听队列来实现。在 Spring Boot 应用程序中&#xff0c;可以使用 RabbitListener 注解来监听队列&#xff0c;并在接收到消息时执行指定的方法…

【k8s】如何批量删除处于Terminating状态的Pod

如果您想删除所有处于Terminating状态的Pod&#xff0c;可以按照以下步骤进行操作&#xff1a; 使用kubectl命令检查当前集群中所有处于Terminating状态的Pod&#xff1a; kubectl get pods --all-namespaces | grep Terminating创建一个包含所有处于Terminating状态Pod的文件…

python 在函数中嵌套定义函数 def 中写 def,python中函数嵌套(nested function)的使用及解析

在Python中&#xff0c;你可以在一个函数内部定义另一个函数&#xff0c;这被称为函数嵌套&#xff08;nested function&#xff09;。在一个函数中定义另一个函数可以让你在特定的上下文中创建和使用函数&#xff0c;以及在更高层级的函数中共享和封装代码块。 下面是一个示例…

Python神经网络学习(七)--强化学习--使用神经网络

前言 前面说到了强化学习&#xff0c;但是仅仅是使用了一个表格&#xff0c;这也是属于强化学习的范畴了&#xff0c;毕竟强化学习属于在试错中学习的。 但是现在有一些问题&#xff0c;如果这个表格非常大呢&#xff1f;悬崖徒步仅仅是一个长12宽4&#xff0c;每个位置4个动…

Spring框架的设计中用到了其中哪些设计模式

单例模式&#xff08;Singleton Pattern&#xff09;&#xff1a;Spring框架中的Bean默认使用单例模式&#xff0c;确保在应用程序中只存在一个实例&#xff0c;并通过IoC容器管理其生命周期。 工厂模式&#xff08;Factory Pattern&#xff09;&#xff1a;Spring框架通过IoC…

【kubernetes系列】Kubernetes之statefulset和daemonset控制器

本章节将分享kubernetes中的StatefulSet控制器和DaemonSet控制器&#xff0c;这是用于另外两种场景的pod控制器。 一、StatefulSet 1、StatefulSet介绍 在Kubernetes系统中&#xff0c;Pod的管理对象RC&#xff08;RS&#xff09;、Deployment、DaemonSet和Job都是面向无状态…