码农知识堂 - 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

  • 相关阅读:
    使用numpy计算相关系数矩阵:np.corrcoef()
    Mac 上 VMvare 虚拟机 Centos 上的 Docker 容器中的文件夹共享到 Mac 实体机
    机器学习与密码学
    英特尔:如何从“小芯片”布局到通用量子计算机
    Ubuntu - 查看、开启、关闭和永久关闭防火墙
    千寻简Java词典音标版
    【Java面试】这道互联网高频面试题难住了80%的程序员?索引什么时候失效?
    批量归一化(部分理解)
    MySQL高级学习笔记
    向后量子密码学迁移!美国NIST公布12家合作伙伴
  • 原文地址:https://blog.csdn.net/qq_45433217/article/details/127449010
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号