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 如下圖
通過瀏覽器查看prometheus.json文件如下圖:
至此,應用側的prometheus client的工作已經完成。
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
方式一、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添加數據源,配置如下
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的頁面: