基于Prometheus 和 Grafana 实现springboot应用监控和服务器监控

news2024/9/22 1:19:43

目录

1.Prometheus环境搭建

1.1 下载和安装Prometheus

1.2 配置Prometheus

1.3 配置Prometheus抓取Spring Boot应用Metrics

1.4 启动Prometheus

2.Grafana环境搭建

2.1 下载和安装Grafana

2.2 配置Grafana数据源

2.3 创建Dashboard

3、NodeExporter(服务器监控)插件配置安装

3.1 下载和安装Node Exporter 插件

3.2配置prometheus.yml

3.3配置Grafana

3.4遇到的坑及解决方法

4、Spring boot应用的监控配置

4.1 springboot工程pom.xml 配置

4.2 application.properties或application.yml 配置

4.3 定义Prometheus监控任务

4.4 配置Grafana的dashboard


1.Prometheus环境搭建

1.1 下载和安装Prometheus

访问Prometheus官方网站(https://prometheus.io/download/),下载适合操作系统的Prometheus版本。解压下载的文件,将包含Prometheus二进制文件和配置文件的目录放在合适的位置。

1.2 配置Prometheus

在Prometheus的安装目录中,找到prometheus.yml配置文件。打开并编辑该文件,以配置Prometheus以抓取Spring Boot应用程序的指标数据。

1.3 配置Prometheus抓取Spring Boot应用Metrics

在prometheus.yml文件中,找到scrape_configs部分,并添加一个新的抓取配置,如下所示:

scrape_configs:​​​​​​​
  - job_name: 'spring_boot_app'​​​​​​​
    metrics_path: '/actuator/prometheus'​​​​​​​
    static_configs:​​​​​​​
      - targets: ['localhost:8080'] # 使用你的Spring Boot应用程序的实际主机和端口替换

在此配置中,我们指定了应用程序的/actuator/prometheus端点作为Prometheus抓取指标数据的路径。确保将targets中的localhost:8080替换为你的Spring Boot应用程序的实际主机名和端口号。

以下是我机器的实际配置:

# my global config

global:

  scrape_interval: 5s # Set the scrape interval to every 15 seconds. Default is every 1 minute.

  evaluation_interval: 5s # Evaluate rules every 15 seconds. The default is every 1 minute.

  # scrape_timeout is set to the global default (10s).



# Alertmanager configuration

alerting:

  alertmanagers:

    - static_configs:

        - targets:

          # - alertmanager:9093



# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.

rule_files:

  # - "first_rules.yml"

  # - "second_rules.yml"



# 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"]

      

scrape_configs:



  - job_name: 'springboot-69-8084'

    metrics_path: '/actuator/prometheus'

    static_configs:

      - targets: ['localhost:8084'] # 使用你的Spring Boot应用程序的实际主机和端口替换

      

  - job_name: 'springboot-69-8082'

    metrics_path: '/actuator/prometheus'

    static_configs:

      - targets: ['localhost:8082'] # 使用你的Spring Boot应用程序的实际主机和端口替换

      

  - job_name: 'springboot-69-8761'

    metrics_path: '/actuator/prometheus'

    static_configs:

      - targets: ['localhost:8761'] # 使用你的Spring Boot应用程序的实际主机和端口替换

      

  - job_name: "nacos_162_8848"

    metrics_path: '/actuator/prometheus'

    static_configs:

      - targets: ['192.168.3.162:8848']

      

  - job_name: "prometheus_162"

    static_configs:

      - targets: ["192.168.3.162:9090"]

      

  - job_name: "prometheus_69"

    static_configs:

      - targets: ["192.168.3.69:9090"]

      

  - job_name: "linux_exporter_162"

    metrics_path: '/metrics'

    static_configs:

      - targets: ["192.168.3.162:9100"]    





  - job_name: order-jvm  #监控Job名称

    static_configs:

      - targets: ['localhost:8314']  #待监控机器列表,node exporter默认端口9100,多台机器用“,”分隔。

1.4 启动Prometheus

在Prometheus的安装目录中,使用命令行运行以下命令以启动Prometheus:

  • 对于Linux/macOS:
./prometheus --config.file=prometheus.yml
  • 对于Windows:
prometheus.exe --config.file=prometheus.yml

现在,Prometheus已经开始运行并监控你的Spring Boot应用程序的指标数据。接下来,需要安装和配置Grafana,以实现指标数据的可视化。

启动成功日志如下

浏览器访问: http://localhost:9090/targets?search=

2.Grafana环境搭建

2.1 下载和安装Grafana

访问Grafana官方网站(https://grafana.com/grafana/download/),下载适合你操作系统的Grafana版本。安装完成后,按照Grafana官方文档中的说明启动Grafana服务。

Windows版本,直接安装运行即可

grafana-enterprise-10.0.1.windows-amd64.msi

Linux版本:

sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.0.1-1.x86_64.rpm

修改配置文件 /etc/grafana/grafana.ini 中端口为3001,并重启

[root@grafana ~]# systemctl daemon-reload

[root@grafana ~]# systemctl start grafana-server

[root@grafana ~]# systemctl enable grafana-server

[root@grafana ~]# systemctl status grafana-server

浏览器访问:

http://192.168.3.162:3001/    admin / admin

2.2 配置Grafana数据源

登录Grafana(默认用户名和密码均为admin),然后转到Configuration > Data Sources。点击Add data source,选择Prometheus作为数据源类型。

 在URL字段中,输入Prometheus实例的地址(例如http://localhost:9090),然后将Access设置为Server。点击Save & Test按钮以测试和保存数据源配置。

2.3 创建Dashboard

完成数据源配置后,转到Dashboards > Manage,点击New dashboard按钮创建一个新的Dashboard。

在新的Dashboard中,点击Add new panel以添加图表。在图表编辑器中,你可以选择查询Prometheus数据源的指标数据。例如,要查看Spring Boot应用程序的HTTP请求数,可以输入以下PromQL查询:

rate(http_server_requests_seconds_count[1m])

3、NodeExporter(服务器监控)插件配置安装

3.1 下载和安装Node Exporter 插件

Node Exporter 安装及启动运行

https://github.com/prometheus/node_exporter/releases

wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz

tar zxvf node_exporter-1.6.0.linux-amd64.tar.gz

cd /usr/local/node_exporter-1.3.1.linux-amd64

nohup ./node_exporter  --web.listen-address=":9100" &

可见,插件已经抓取到服务器的监控信息。

3.2配置prometheus.yml

将192.168.3.162:9100的ip和端口配置到一个新建job

配置完后,重启普罗米修斯:

可以看到普罗米修斯已经监控到9100的job,点击确认查看监控详情:

3.3配置Grafana

这里我们使用官方的模板配置 9276 :

grafana模板下载地址

https://grafana.com/grafana/dashboards

数据源选Prometheus:

点确定后,进入监控面板:

3.4遇到的坑及解决方法

Grafana中监控不到数据

1.Centos上安装ntpdate:命令  yum install ntpdate -y

2.输入命令:ntpdate ntp1.aliyun.com

3.输入命令:hwclock --sysohc

4.输入命令:timedatectl

5.重新查看一下日期,输入命令:date

6.删除原来的Prometheus,重新安装配置一下

7.输入命令:reboot,重启centos

8.先检查一下日期:输入命令date,保证时间与windows监控机一致

9.关闭防火墙,重新启动grafana,prometheus,node_exporter

10.windows登录grafana,进入监控面板,已经可以监控到数据了,问题完美解决

4、Spring boot应用的监控配置

4.1 springboot工程pom.xml 配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

4.2 application.properties或application.yml 配置

management.endpoints.web.exposure.include=*

management:

  endpoints:

    web:

      exposure:

        include: *

如下图:

4.3 定义Prometheus监控任务

 - job_name: 'springboot-69-8084'

    metrics_path: '/actuator/prometheus'

    static_configs:

      - targets: ['localhost:8084'] # 使用你的Spring Boot应用程序的实际主机和端口替换

这里注意 metrics_path 要修改为  '/actuator/prometheus'

4.4 配置Grafana的dashboard

这里我们还是使用官方的模板:

SpringBoot Dashboard 模板ID:  12900

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/728891.html

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

相关文章

华为云Astro出品《低代码开发者101问》电子书上线

这四年来&#xff0c;在低代码平台的运营和客户支持过程中&#xff0c;我们有幸见证了华为云低代码的成长&#xff0c;也有幸认识了很多有思想、有热情、有行动力的低代码开发者朋友&#xff0c;我们自身对低代码领域的认识和理解也在不断整合、刷新。与开发者、客户、伙伴以及…

Vue3使用混入(混合)

minixs/entity.js&#xff1a;混入代码 //混入的实体类 import { reactive } from vue;const userEntityMixin {setup() {const Admin reactive({id: -1,account: "",userPassword: "",});const Teacher reactive({id: -1,account: "",userP…

Nginx启动、关闭及信息查看命令

1、查找Nginx的位置 ps -aux | grep nginx 2、启动Nginx nginx直接启动&#xff0c;这时候可能出现两种情况&#xff1a; 第一种&#xff0c;就是Nginx已经启动过了&#xff0c;会提示端口被占用&#xff0c;启动失败 我们尝试杀掉占用端口的进程&#xff0c;然后重启 sys…

基于springboot图书个性化推荐系统的设计与实现【附ppt和万字文档(Lun文)和搭建文档】

主要功能 前台登录&#xff1a; ①首页&#xff1a;图书名称查询、图书信息推荐、好书推荐、图书信息展示 ②图书信息&#xff1a;图书类别、图书名称、名称类别作者查询、图书详情、收藏、点赞、评论 ③好书推荐&#xff1a;图书展示、点击查看 ④留言反馈&#xff1a;用户可…

爆肝整理,Docker容器测试-常见问题+解决(汇总)

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 问题1&#xff1a…

9 首页和图标定制

9.1 首页 springboot的首页是写在index.html中&#xff0c;而index.html可以放在静态资源存放的任何一个文件夹&#xff08;public、resources、static&#xff09;。然后&#xff0c;直接运行&#xff0c;访问localhost:8080即可。 9.2 图标定制 对于springboot2.7.13版本&am…

应用层:域名系统DNS

1.应用层&#xff1a;域名系统DNS&#xff08;Domain Name System&#xff09; 笔记来源&#xff1a; 湖科大教书匠&#xff1a;应用层概述 湖科大教书匠&#xff1a;域名系统DNS 声明&#xff1a;该学习笔记来自湖科大教书匠&#xff0c;笔记仅做学习参考 DNS报文使用运输层的…

Rust 动态数组Vec基本概念及其用法

目录 一、基本概念 Vec是什么&#xff1f; Vec的特点 (1)动态大小&#xff1a; (2)可变性&#xff1a; (3)泛型&#xff1a; 二、基础用法 1. 创建 (1) Vec::new()方法 (2) Vec::from()方法 (3) vec! 宏 2. 基础用法 三、Vec的简单实现及其宏模拟 四、leetcode 实…

vue开发图表echarts基本使用

官网传送门 1.echarts-介绍 是一个js插件 性能好可流畅远行PC和移动设备 兼容主流浏览器 提供很多图标&#xff0c;用户且可自行修改。 2.使用npm安装 npm install echarts3.echarts-基础配置 series -- 系列列表。每个系列通过 type 决定自己的图表类型 -- 大白话&#xff…

Web安全——数据库mysql学习

数据库mysql基础 Web安全分享一、数据库的基本操作1、MYSQL登录与退出2、MYSQL数据库的一些解释3、MYSQL注释符有三种&#xff1a; 二、数据库的一些基本操作1、数据库的增删改查(sql语句) 三、table 表的操作1、查看表结构2、查看表的内容3、建立表4、约束条件5、修改表的操作…

小红书发布图文视频笔记软件

小红书发布图文视频笔记软件,&#x1f198;这个工具&#xff0c;不愧是做小红书的神&#xff01;附教程#新媒体 #微信创作者助手 #小红书 微信时刻 软件有月卡、季卡、半年卡、年卡 【引流脚本软件开发定制&#xff0c;欢迎你】 服务时间&#xff1a;&#xff08;8&#xff1a…

公司新来了个测试,一副毛头小子的样儿,哪想到是新一代卷王。。。

内卷&#xff0c;是现在热度非常高的一个词汇&#xff0c;随着热度不断攀升&#xff0c;隐隐到了“万物皆可卷”的程度。 在程序员职场上&#xff0c;什么样的人最让人反感呢? 是技术不好的人吗?并不是。技术不好的同事&#xff0c;我们可以帮他。 是技术太强的人吗?也不是…

vue基础-某个项目ESLint不生效问题

文章目录 前言一、解决方法二、在配置文件中添加一下配置三、本人电脑的ESLint配置总结 前言 vue基础&#xff0c;最近接手同事的项目&#xff0c;发现ESLint不生效了&#xff0c;找了很久也没有发现问题&#xff0c;因为其他的项目是可以用的。 一、解决方法 1、在vscode中打…

KafKa 3.x(二、Broker,消费者)

4. Kafka Broker 4.1 kafka Broker工作流程 4.1.1 Zoopkeeper存储的Kafka信息 启动Zookeeper客户端通过ls命令查看kafka相关信息 在Zookeeper的服务端存储的Kafka相关信息 /kafka/brokers/ids [0,1,2] 记录那些服务器/kafka/brokers/topics/first/partitions/0/state {“l…

关于超卖程序问题分析-java

关于超卖程序问题分析 1.并发情况下&#xff0c;GET缓存 判断>0&#xff0c;成立&#xff0c;均执行扣减库存&#xff0c;导致超卖 2.加锁 以库存key&#xff0c;加锁&#xff0c;setNx&#xff0c;finally解锁 deleteKey 存在问题 1.误解锁&#xff08;是不是也是因为…

Android 11 SystemUI 启动流程

SystemUI 有哪内容 从表面上看&#xff0c; 我们看到的状态栏、通知栏、下拉菜单、导航栏、锁屏、最近任务、低电提示等系统页面都是 SystemUI 的。SystemUI&#xff0c;在源码目录中位于&#xff1a; framework/base/packages 目录下&#xff0c; 可见 SystemUI 和 framework…

vue3脚本绑定CodeMirror的使用

代码&#xff1a; <template><CodeMirrorref"codeMirror":value"codeVal":languageSingle"languageSingle":readOnly"!isEdit"submitCode"submitCode"></CodeMirror> </template><script setup…

文华财经期货APP随身行和同花顺期货可以模拟交易的期货软件,那个更好用?

期货app是一种可以在手机上进行期货交易和行情分析的软件&#xff0c;它可以让投资者随时随地掌握期货市场的动态&#xff0c;进行投资决策。随着科技的发展&#xff0c;越来越多的期货投资者选择通过手机端app来进行期货交易&#xff0c;享受随时随地、方便快捷的服务。市面上…

Js获取浏览器地址栏参数

获取浏览器地址参数 //获取浏览器地址栏参数function getQueryString(name) {var reg new RegExp("(^|&)" name "([^&]*)(&|$)");var result window.location.search.substr(1).match(reg);if (result ! null) {return unescape(result[2…

VMware 17虚拟Ubuntu 22.04设置共享目录

之前使用VM 17之前的版本虚拟CentOS&#xff0c;设置共享目录非常方便&#xff0c;在CentOS中安装VMware Tools即可。随着CentOS变成上游版本后&#xff0c;转向使用Ubuntu&#xff0c;VM也升级到了17&#xff0c;Ubuntu也升级到了最新的22.04&#xff0c;但是发现共享目录不能…