Prometheus+Grafana监控Springboot应用

一、整体结构

整体结构图

监控架构图

  • springboot(micrometer)产生监控数据。
  • Prometheus获取springboot的监控数据。
  • Grafana 对接 Prometheus 数据源,展示成相关图表。

二、 实践步骤

1)创建springboot应用集成 micrometer

  1. 在pom中添加micrometer依赖

            <dependency>
                <groupId>io.micrometer</groupId>
                <artifactId>micrometer-registry-prometheus</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-actuator-autoconfigure</artifactId>
            </dependency>
    
    
  2. 修改配置文件

    # 定义应用的名称
    spring.application.name=springboot2prometheus
    # 公开prometheus的接口,也可以写*表示公开所有
    management.endpoints.web.exposure.include=prometheus
    
    
  3. 启动应用并访问http://localhost:8080/actuator/prometheus即可看到相关的监控数据

2)部署 Prometheus

由于docker安装非常方便而且速度很快,所以采用docker安装的方式,如不熟悉docker的可以看往期的docker技术文章

  1. 执行docker命令

    docker run --rm -p 9090:9090 --name prometheus prom/prometheus
    
    
  2. 从容器中拷贝配置文件到本地

    mkdir -p ~/etc/prometheus
    cd ~/etc/prometheus
    docker cp prometheus:/etc/prometheus/prometheus.yml .
    
    
  3. 修改拷贝出的配置文件,修改最后三行,完整文件如下

    # 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'.
        metrics_path: '/actuator/prometheus'
        static_configs:
          - targets: ["192.168.18.114:8080"] # 为springboot应用所在地址
    
    
  4. 关闭容器并重新执行(挂载上新的配置文件)

    docker run --rm -p 9090:9090 -v ~/etc/prometheus:/etc/prometheus --name prometheus prom/prometheus
    
    

3)部署Grafana

  1. docker安装

    docker run --rm -p 3000:3000 --name=grafana grafana/grafana
    
    
  2. 访问http://localhost:3000/,默认用户:admin,密码:admin

  3. 新建数据源,点击左侧的齿轮按钮中的DataSource

    填入以下信息

  4. 导入仪表盘,使用4701导入

  5. 点击import后即可看到监控面板

4)自定义监控数据和面板

  1. 修改springboot应用,添加监控数据-监控请求总数

    RequestCounterAop.java

    @Aspect
    @Component
    public class RequestCounterAop {
    
        private Counter counter;
    
        @Pointcut("bean(*Controller) && execution(public * *(..))")
        public void pointCut() {
        }
    
        public RequestCounterAop(MeterRegistry registry) {
            this.counter = registry.counter("requests_total", "status", "success");
        }
    
        @Before("pointCut()")
        public void count(JoinPoint joinPoint) {
            counter.increment(); //请求计数
        }
    }
    
    

    TestController.java

    @RestController
    @RequestMapping("/home")
    public class TestController {
        @GetMapping("")
        public String hello() {
            return "hello;";
        }
    }
    
    
  2. 在浏览器中访问几次写好的测试接口http://localhost:8080/home,访问http://localhost:8080/actuator/prometheus即可看到监控数据

  3. 打开grafana控制面板

填入以下内容,点击apply就看到监控面板

requests_total{application="springboot2prometheus",status="success",}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章