Grafana+Prometheus打造springboot監控平臺

1. 環境

springboot 1.5.10.RELEASElinux

Grafana 5.4.2git

Prometheus 2.6.0github

jdk 1.8web

2.經過micrometer與springboot應用和prometheus的集成

在項目pom.xml中添加以下依賴spring

<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中增長以下:json

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

在 application.yml中添加以下配置(由於是測試,因此我把全部端點都暴露了,生產環境自行選擇打開端點)ubuntu

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 以下圖瀏覽器

 

經過瀏覽器查看prometheus.json文件以下圖:springboot

至此,應用側的prometheus client的工做已經完成。app

 

3.安裝prometheus

下載你想安裝的prometheus版本,地址爲download prometheus

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

解壓

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

在某目錄建立 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 ,以下圖

 

至此,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
方式1、Start Grafana by running:

sudo service grafana-server start
sudo update-rc.d grafana-server defaults //設置開機啓動(可選)

方式2、To start the service using systemd:

systemctl daemon-reload
systemctl start grafana-server
systemctl status grafana-server
sudo systemctl enable grafana-server.service //設置開機啓動

 

grafana添加數據源,配置以下

 

 6.建立看板

grafana支持不少種看板,你能夠根據不一樣的指標生成圖表,

 

由於圖表的配置比較複雜,這裏沒有深刻的研究,而是選用了大神 們作好的模板,對接數據源進行展現

 https://grafana.com/dashboards 在這裏能夠搜索不一樣的模板

選擇一個你想要的點擊去,而後複製id

 

打開下圖頁面,並將id粘貼進去,光標離開輸入框,會自動加載模板配置

 

接着選datasource:

 

而後選取數據源,點擊import按鈕,完成模板添加

看數據都是空的,由於這個是springboot2.x的。要換成springboot1.x的,以下圖:

結果:

完成。

多個應用的配置,以下配置兩個微服務應用:

# 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的頁面:

相關文章
相關標籤/搜索