一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - springboot集成普羅米修斯(Prometheus)的方法

springboot集成普羅米修斯(Prometheus)的方法

2020-08-09 20:49方志朋 Java教程

這篇文章主要介紹了springboot集成普羅米修斯(Prometheus)的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

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 等。

一些其他的工具。

springboot集成普羅米修斯(Prometheus)的方法

其大概的工作流程是:

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/,展示的界面如下:

springboot集成普羅米修斯(Prometheus)的方法

prometheus提供了一些可視化圖,比如使用柱狀圖來展示每秒請求數:

springboot集成普羅米修斯(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。

配置數據源,如圖:

springboot集成普羅米修斯(Prometheus)的方法

配置prometheus的地址:http://localhost:9090 ,如圖所示:

springboot集成普羅米修斯(Prometheus)的方法

在dashboard界面新建panel,展示的metrics為http_server_request_seconds_count,即展示了以時間為橫軸,請求數為縱軸的請求曲線,如圖所示:

springboot集成普羅米修斯(Prometheus)的方法

參考資料

[Prometheus 入門與實踐]

到此這篇關于springboot集成普羅米修斯(Prometheus)的方法的文章就介紹到這了,更多相關springboot集成普羅米修斯內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/forezp/article/details/107465307

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 97久久久亚洲综合久久88 | 四虎最新永久免费视频 | 亚洲视频中文字幕 | 国产我不卡 | www毛片| 国产在线乱子伦一区二区 | 久久免费看少妇高潮A片2012 | 久久大胆视频 | 国产小视频在线 | 日韩在线a视频免费播放 | 天堂网在线.www天堂在线视频 | 亚洲黄色天堂 | 久久久久久久久人体 | 午夜免费无码福利视频麻豆 | 欧美一区二区福利视频 | 俄罗斯一级在线播放 | 俄罗斯烧性春三级k8播放 | 好姑娘在线完整版视频 | 欧美一级xxxx俄罗斯一级 | 憋尿调教绝望之岛 | 亚洲福利一区二区精品秒拍 | 亚洲欧美日韩成人一区在线 | 天天做天天爽 | 成人性色生活片免费网 | 成人高辣h视频一区二区在线观看 | 男人懂得网站 | 亚洲色图亚洲色图 | 歪歪视频在线播放无遮挡 | 国产无限 | 超级乱淫 不详 | 91看片淫黄大片欧美看国产片 | 牧教师在线观看 | 污漫日本E同人 | 日韩一区二区三区四区五区 | 久久足恋网 | 亚洲成片在线看 | 极品奶妈漫画 | 日本午夜影院 | 午夜精品久久久久 | 欧美日韩国产亚洲一区二区 | 久久精品国产色蜜蜜麻豆国语版 |