springboot Actuator整合prometheus并使用grafana可视化(prometheus,grafana使用docker搭建)

springboot 版本2.7.4

springboot

依赖

 		<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>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
            <scope>runtime</scope>
        </dependency>

application.yaml

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include:
          - prometheus
          - health
          - info

  metrics:
    tags:
      application: tcoding-demo

main 方法

@SpringBootApplication
@RestController
public class Application {

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

    @GetMapping
    public String hello(){
        return "hello world";
    }
}

prometheus

拉取镜像

docker pull bitnami/prometheus:2.38.0

运行

方式一:进入容器修改prometheus.yml

 docker run -itd --name prom -p 9090:9090 bitnami/prometheus:2.38.0

进入容器修改prometheus.yml

docker exec -it -u 0  prom /bin/bash 

apt-get update -y

apt-get install -y vim

修改metrics_path和static_configs

host.docker.internal 是容器访问宿主机hostname

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "tcoding-demo"

    metrics_path: '/actuator/prometheus'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["host.docker.internal:8080"]

然后重启 docker restart prom

方式二

本地新建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: "tcoding-demo"

    metrics_path: '/actuator/prometheus'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["host.docker.internal:8080"]
 docker run -itd --name prom -p 9090:9090 \
 -v /your path/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus \
 bitnami/prometheus:2.38.0 

grafana

docker搭建

docker pull grafana/grafana:9.1.6 
docker run \
      -itd \
      -p 3000:3000 \
      --name=grafana \
      --link=prom \
      grafana/grafana:9.1.6

grafana配置Prometheus

访问 http://localhost:3000/
在这里插入图片描述
修改数据源url
在这里插入图片描述

Dashboards配置

下载配置

https://grafana.com/grafana/dashboards/
在这里插入图片描述

我是用的是jvm

在这里插入图片描述
上传刚刚下载的json文件

测试

  • 启动springboot

  • grafana切换一下时间范围,太长显示有问题(没有历史数据)
    在这里插入图片描述

  • 效果预览
    在这里插入图片描述

源码地址

https://github.com/googalAmbition/hello-spring-boot/tree/main/44-actuator-prometheus

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值