1. 环境

springboot 1.5.10.RELEASE

Grafana 5.4.2

Prometheus 2.6.0

jdk 1.8

2.通过micrometer与springboot应用和prometheus的集成

在项目pom.xml中添加如下依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
<!--<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.5.0</version>
</dependency>-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-spring-legacy</artifactId>
<version>1.1.1</version>
</dependency>

gradle.build中增加如下:

    compile 'io.micrometer:micrometer-registry-prometheus:1.1.1'
    compile 'io.micrometer:micrometer-spring-legacy:1.1.1'

在 application.yml中添加如下配置(因为是测试,所以我把所有端点都暴露了,生产环境自行选择打开端点)

management:
  endpoints:
    web:
      exposure:
        include: health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
    jmx:
      exposure:
        include: '*'
    shutdown:
      enabled: false
  metrics:
    distribution:
      percentiles-histogram[http.server.requests]: true
  security:
    enabled: false

启动项目,在eclipse中可以看到接口 /prometheus 如下图

 Grafana+Prometheus打造springboot监控平台_lua

通过浏览器查看prometheus.json文件如下图:

Grafana+Prometheus打造springboot监控平台_monitor_02

至此,应用侧的prometheus client的工作已经完成。

 

3.安装prometheus

下载你想安装的prometheus版本,地址为download prometheus

我下载的是prometheus-2.6.0.linux-amd64.tar.gz 

解压

tar xvfz prometheus-*.tar.gz
cd prometheus-*

Grafana+Prometheus打造springboot监控平台_开机启动_03

在某目录创建 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'.

    metrics_path: /prometheus
    static_configs:
    - targets: ['10.200.110.100:8082']#此处填写 Spring Boot 应用的 IP + 端口号

注意:metrics_path:和targets的配置。

启动

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

在Status->Targets页面下,我们可以看到我们配置的Target,它们的State为UP ,如下图

 Grafana+Prometheus打造springboot监控平台_开机启动_04

至此,prometheus和springboot已经连接成功。

4.安装Grafana

使用的是ubuntu 16.04TLS,所以找到官网相对应的Ubuntu方式,这是官网的链接地址:https://grafana.com/grafana/download?platform=linux

wget https://dl.grafana.com/oss/release/grafana_5.4.2_amd64.deb 
sudo dpkg -i grafana_5.4.2_amd64.deb 

启动grafana
方式一、Start Grafana by running:

sudo service grafana-server start
sudo update-rc.d grafana-server defaults //设置开机启动(可选)

方式二、To start the service using systemd:

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
sudo systemctl enable grafana-server.service //设置开机启动

Grafana+Prometheus打造springboot监控平台_monitor_05

 

grafana添加数据源,配置如下

 Grafana+Prometheus打造springboot监控平台_数据源_06

 6.创建看板

grafana支持很多种看板,你可以根据不同的指标生成图表,

 Grafana+Prometheus打造springboot监控平台_monitor_07

因为图表的配置比较复杂,这里没有深入的研究,而是选用了大神 们做好的模板,对接数据源进行展示

 https://grafana.com/dashboards 在这里可以搜索不同的模板

Grafana+Prometheus打造springboot监控平台_开机启动_08

选择一个你想要的点击去,然后复制id

 Grafana+Prometheus打造springboot监控平台_lua_09

打开下图页面,并将id粘贴进去,光标离开输入框,会自动加载模板配置

 Grafana+Prometheus打造springboot监控平台_开机启动_10

接着选datasource:

 Grafana+Prometheus打造springboot监控平台_lua_11

然后选取数据源,点击import按钮,完成模板添加

Grafana+Prometheus打造springboot监控平台_数据源_12

看数据都是空的,因为这个是springboot2.x的。要换成springboot1.x的,如下图:

Grafana+Prometheus打造springboot监控平台_lua_13

结果:

Grafana+Prometheus打造springboot监控平台_开机启动_14

完成。

多个应用的配置,如下配置两个微服务应用:

# 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: 'service-productor'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    metrics_path: /prometheus
    static_configs:
    - targets: ['10.200.110.100:8082']
  - job_name: 'service-consumer'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    metrics_path: /prometheus
    static_configs:
    - targets: ['10.200.110.100:8081']

 重启prometheus后,再刷新target页面:

Grafana+Prometheus打造springboot监控平台_monitor_15

回到Grafana的页面:

Grafana+Prometheus打造springboot监控平台_monitor_16