亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

瀏覽:77日期:2023-07-20 13:39:22
1. Prometheus是什么

Prometheus是一個(gè)具有活躍生態(tài)系統(tǒng)的開源系統(tǒng)監(jiān)控和告警工具包。一言以蔽之,它是一套開源監(jiān)控解決方案。

Prometheus主要特性:

多維數(shù)據(jù)模型,其中包含由指標(biāo)名稱和鍵/值對(duì)標(biāo)識(shí)的時(shí)間序列數(shù)據(jù) PromQL,一種靈活的查詢語言 不依賴分布式存儲(chǔ); 單服務(wù)器節(jié)點(diǎn)是自治的 時(shí)間序列收集通過HTTP上的pull模型進(jìn)行 通過中間網(wǎng)關(guān)支持推送(push)時(shí)間序列 通過服務(wù)發(fā)現(xiàn)或靜態(tài)配置發(fā)現(xiàn)目標(biāo) 支持多種模式的圖形和儀表盤

為什么用pull(拉取)而不用push(推送)呢?

因?yàn)椋琾ull有以下優(yōu)勢(shì):

進(jìn)行更改時(shí),可以在筆記本電腦上運(yùn)行監(jiān)控 可以更輕松地判斷目標(biāo)是否下線 可以手動(dòng)轉(zhuǎn)到目標(biāo)并使用Web瀏覽器檢查其運(yùn)行狀況

目標(biāo)暴露HTTP端點(diǎn),Prometheus服務(wù)端通過HTTP主動(dòng)拉取數(shù)據(jù)。既然是服務(wù)端自己主動(dòng)向目標(biāo)拉取數(shù)據(jù),那么服務(wù)端運(yùn)行在本地(我們自己的電腦上)也是可以的,只要能訪問目標(biāo)端點(diǎn)即可,同時(shí)就像心跳檢測(cè)一樣可以判斷目標(biāo)是否下線,還有,服務(wù)端自己主動(dòng)拉取,那么想拉取誰的數(shù)據(jù)就拉取誰的數(shù)據(jù),因而可以隨意切換拉取目標(biāo)。

回想一下Skywalking是怎么做的,SkyWalking有客戶端和服務(wù)端,需要在目標(biāo)服務(wù)上安裝探針(agent),探針采集目標(biāo)服務(wù)的指標(biāo)數(shù)據(jù),上報(bào)給服務(wù)端OAP服務(wù),這個(gè)對(duì)目標(biāo)有一定的侵入性,不過可以接受。Prometheus不需要探針,可以借助push gateway來實(shí)現(xiàn)push效果。

對(duì)了,有個(gè)名詞要先說清楚,metrics (譯:度量,指標(biāo)),個(gè)人更傾向于把它翻譯成指標(biāo),后面說指標(biāo)就是metrics

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

2. 基本概念2.1. 數(shù)據(jù)模型

Prometheus基本上將所有數(shù)據(jù)存儲(chǔ)為時(shí)間序列:具有時(shí)間戳的值流,它們屬于同一個(gè)指標(biāo)和同一組標(biāo)記的維度。除了存儲(chǔ)的時(shí)間序列外,Prometheus還可以生成臨時(shí)派生的時(shí)間序列作為查詢的結(jié)果。

Metric names and labels

Every time series is uniquely identified by its metric name and optional key-value pairs called labels.

每個(gè)時(shí)間序列都由它的指標(biāo)名稱和稱為標(biāo)簽的可選鍵/值對(duì)唯一標(biāo)識(shí)。

樣本構(gòu)成實(shí)際的時(shí)間序列數(shù)據(jù)。 每個(gè)樣本包括:

一個(gè)64位的浮點(diǎn)值 一個(gè)毫秒時(shí)間戳

給定指標(biāo)名稱和一組標(biāo)簽,時(shí)間序列通常使用這種符號(hào)來標(biāo)識(shí):

<metric name>{<label name>=<label value>, ...}

例如,有一個(gè)時(shí)間序列,指標(biāo)名稱是api_http_requests_total,標(biāo)簽有method='POST'和handler='/messages',那么它可能被表示成這樣:

api_http_requests_total{method='POST', handler='/messages'}2.2. 指標(biāo)類型

Counter

counter是一個(gè)累積量度,代表一個(gè)單調(diào)遞增的計(jì)數(shù)器,其值只能增加或在重新啟動(dòng)時(shí)重置為零。例如,可以使用計(jì)數(shù)器來表示已服務(wù)請(qǐng)求數(shù),已完成任務(wù)或錯(cuò)誤的數(shù)量。

不要使用計(jì)數(shù)器來顯示可以減小的值。例如,請(qǐng)勿對(duì)當(dāng)前正在運(yùn)行的進(jìn)程數(shù)使用計(jì)數(shù)器,代替的應(yīng)該使用量規(guī)。

Gauge

量規(guī)是一種指標(biāo),代表可以任意上下波動(dòng)的單個(gè)數(shù)值。

量規(guī)通常用于測(cè)量值,例如溫度或當(dāng)前內(nèi)存使用量,還用于可能上升和下降的“計(jì)數(shù)”,例如并發(fā)請(qǐng)求數(shù)。

Histogram

直方圖對(duì)觀察結(jié)果(通常是請(qǐng)求持續(xù)時(shí)間或響應(yīng)大小)進(jìn)行抽樣,并在可配置的桶中對(duì)它們進(jìn)行計(jì)數(shù)。它還提供了所有觀測(cè)值的總和。

一個(gè)基礎(chǔ)指標(biāo)名稱為<basename>的直方圖在抓取期間會(huì)暴露多個(gè)時(shí)間序列:

觀察桶的累積計(jì)數(shù)器,表示為 <basename>_bucket{le='<upper inclusive bound>'} 所有觀測(cè)值的總和,表示為 <basename>_sum 觀察到的事件數(shù)量,表示為 <basename>_count

Summary

與直方圖類似,摘要對(duì)觀察結(jié)果(通常是請(qǐng)求持續(xù)時(shí)間和響應(yīng)大小等內(nèi)容)進(jìn)行抽樣分析。雖然它還提供了觀測(cè)值的總數(shù)和所有觀測(cè)值的總和,但它可以計(jì)算滑動(dòng)時(shí)間窗口內(nèi)的可配置分位數(shù)。

一個(gè)基礎(chǔ)指標(biāo)名稱為<basename>的摘要在抓取期間暴露多個(gè)時(shí)間序列:

觀察桶的累積計(jì)數(shù)器,表示為 <basename>_bucket{le='<upper inclusive bound>'} 所有觀測(cè)值的總和,表示為 <basename>_sum 觀察到的事件數(shù)量,表示為 <basename>_count2.3. 作業(yè)和實(shí)例

在Prometheus的術(shù)語中,可以抓取的端點(diǎn)稱為實(shí)例,通常對(duì)應(yīng)于單個(gè)進(jìn)程。具有相同目的的實(shí)例集合,稱為作業(yè)。

例如,一個(gè)作業(yè)有四個(gè)實(shí)例:

job: api-serverinstance 1: 1.2.3.4:5670instance 2: 1.2.3.4:5671instance 3: 5.6.7.8:5670instance 4: 5.6.7.8:5671

當(dāng)Prometheus抓取目標(biāo)時(shí),它會(huì)自動(dòng)在抓取的時(shí)間序列上附加一些標(biāo)簽,以識(shí)別被抓取的目標(biāo):

job:目標(biāo)所屬的已配置的作業(yè)名稱 instance:被抓取的目標(biāo)URL的<host>:<port>部分3. 安裝與配置

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

Prometheus通過抓取指標(biāo)HTTP端點(diǎn)從目標(biāo)收集指標(biāo)。由于Prometheus以相同的方式暴露自己的數(shù)據(jù),因此它也可以抓取并監(jiān)視其自身的健康狀況。

默認(rèn)情況下,不用更改配置,直接運(yùn)行就可以抓取prometheus自身的健康狀況數(shù)據(jù)

# Start Prometheus.# By default, Prometheus stores its database in ./data (flag --storage.tsdb.path)./prometheus --config.file=prometheus.yml

直接訪問 localhost:9090

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

訪問 localhost:9090/metrics 可以查看各項(xiàng)指標(biāo)

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

舉個(gè)例子

輸入以下表達(dá)式,點(diǎn)“Execute”,可以看到以下效果

prometheus_target_interval_length_seconds

這應(yīng)該返回多個(gè)不同的時(shí)間序列(以及每個(gè)序列的最新值),每個(gè)序列的指標(biāo)名稱均為prometheus_target_interval_length_seconds,但具有不同的標(biāo)簽。

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

這個(gè)是以圖形化的方式展示指標(biāo),通過localhost:9090/metrics查看也是一樣的

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

如果我們只對(duì)99%的延遲感興趣,我們可以使用以下查詢:

prometheus_target_interval_length_seconds{quantile='0.99'}

為了計(jì)算返回的時(shí)間序列數(shù),查詢應(yīng)該這樣寫:

count(prometheus_target_interval_length_seconds)

接下來,讓我們利用Node Exporter來多添加幾個(gè)目標(biāo):

tar -xzvf node_exporter-*.*.tar.gzcd node_exporter-*.*# Start 3 example targets in separate terminals:./node_exporter --web.listen-address 127.0.0.1:8080./node_exporter --web.listen-address 127.0.0.1:8081./node_exporter --web.listen-address 127.0.0.1:8082

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

接下來,配置Prometheus來抓取這三個(gè)新目標(biāo)

首先,定義一個(gè)名為’node’的作業(yè),這個(gè)作業(yè)負(fù)責(zé)從這三個(gè)目標(biāo)端點(diǎn)抓取數(shù)據(jù)。假設(shè),想象前兩個(gè)端點(diǎn)是生產(chǎn)環(huán)境的,另一個(gè)是非生產(chǎn)環(huán)境的,為了以示區(qū)別,我們將其打上兩個(gè)不同的標(biāo)簽。在本示例中,我們將group='production'標(biāo)簽添加到第一個(gè)目標(biāo)組,同時(shí)將group='canary'添加到第二個(gè)目標(biāo)。

scrape_configs: - job_name: ’node’ # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: [’localhost:8080’, ’localhost:8081’] labels: group: ’production’ - targets: [’localhost:8082’] labels: group: ’canary’

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

3.1. 配置

為了查看所有的命令行參數(shù),運(yùn)行如下命令

./prometheus -h

配置文件是YAML格式的,可以使用 --config.file參數(shù)指定

配置文件的主要結(jié)構(gòu)如下:

global: # How frequently to scrape targets by default. [ scrape_interval: <duration> | default = 1m ] # How long until a scrape request times out. [ scrape_timeout: <duration> | default = 10s ] # How frequently to evaluate rules. [ evaluation_interval: <duration> | default = 1m ] # The labels to add to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: [ <labelname>: <labelvalue> ... ] # File to which PromQL queries are logged. # Reloading the configuration will reopen the file. [ query_log_file: <string> ]# Rule files specifies a list of globs. Rules and alerts are read from# all matching files.rule_files: [ - <filepath_glob> ... ]# A list of scrape configurations.scrape_configs: [ - <scrape_config> ... ]# Alerting specifies settings related to the Alertmanager.alerting: alert_relabel_configs: [ - <relabel_config> ... ] alertmanagers: [ - <alertmanager_config> ... ]# Settings related to the remote write feature.remote_write: [ - <remote_write> ... ]# Settings related to the remote read feature.remote_read: [ - <remote_read> ... ]4. 抓取 Spring Boot 應(yīng)用

Prometheus希望抓取或輪詢單個(gè)應(yīng)用程序?qū)嵗垣@取指標(biāo)。 Spring Boot在 /actuator/prometheus 提供了一個(gè)actuator端點(diǎn),以適當(dāng)?shù)母袷教峁㏄rometheus抓取。

為了以Prometheus服務(wù)器可以抓取的格式公開指標(biāo),需要依賴 micrometer-registry-prometheus

<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <version>1.6.4</version></dependency>

下面是一個(gè)示例 prometheus.yml

scrape_configs: - job_name: ’spring’ metrics_path: ’/actuator/prometheus’ static_configs: - targets: [’HOST:PORT’]

接下來,創(chuàng)建一個(gè)項(xiàng)目,名為prometheus-example

pom.xml

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cjs.example</groupId> <artifactId>prometheus-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>prometheus-example</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

application.yml

spring: application: name: prometheus-examplemanagement: endpoints: web: exposure: include: '*' metrics: tags: application: ${spring.application.name}

這句別忘了:management.metrics.tags.application=${spring.application.name}

Spring BootActuator 默認(rèn)的端點(diǎn)很多,詳見

https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/production-ready-features.html

啟動(dòng)項(xiàng)目,瀏覽器訪問/actuator/prometheus 端點(diǎn)

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

配置Prometheus抓取該應(yīng)用

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’. static_configs: - targets: [’localhost:9090’] - job_name: ’springboot-prometheus’ metrics_path: ’/actuator/prometheus’ static_configs: - targets: [’192.168.100.93:8080’]

重啟服務(wù)

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

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

4.1. Grafana

https://grafana.com/docs/

https://grafana.com/tutorials/

下載&解壓

wget https://dl.grafana.com/oss/release/grafana-7.4.3.linux-amd64.tar.gztar -zxvf grafana-7.4.3.linux-amd64.tar.gz

啟動(dòng)

./bin/grafana-server web

瀏覽器訪問 http://localhost:3000

默認(rèn)賬號(hào)是 admin/admin

首次登陸后我們將密碼改成admin1234

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

先配置一個(gè)數(shù)據(jù)源,一會(huì)兒添加儀表盤的時(shí)候要選擇數(shù)據(jù)源的

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

Grafana官方提供了很多模板,我們可以直接使用

首先要找到我們想要的模板

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

比如,我們這里隨便選了一個(gè)模板

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

可以直接將模板JSON文件下載下來導(dǎo)入,也可以直接輸入模板ID加載,這里我們直接輸入模板ID

立竿見影,馬上就看到漂亮的展示界面了

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

我們?cè)偬砑右粋€(gè)DashBoard (ID:12856)

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題

到此這篇關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的文章就介紹到這了,更多相關(guān)Prometheus + Spring Boot 應(yīng)用監(jiān)控內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产精品三级在线观看 | 婷婷色中文网 | 免费a资源 | 免费无遮挡嘿嘿嘿视频动态 | 亚洲精品中文字幕第一区 | 手机看片福利永久 | 老妇女人一级毛片 | 亚洲性图| 精品一区 二区三区免费毛片 | 欧美日韩国产在线人成 | 免费观看爱爱视频 | 国产精品一国产精品免费 | 国产亚洲精品第一综合linode | 手机免费看片网站 | 欧洲美女a视频一级毛片 | 91视频免费入口 | 欧美日韩视频二区三区 | 黄色一级大片视频 | 日韩精品一| 在线观看国产区 | 久久久亚洲欧洲日产国码606 | 国产精品人成在线播放新网站 | 7777sq国产精品 | 一区二区三区四区视频在线观看 | 91视频亚洲| 欧美大片aaaa一级毛片 | 亚洲精品一区二区不卡 | 亚洲无圣光一区二区 | 亚洲第一视频在线播放 | 久久福利国产 | 免费一级特黄欧美大片久久网 | 久青草免费视频手机在线观看 | 精品视频在线观看一区二区三区 | 亚洲黄色一级大片 | 国产欧美在线观看不卡 | 黑人性生活视频 | 午夜精品久久久 | 在线观看91香蕉国产免费 | 91视频啪 | 成人综合在线视频 | 欧美一级毛片一级毛片 |