前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >十分钟就能上手Prometheus与Grafana监控SpringBoot项目

十分钟就能上手Prometheus与Grafana监控SpringBoot项目

作者头像
知识浅谈
发布于 2022-12-18 09:47:53
发布于 2022-12-18 09:47:53
73200
代码可运行
举报
文章被收录于专栏:分享学习分享学习
运行总次数:0
代码可运行

🍁 作者:知识浅谈,CSDN签约讲师,CSDN原力作者,后端领域优质创作者,热爱分享创作 💒 公众号:知识浅谈 📌 擅长领域:全栈工程师、爬虫、ACM算法

🤞这次都给他拿下🤞 十分钟快速上手Prometheus与Grafana监控SpringBoot项目 先来捋一下数据流的传输

正菜来了⛳⛳⛳

环境: springboot项目:127.0.0.1:8081 prometheus:127.0.0.1:9090 grafana:127.0.0.1:3000

🎈项目的创建

📐第 1 步:pom文件依赖

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<!--这个依赖用于健康检查,审计,指标收集-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-actuator</artifactId>
</dependency>
<!--这个依赖用于把数据转换为prometheus格式使用-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.9.2</version>
</dependency>

📐第 2 步:application配置文件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
spring:
  application:
    name: springboot-prometheus
#对外暴露哪些指标
management:
  endpoints:
    web:
      exposure:
        include: "*"
  #激活promethues并转换为对应的promethues数据
  endpoint:
    prometheus:
      enabled: true
    health:
      show-details: always
  #允许对应的数据指标被导出
  metrics:
    export:
      prometheus:
        enabled: true
server:
  port: 8081

📐第 3 步:为启动类设置自定义标签

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
public class SpringbootdemoApplication {

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

    // 为实例设置自定义标签
    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(
            @Value("${spring.application.name}") String name){
        return registry -> registry.config().commonTags("application",name);
    }

}

📐第 4 步:启动之后结果

🎈Prometheus搭建

📐第 1 步: 创建prometheus配置文件

  • mkdir -p /etc/prometheus
  • vi /etc/prometheus/prometheus.yml
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # 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']
### 以下内容为springboot的配置,主要是这个地方
  - job_name: 'springboot_promethues'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8081']

📐第 2 步 :prometheus docker容器的创建

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
docker run -d --name=prometheus -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -p 9090:9090 bitnami/prometheus:latest

📐第 3 步:查看结果

🎈Grafana搭建

📐第 1 步: 创建grafana容器 docker run -d --name=grafana -p 3000:3000 grafana/grafana

📐第 3 步:登陆账号密码都是admin

📐第 3 步 :指定数据源

4701是针对springboot项目的。

📐第 3 步:查看结果

🍚总结

通过Prometheus与Grafana,成功监控springboot项目的运行状态,像是jvm等一些指标都能够可视化出来。

Written By 知识浅谈

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-12-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🎈项目的创建
  • 🎈Prometheus搭建
  • 🎈Grafana搭建
  • 🍚总结
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档