码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Windows和Linux上使用Prometheus+Grafana监控Springboot


    Windows和Linux上使用Prometheus+Grafana监控Springboot

    • 一、Springboot代码修改
      • 1.引入maven依赖
      • 2. 启动类新增代码
      • 3. application.properties文档配置
    • 二、Windows中Prometheus和Grafana安装及配置
      • 1.下载Prometheus压缩包
      • 2. 运行Prometheus及后台程序
      • 3. 下载Grafana压缩包并启动
      • 4. Grafana页面配置
      • 5.下载并导入模板
    • 三、Linux中Prometheus和Grafana安装及配置
      • 1. 下载prometheus的Linux压缩包并解压
      • 2. prometheus.yml新增Job
      • 3. 在安装目录下启动prometheus
      • 4. 下载Grafana压缩包

    一、Springboot代码修改

    1.引入maven依赖

       <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.1.3</version>
        </dependency> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2. 启动类新增代码

        @Bean
        MeterRegistryCustomizer<MeterRegistry> configurer(
                @Value("${spring.application.name}") String applicationName) {
            return (registry) -> registry.config().commonTags("application", applicationName);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3. application.properties文档配置

    	spring.application.name=community
    	# 打开所有 Actuator 服务
    	management.endpoints.web.exposure.include=*
    	# 将应用名称添加到计量器的 tag 中去,以便 Prometheus 根据应用名区分不同服务
    	management.metrics.tags.application=${spring.application.name}
    	# 暴露Tomcat
    	server.tomcat.mbeanregistry.enabled=true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二、Windows中Prometheus和Grafana安装及配置

    1.下载Prometheus压缩包

    下载:Prometheus压缩包 解压并在安装目录下的prometheus.yml的最下方新增Job(要注意格式),代码如下:

    - job_name: "community"
      # 这里我们springboot暴露出来的endpoint
      metrics_path: 'actuator/prometheus' 
      # 信息收集时间是间隔5秒    
      scrape_interval: 5s  
      static_configs:
        - targets: ["localhost:后台端口号"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. 运行Prometheus及后台程序

    运行Prometheus及后台程序,访问【localhost:9090】,点击【Status-Targets】可以查看监控的端口列表
    在这里插入图片描述
    在这里插入图片描述

    3. 下载Grafana压缩包并启动

    下载:Grafana压缩包 解压后把conf/sample.ini复制一份然后重命名为conf/custom.ini,点击bin目录下的grafana-server.exe运行

    4. Grafana页面配置

    访问localhost:3000,添加Prometheus数据源

    在这里插入图片描述

    5.下载并导入模板

    SpringBoot模板: https://grafana.com/grafana/dashboards/10280
    JVM模板: https://grafana.com/grafana/dashboards/12856
    在这里插入图片描述

    三、Linux中Prometheus和Grafana安装及配置

    注意:若是阿里云服务器需要在阿里云管理后台新增两个端口的访问规则(9090、3000)

    1. 下载prometheus的Linux压缩包并解压

    下载:Prometheus压缩包 并解压

    tar -zxvf prometheus-2.39.1.linux-amd64.tar.gz -C /usr/local/
    
    • 1

    2. prometheus.yml新增Job

    在安装目录下的prometheus.yml的最下方新增Job(要注意格式),代码如下:

    - job_name: "community"
      scheme: https
      tls_config:
        insecure_skip_verify: true
      # 这里我们springboot暴露出来的endpoint
      metrics_path: 'actuator/prometheus'
      # 信息收集时间是间隔5秒    
      scrape_interval: 5s
      static_configs:
        - targets: ["服务器IP:后台端口"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 在安装目录下启动prometheus

    	# 非后台启动(修改好配置文件后先运行此命令并访问【http://服务器IP:9090】,确定能访问后再用以下语句启动)
    	sudo ./prometheus 
    	# 后台启动
    	nohup ./prometheus --config.file=prometheus.yml --web.enable-admin-api --web.listen-address=:9090 >/dev/null 2>&1 &
    	# 查看占用端口进程的PID
    	netstat -tunlp|grep 9090
    	# 杀死进程
    	kill -9 {PID}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4. 下载Grafana压缩包

    a. 下载:https://grafana.com/grafana/download 中的grafana-enterprise-2.1-1.x86_64.rpm并运行以下命令

       # 运行安装命令 
       sudo rpm -i --nodeps grafana-enterprise-9.2.1-1.x86_64.rpm
       # 启动Grafana
       sudo /bin/systemctl start grafana-server.service
    
    • 1
    • 2
    • 3
    • 4

    b. 访问【http://服务器IP:3000】,添加Prometheus数据源

    c. 下载并导入模板:
    SpringBoot模板: https://grafana.com/grafana/dashboards/10280
    JVM模板: https://grafana.com/grafana/dashboards/12856

  • 相关阅读:
    (附源码)springboot篮球场地预约系统 毕业设计 345655
    Latex论文排版
    结构型模式-装饰器模式
    【PAT乙级】一百一十道真题刷后大汇总——C/C++
    五、Spring Boot(1)
    m基于OFDM的OMP压缩感知信道估计算法误码率仿真,对比传统的LS,MMSE以及LMMSE信道估计性能
    世界各国当日数据探索性分析
    FineReport 基础报表样式
    如何创建python虚拟环境
    week9|查阅文章 Mac os M2芯片 arm64架构|安装paddlepaddle问题
  • 原文地址:https://blog.csdn.net/qq_45433217/article/details/127449010
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号