使用Prometheus+Grafana实现监控

news2024/10/7 0:08:50

使用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.coloradmin.cn/o/704317.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

从零开始 Spring Boot 54:@NotNull in Entity

从零开始 Spring Boot 54&#xff1a;NotNull in Entity 图源&#xff1a;简书 (jianshu.com) 之前通过两篇文章介绍了 Hibernate Validation 在 Spring 中的应用&#xff1a; 从零开始 Spring Boot 13&#xff1a;参数校验 - 红茶的个人站点 (icexmoon.cn)从零开始 Spring B…

[RocketMQ] Broker asyncSendMessage处理消息以及自动创建Topic (十)

asyncSendMessage方法用来处理来自producer发送的消息。 文章目录 1.asyncSendMessage异步处理单条消息2.preSend准备响应命令对象2.1 msgCheck检查并自动创建topic2.1.1 createTopicInSendMessageMethod创建普通topic2.1.2 createTopicInSendMessageBackMethod创建重试topc2.1…

浅入浅出Java锁

前提 做分布式爬虫时&#xff0c;结合已有的架构&#xff0c;直接对某网站的详情页进行了爬取&#xff1b;尴尬的是&#xff0c;某网站需先采集列表页&#xff0c;之后才能采集详情页&#xff1b;这种防爬手段使用了用户行为监控&#xff0c;行为异常的访问直接就给屏蔽了。 对…

c++11 日期和时间工具-(std::chrono)

链接 std::chrono是C11引入的日期时间处理库&#xff0c;其中包含3种时钟&#xff1a; system_clock&#xff0c;steady_clock&#xff0c;high_resolution_clock。 定义于头文件 <chrono> std::chrono 库 system_clock steady_clock 链接 链接2 每一次调用time_…

解决go install github.com/mattn/goreman@latest安装报错

go install github.com/mattn/goremanlatest报错&#xff1a; [rootlocalhost ~]# go install github.com/mattn/goremanlatest go: github.com/mattn/goremanlatest: module github.com/mattn/goreman: Get "https://proxy.golang.org/github.com/mattn/goreman/v/list&…

现在都在说 Docker 好,为什么我一用就出现这么多问题?查了一宿才解决!

#配置国内源进行docker安装 报错 HTTP Error 404 - Not Found 原因&#xff1a;由于配置国内镜像源时&#xff0c;把地址写错了&#xff0c;导致后面安装docker提示HTTP Error 404 解决方法&#xff1a; 1&#xff09;进入到 /etc/yum.repos.d目录下 cd /etc/yum.repos.d l…

8. 查询每日新用户数

文章目录 题目需求思路一实现一题目来源 题目需求 从用户登录明细表&#xff08;user_login_detail&#xff09;中查询每天的新增用户数&#xff0c;若一个用户在某天登录了&#xff0c;且在这一天之前没登录过&#xff0c;则任务该用户为这一天的新增用户。 期望结果如下&am…

Baumer工业相机堡盟工业相机如何通过BGAPISDK进行定序器编程:根据每次触发信号移动感兴趣区域(C#)

Baumer工业相机堡盟工业相机如何通过BGAPISDK进行定序器编程:根据每次触发信号移动感兴趣区域&#xff08;C#&#xff09; Baumer工业相机Baumer工业相机BGAPISDK和定序器编程的技术背景Baumer工业相机通过BGAPISDK进行定序器编程功能1.引用合适的类文件2.Baumer工业相机通过BG…

采集发布到WordPress自定义参数

Wordpress有自定义设置的参数&#xff0c;一般是用户自行设置&#xff0c;或主题和插件扩展新增的自定义参数&#xff0c;要怎么发布&#xff1f; WordPress主题或插件扩展新增的自定义参数&#xff0c;一般是保存到数据库的wp_postmeta表中。 先去数据库中确定对应自定义参数…

设计消息模块的持久层

一、创建MessageDao类 在 com.example.emos.wx.db.dao 包中创建 MessageDao.java 类 Repository public class MessageDao {Autowiredprivate MongoTemplate mongoTemplate;public String insert(MessageEntity entity){Date sendTimeentity.getSendTime();sendTimeDateUtil.…

【day4】类和对象

#include <iostream> using namespace std;class Complex {int real;int vir; public:Complex(){}Complex(int a,int b):real(a),vir(b){}void show(){cout << real << "" << vir << "i" << endl;}//成员函数版的运算…

Java基础-lambda表达式

简化匿名内部类的书写 下面两种写法均可&#xff1b; Arrays.sort(arr, new Comparator<Integer>() {Overridepublic int compare(Integer o1, Integer o2) {return o1 - o2;} }); Arrays.sort(arr, (Integer o1, Integer o2) -> {return o1 - o2;} );函数式编程思想&…

山西电力市场日前价格预测【2023-07-01】

日前价格预测 预测明日&#xff08;2023-07-01&#xff09;山西电力市场全天平均日前电价为364.57元/MWh。其中&#xff0c;最高日前价格为451.88元/MWh&#xff0c;预计出现在21: 30。最低日前电价为309.59元/MWh&#xff0c;预计出现在13: 30。以上预测仅供学习参考&#xff…

LabVIEW开发工业物联网状态监测

物理对象的网络&#xff0c;允许在它们之间传输数据。信息通常保存在集中式云数据库中。由于物联网&#xff0c;我们现在可以从远处进行监控和感知。由于网络和通信的增加&#xff0c;越来越多的流程可能会自动化。 调度、维护管理和质量改进等关键领域的决策正受到大数据技术…

python接口自动化(六)--发送get请求接口(详解)

简介 如果想用python做接口测试&#xff0c;我们首先有不得不了解和学习的模块。它就是第三方模块&#xff1a;Requests。 虽然Python内置的urllib模块&#xff0c;用于访问网络资源。但是&#xff0c;它用起来比较麻烦&#xff0c;而且&#xff0c;缺少很多实用的高级功能。更…

Git入门级指南

Git入门级指南 在软件开发和版本控制中&#xff0c;Git是一种非常流行且强大的工具。本文将为你提供关于Git的基本知识&#xff0c;并提 供一些实例来演示如何正确使用Git来管理代码。 关于git的简介 Git是一种分布式版本控制系统&#xff0c;它可以跟踪和管理项目中的代码…

前端安全问题及解决方案

随着互联网的高速发展&#xff0c;信息安全问题已经成为行业最为关注的焦点之一。总的来说安全是很复杂的一个领域&#xff0c;在移动互联网时代&#xff0c;前端人员除了传统的 XSS、CSRF 等安全问题之外&#xff0c;还时常遭遇网络劫持、非法调用 Hybrid API 等新型安全问题。…

(2023最新)互联网1010道Java面试真题汇总

我相信各位小伙伴们都发现了&#xff0c;现在的 IT 的环境并不如以前了&#xff0c;似乎是迎来“寒冬”&#xff0c;再加上最近上热搜的阿里云大裁员事件&#xff0c;又将 Java 开发岗推上了一个新的难度。而被裁员的人&#xff0c;不得不降薪重新找到一份工作&#xff0c;而经…

Django框架-5

路由系统 通过URL&#xff08;Uniform Resource Locator&#xff0c;统一资源定位符&#xff09;可以访问互联网上的资源——用户通过 浏览器向指定URL发起请求&#xff0c;Web服务器接收请求并返回用户请求的资源&#xff0c;因此可以将URL视为用户与服务器之间交互的桥梁。 …