SpringBoot整合Prometheus

logo-no-title.d91d5ac6

Micrometer 是一个统一监控指标采集的门面,这个有点类似SLF4J,具体的指标数据采集实现有AppOptics, Azure Monitor, Atlas, CloudWatch, Datadog, Dynatrace, Elastic, Ganglia, Graphite, Humio, Influx/Telegraf, JMX, KairosDB, New Relic, Prometheus, SignalFx, Stackdriver, StatsD,Wavefront等。因此使用Micrometer时,只需更换底层实现包,应用程序无需修改任何代码即可对接到不同监控系统。

​ 在最新SpringBoot2.0中,Micrometer门面已经整合到了spring-boot-starter-actuator项目中,我们只需引入相应的具体实现包即可对接到相应的监控系统,本次将使用promethues来监控、采集SpringBoot的指标数据。

引入promehtus依赖

implementation 'org.springframework.boot:spring-boot-starter-actuator'
runtimeOnly 'io.micrometer:micrometer-registry-prometheus'

配置指标endpoint

默认情况下指标的endpoint可以与服务同一个端口,通常为了不对业务造成干扰,使用额外的端口向外暴露指标endpoint,只需更改application.yml即可实现,如下:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  server:
    port: 30000

浏览器访问http://localhost:30000/actuator/metrics 可以查看到目前存在监控指标,这些指标都是默认开启的。

image-20201210154752319

使用promethues采集指标数据

目前dockerHub上未提供官方的镜像,这里还使用二进制文件的方式来进行监控数据的采集,下载完二进制包后,需修改promethues.yml文件,配置scrape_configs,详细如下:

# 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=` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus' #指标路径
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['127.0.0.1:30000'] #指标暴露地址端口

启动promethues

./prometheus --config.file=./prometheus.yml

使用Grafana可视化指标

这里使用docker运行grafana,默认账号密码:admin/admin

docker run -d -p 3000:3000 grafana/grafana

来到设置,准备添加数据源。

image-20201210160946826

这里选择promethues

image-20201210161114105

输入 ip(ip不能填127.0.0.1 或者是localhost,局域网ip就行) 和 端口即可,点击下面的save&Test完成数据源的添加。

添加Panel,这里选之前创建的数据源,选择指标即可实现可视化。

image-20201210161839159

监控http响应

默认已经开启了http请求的监控,但是未开启histogram

management:
  endpoints:
    web:
      exposure:
        include: "*"
  server:
    port: 30000
  metrics:
    distribution:
      percentiles-histogram[http.server.requests]: true 
      maximum-expected-value[http.server.requests]: 10000 #预期最大值
      minimum-expected-value[http.server.requests]: 1 #预期最小值

编写测试接口

@RestController
public class TestController {


    @GetMapping(value = "/hello")
    public String helloPromethues(){
        try {

            TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  "helloPromethues";
    }
}

使用wrk压测该接口

wrk -t4 -c2000 -d100s  http://127.0.0.1:8080/hello

在Grafana中创建Panel,指标选择http_server_requests_seconds_bucket,并修改Y轴单位为秒。

image-20201210165035652

获取源码

你可能感兴趣的:(SpringBoot整合Prometheus)