Prometheus 是一套開源的系統監控報警框架。它由工作在 SoundCloud 的 員工創建,并在 2015 年正式發布的開源項目。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,非常的受歡迎。
簡介
Prometheus 具有以下特點:
- 一個多維數據模型,其中包含通過度量標準名稱和鍵/值對標識的時間序列數據
- PromQL,一種靈活的查詢語言,可利用此維度
- 不依賴分布式存儲; 單服務器節點是自治的
- 時間序列收集通過HTTP上的拉模型進行
- 通過中間網關支持推送時間序列
- 通過服務發現或靜態配置發現目標
- 多種圖形和儀表板支持模式
Prometheus 組成及架構
聲明:該小節參考了文章[Prometheus 入門與實踐]
Prometheus 生態圈中包含了多個組件,其中許多組件是可選的:
- Prometheus Server: 用于收集和存儲時間序列數據。
- Client Library: 客戶端庫,為需要監控的服務生成相應的 metrics 并暴露給 Prometheus server。當 Prometheus server 來 pull 時,直接返回實時狀態的 metrics。
- Push Gateway: 主要用于短期的 jobs。由于這類 jobs 存在時間較短,可能在 Prometheus 來 pull 之前就消失了。為此,這次 jobs 可以直接向 Prometheus server 端推送它們的 metrics。這種方式主要用于服務層面的 metrics,對于機器層面的 metrices,需要使用 node exporter。
- Exporters: 用于暴露已有的第三方服務的 metrics 給 Prometheus。
- Alertmanager: 從 Prometheus server 端接收到 alerts 后,會進行去除重復數據,分組,并路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
一些其他的工具。
其大概的工作流程是:
1.Prometheus server 定期從配置好的 jobs 或者 exporters 中拉 metrics,或者接收來自 Pushgateway 發過來的 metrics,或者從其他的 Prometheus server 中拉 metrics。
2.Prometheus server 在本地存儲收集到的 metrics,并運行已定義好的 alert.rules,記錄新的時間序列或者向 Alertmanager 推送警報。
3.Alertmanager 根據配置文件,對接收到的警報進行處理,發出告警。
4.在圖形界面中,可視化采集數據。
springboot 集成prometheus
在spring boot工程中引入actuator的起步依賴,以及micrometer-registry-prometheus的依賴。
1
2
3
4
5
6
7
8
9
10
11
12
|
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-actuator</ artifactId > </ dependency > < dependency > < groupId >io.micrometer</ groupId > < artifactId >micrometer-registry-prometheus</ artifactId > </ dependency > |
暴露prometheus的接口;暴露metrics.tags,和spring.application.name一致。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server: port: 8081 spring: application: name: my-prometheus management: endpoints: web: exposure: include: 'prometheus' metrics: tags: application: ${spring.application.name} |
寫一個API接口,用作測試。代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@RestController public class TestController { Logger logger = LoggerFactory.getLogger(TestController. class ); @GetMapping ( "/test" ) public String test() { logger.info( "test" ); return "ok" ; } @GetMapping ( "" ) public String home() { logger.info( "home" ); return "ok" ; } } |
在瀏覽器上訪問http://localhost:8081/actuator/prometheus,展示的信息如下,這些信息都是actuator的一些監控信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool # TYPE jvm_gc_max_data_size_bytes gauge jvm_gc_max_data_size_bytes{application="my-prometheus",} 2.863661056E9 # HELP http_server_requests_seconds # TYPE http_server_requests_seconds summary http_server_requests_seconds_count{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0 http_server_requests_seconds_sum{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327 # HELP http_server_requests_seconds_max # TYPE http_server_requests_seconds_max gauge http_server_requests_seconds_max{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327 # HELP jvm_threads_states_threads The current number of threads having NEW state # TYPE jvm_threads_states_threads gauge jvm_threads_states_threads{application="my-prometheus",state="waiting",} 12.0 jvm_threads_states_threads{application="my-prometheus",state="runnable",} 8.0 jvm_threads_states_threads{application="my-prometheus",state="timed-waiting",} 2.0 jvm_threads_states_threads{application="my-prometheus",state="terminated",} 0.0 jvm_threads_states_threads{application="my-prometheus",state="blocked",} 0.0 jvm_threads_states_threads{application="my-prometheus",state="new",} 0.0 # HELP process_files_open_files The open file descriptor count # TYPE process_files_open_files gauge ...省略更多 |
安裝Prometheus
安裝Prometheus很簡單,在linux系統上安裝,執行以下的安裝命令。其他的操作系統,比如windows、mac等在官網上(https://prometheus.io/download/)下載并安裝。
1
2
3
|
wget https: //github .com /prometheus/prometheus/releases/download/v2 .19.2 /prometheus-2 .19.2.darwin-amd64. tar .gz tar xvfz prometheus-*. tar .gz cd prometheus-* |
修改Prometheus的配置文件prometheus.yml,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # 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' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090'] - job_name: 'springboot_prometheus' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['127.0.0.1:8081'] |
- config.job_name,配置job的名稱
- config.scrape_interval,配置多久抓一次監控信息
- config.metrics_path,獲取監控信息的接口
- config.static_configs.targets配置獲取監控信息的地址。
使用以下的命令啟動prometheus,并通過–config.file指定配置文件
1
|
./prometheus --config.file=prometheus.yml |
多次請求springboot項目的接口http://localhost:8081/test , 并訪問prometheus的控制臺http://localhost:9090/,展示的界面如下:
prometheus提供了一些可視化圖,比如使用柱狀圖來展示每秒請求數:
安裝grafana
grafana 是一款采用 go 語言編寫的開源應用,主要用于大規模指標數據的可視化展現,是網絡架構和應用分析中最流行的時序數據展示工具,目前已經支持絕大部分常用的時序數據庫。使用grafana去展示prometheus上的數據。先安裝,安裝命令如下:
1
2
3
|
wget https: //dl .grafana.com /oss/release/grafana-7 .0.4.darwin-amd64. tar .gz tar -zxvf grafana-7.0.4.darwin-amd64. tar .gz . /grafana-server |
訪問http://localhost:3000/,初始密碼:admin/admin。
配置數據源,如圖:
配置prometheus的地址:http://localhost:9090 ,如圖所示:
在dashboard界面新建panel,展示的metrics為http_server_request_seconds_count,即展示了以時間為橫軸,請求數為縱軸的請求曲線,如圖所示:
參考資料
[Prometheus 入門與實踐]
到此這篇關于springboot集成普羅米修斯(Prometheus)的方法的文章就介紹到這了,更多相關springboot集成普羅米修斯內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/forezp/article/details/107465307