使用prometheus监控springboot--入门

pom.xml引入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

       <!-- <dependency>
            <groupId>com.taobao.arthas</groupId>
            <artifactId>arthas-spring-boot-starter</artifactId>
            <version>3.6.7</version>
            <scope>runtime</scope>
        </dependency>-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

编辑application.properties

# Actuator Web 访问端口 默认8081
management.server.port=8080
# 暴露所有端点
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true

启动springboot应用
在这里插入图片描述
在这里插入图片描述
浏览器正常展示metrics指标
springboot应用完成
接下来处理prometheus
拷贝一份prometheus.yml
在prometheus.yml添加以下监控springboot应用内容

# 监控springboot应用配置 
  - job_name: "boot2prometheus-service"
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["192.168.1.106:8080"]

修改后的完整的prometheus.yml文件内容

# 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: "boot2prometheus-service"
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["192.168.1.106:8080"]

在etc路径下创建prometheus目录
mkidr /etc/prometheus
在执行以下命令创建prometheus.yml文件

cat > /etc/prometheus/prometheus.yml <<-'EOF'
# 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: "boot2prometheus-service"
    scrape_interval: 5s
    scrape_timeout: 5s
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["192.168.1.106:8080"]
EOF

确保宿主机安装了docker
采用docker命令安装prometheus

docker run \
	-d \
    -p 9090:9090 \
    -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    bitnami/prometheus

prometheus中status选择targets
在这里插入图片描述
这里也采用docker命令安装grafana

docker run -d -p 3000:3000 --name=grafana grafana/grafana-enterprise

grafana默认账户密码为admin
点击Configuration(小齿轮图标)
选择Data sources 点击Add data source按钮
在这里插入图片描述
填写prometheus地址并保存
在这里插入图片描述

点击+号图标并选择import
在这里插入图片描述
填4701编号点击load按钮
在这里插入图片描述
选择prometheus点击Import按钮
在这里插入图片描述
导入后会展示各种图标,里面有java常用的指标监控,记得选择右上角的时间范围,默认是24小时,对于刚启动的程序是没有数据的,我们选择过去5分钟的选项即可观察到数据
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值