目录

  • 一、增加依赖
  • 二、Prometheus 装置与配置
  • 三、Grafana 装置和配置
  • 四、自定义监控指标

一、增加依赖

  • Maven pom.xml
<!--  第一条必须加,否则会导致 Could not autowire. No beans of 'xxxx' type found 的谬误  --><dependency>    <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>  <groupId>io.micrometer</groupId>  <artifactId>micrometer-core</artifactId></dependency><dependency>  <groupId>io.micrometer</groupId>  <artifactId>micrometer-registry-prometheus</artifactId></dependency>
  • Gradle build.gradle
implementation 'org.springframework.boot:spring-boot-starter-actuator'compile 'io.micrometer:micrometer-registry-prometheus'compile 'io.micrometer:micrometer-core'
  • 关上 Prometheus 监控接口 application.properties
server.port=8088spring.application.name=springboot2-prometheusmanagement.endpoints.web.exposure.include=*management.metrics.tags.application=${spring.application.name}

能够间接运行程序,拜访http://localhost:8088/actuator/prometheus能够看到上面的内容:

# HELP jvm_buffer_total_capacity_bytes An estimate of the total capacity of the buffers in this pool# TYPE jvm_buffer_total_capacity_bytes gaugejvm_buffer_total_capacity_bytes{id="direct",} 90112.0jvm_buffer_total_capacity_bytes{id="mapped",} 0.0# HELP tomcat_sessions_expired_sessions_total  # TYPE tomcat_sessions_expired_sessions_total countertomcat_sessions_expired_sessions_total 0.0# HELP jvm_classes_unloaded_classes_total The total number of classes unloaded since the Java virtual machine has started execution# TYPE jvm_classes_unloaded_classes_total counterjvm_classes_unloaded_classes_total 1.0# HELP jvm_buffer_count_buffers An estimate of the number of buffers in the pool# TYPE jvm_buffer_count_buffers gaugejvm_buffer_count_buffers{id="direct",} 11.0jvm_buffer_count_buffers{id="mapped",} 0.0# HELP system_cpu_usage The "recent cpu usage" for the whole system# TYPE system_cpu_usage gaugesystem_cpu_usage 0.0939447637893599# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool# TYPE jvm_gc_max_data_size_bytes gaugejvm_gc_max_data_size_bytes 2.841116672E9# 此处省略超多字...

二、Prometheus 装置与配置

应用 docker 运行 Prometheus(仅初始测试)

docker run --name prometheus -d -p 9090:9090 prom/prometheus:latest

写配置文件prometheus.yml

# my global configglobal:  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).# 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']  # demo job  -  job_name: 'springboot-actuator-prometheus-test' # job name     metrics_path: '/actuator/prometheus' # 指标获取门路     scrape_interval: 5s # 距离     basic_auth: # Spring Security basic auth        username: 'actuator'       password: 'actuator'     static_configs:     - targets: ['docker.for.mac.localhost:18080'] # 实例的地址,默认的协定是http (这里开始有问题,间接写 localhost 是拜访容器内的地址,而不是宿主机的。可通过在网页上方 status -> targets 查看对应的服务状况

运行 docker

docker run -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

拜访 http://localhost:9090,可看到如下界面

  • 点击 Insert metric at cursor ,即可抉择监控指标;点击 Graph ,即可让指标以图表形式展现;点击Execute 按钮,即可看到指标图

三、Grafana 装置和配置

1、启动

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

2、登录

拜访 http://localhost:3000/login ,初始账号/明码为:admin/admin

3、配置数据源

  • 点击左侧齿轮ConfigurationAdd Data Source,会看到如下界面:

  • 这里咱们抉择Prometheus 当做数据源,这里咱们就配置一下Prometheus 的拜访地址,点击 Save & Test

4、创立监控 Dashboard

  • 点击导航栏上的 + 按钮,并点击Dashboard,将会看到相似如下的界面

  • 点击+ Add new panel

四、自定义监控指标

1、创立 Prometheus 监控治理类PrometheusCustomMonitor

import io.micrometer.core.instrument.Counter;import io.micrometer.core.instrument.DistributionSummary;import io.micrometer.core.instrument.MeterRegistry;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import java.util.concurrent.atomic.AtomicInteger;@Componentpublic class PrometheusCustomMonitor {    private Counter requestErrorCount;    private Counter orderCount;    private DistributionSummary amountSum;    private AtomicInteger failCaseNum;    private final MeterRegistry registry;    @Autowired    public PrometheusCustomMonitor(MeterRegistry registry) {        this.registry = registry;    }    @PostConstruct    private void init() {        requestErrorCount = registry.counter("requests_error_total", "status", "error");        orderCount = registry.counter("order_request_count", "order", "test-svc");        amountSum = registry.summary("order_amount_sum", "orderAmount", "test-svc");        failCaseNum = registry.gauge("fail_case_num", new AtomicInteger(0));    }    public Counter getRequestErrorCount() {        return requestErrorCount;    }    public Counter getOrderCount() {        return orderCount;    }    public DistributionSummary getAmountSum() {        return amountSum;    }    public AtomicInteger getFailCaseNum() {        return failCaseNum;    }}

2、新增/order接口

flag="1"时,抛异样,模仿下单失败状况。在接口中统计order_request_countorder_amount_sum

import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.util.Random;@RestControllerpublic class TestController {    @Resource    private PrometheusCustomMonitor monitor;    @RequestMapping("/order")    public String order(@RequestParam(defaultValue = "0") String flag) throws Exception {        // 统计下单次数        monitor.getOrderCount().increment();        if ("1".equals(flag)) {            throw new Exception("出错啦");        }        Random random = new Random();        int amount = random.nextInt(100);        // 统计金额        monitor.getAmountSum().record(amount);        monitor.getFailCaseNum().set(amount);        return "下单胜利, 金额: " + amount;    }}

3、新增全局异样处理器GlobalExceptionHandler

统计下单失败次数requests_error_total

import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;@ControllerAdvicepublic class GlobalExceptionHandler {    @Resource    private PrometheusCustomMonitor monitor;    @ResponseBody    @ExceptionHandler(value = Exception.class)    public String handle(Exception e) {        monitor.getRequestErrorCount().increment();        return "error, message: " + e.getMessage();    }}

4、测试

启动我的项目,拜访http://localhost:8080/orderhttp://localhost:8080/order?flag=1模仿下单胜利和失败的状况,而后咱们拜访http://localhost:8080/actuator/prometheus,能够看到咱们自定义指标曾经被 /prometheus 端点裸露进去

# HELP requests_error_total  # TYPE requests_error_total counterrequests_error_total{application="springboot-actuator-prometheus-test",status="error",} 41.0# HELP order_request_count_total  # TYPE order_request_count_total counterorder_request_count_total{application="springboot-actuator-prometheus-test",order="test-svc",} 94.0# HELP order_amount_sum  # TYPE order_amount_sum summaryorder_amount_sum_count{application="springboot-actuator-prometheus-test",orderAmount="test-svc",} 53.0order_amount_sum_sum{application="springboot-actuator-prometheus-test",orderAmount="test-svc",} 2701.0

5、应用 Prometheus 监控

从新运行 docker

docker run -d -p 9090:9090 -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

抉择对应指标后能够看到数据变动

6、应用 Grafana 展现

在 Dashboard 界面抉择对应的监控指标即可


参考资料:

Metric types | Prometheus

IntelliJ IDEA创立第一个Spring Boot我的项目\_Study Notes-CSDN博客

Spring Boot 应用 Micrometer 集成 Prometheus 监控 Java 利用性能 【springboot 2.0】

Micrometer Application Monitoring【官网文档】

Spring Boot 微服务利用集成Prometheus + Grafana 实现监控告警 ★

Monitoring Java Spring Boot applications with Prometheus: Part 1 | by Arush Salil | Kubernauts 【放弃这个教程?】client java 不反对 springboot 2.x,最高反对 1.5

Spring Boot 参考指南(端点)\_风持续吹 - SegmentFault 思否