偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Spring Boot服務(wù)監(jiān)控(Prometheus)

開發(fā) 架構(gòu)
Prometheus是什么,一款開源的優(yōu)秀的時(shí)間序列數(shù)據(jù)庫監(jiān)控軟件。收集各項(xiàng)指標(biāo),用于監(jiān)控系統(tǒng)狀態(tài)。提供強(qiáng)大的PromQL查詢語句,滿足各種個(gè)性化查詢需求。

哲學(xué)

最近看到了一句話:定乎內(nèi)外之分 辯乎榮辱之境。

一個(gè)外國作家也說過:

我生命里的的最大突破之一,就是我不再為別人的看法而擔(dān)憂。此后,我真的能自由的去做我認(rèn)為對(duì)自己最好的事,只有在我們不需要外來的贊許時(shí),才變得自由。

說的都很好。人就是要突破自己,就像許三多,不要在意別人的看法,做自己認(rèn)為有意義的事,今天比昨天好,這不就是希望。

監(jiān)控

思考完一波哲學(xué),開始搞搞軟件上的東西。這篇記錄下監(jiān)控配置相關(guān)的知識(shí)。

為什么需要監(jiān)控系統(tǒng):簡單點(diǎn)說。隨時(shí)掌握系統(tǒng)運(yùn)行情況,保證在你預(yù)期內(nèi)運(yùn)行。

先不扯別的,看兩張效果圖:

1、監(jiān)控Linux服務(wù)器的CPU,內(nèi)存,磁盤等:

2、監(jiān)控Tomcat和jvm:

概念

1、Prometheus是什么,一款開源的優(yōu)秀的時(shí)間序列數(shù)據(jù)庫監(jiān)控軟件。收集各項(xiàng)指標(biāo),用于監(jiān)控系統(tǒng)狀態(tài)。提供強(qiáng)大的PromQL查詢語句,滿足各種個(gè)性化查詢需求。

2、什么是Metrics,Metrics就是監(jiān)控指標(biāo),在外行術(shù)語中,指標(biāo)是數(shù)字度量,時(shí)間序列意味著隨著時(shí)間的推移記錄變化。用戶想要測量的內(nèi)容因應(yīng)用程序而異。對(duì)于web服務(wù)器來說,它可能是請(qǐng)求時(shí)間,對(duì)于數(shù)據(jù)庫,它可能是活動(dòng)連接數(shù)或活動(dòng)查詢數(shù)等。簡單理解,就是你想監(jiān)控的東西,不必過分深究。

3、Grafana又是什么?簡單來說就是圖形化展示工具,和Prometheus天作之合。

安裝配置

1、下載prometheus:

wget https://github.com/prometheus/prometheus/releases/download/v2.35.0/prometheus-2.35.0.linux-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

2、配置prometheus:

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:
# 這里這個(gè)就是自帶的監(jiān)控,監(jiān)控preometheus自己
- targets: ['localhost:9090']

以上就完成了prometheus的下載和配置,非常的簡單。

訪問host:9090就可以看到如下界面:

這就是prometheus的管理頁面。不夠酷炫,接下來下載grafana。

3、下載grafana

sudo yum install grafana
sudo systemctl start grafana-server

這就完事了。驗(yàn)證下,默認(rèn)的管理端是運(yùn)行在3000端口,也就是http://ip:3000,就可以打開如下頁面,賬號(hào)密碼默認(rèn)都是admin。

我們現(xiàn)在有了prometheus和grafana,接下來將grafana連上prometheus。

1、添加數(shù)據(jù)源。

2、連接上prometheus。

3、測試是否連接成功。

這就完成了。截止到現(xiàn)在,最基本prometheus和grafna下載和安裝的操作就完畢了。

exporter

接下來,來監(jiān)控linux的狀態(tài)。這個(gè)也是極其的簡單。

首先下載node_exporter,然后啟動(dòng)

wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz
tar xvfz node_exporter-*.*-amd64.tar.gz
cd node_exporter-*.*-amd64
./node_exporter

再去prometheus修改下配置文件prometheus.yaml,仿照之前的,加上下面的配置。然后重啟prometheus。

- job_name: node
static_configs:
- targets: ['localhost:9100']

這樣已經(jīng)有了監(jiān)控linux運(yùn)行情況的能力了,只是目前還沒有展示出來而已。

接下來加上酷炫的頁面。官網(wǎng)有配置好的,甚至不用自己配置。地址是:

https://grafana.com/grafana/dashboards/。

打開如下,搜索node,選中第一個(gè)。

復(fù)制官方提供的模板ID。然后去grafana導(dǎo)入一下即可。

大功告成了,如下圖所示。

jvm的也是類似的操作,自己可以試驗(yàn)。

自定義監(jiān)控指標(biāo)

以上都是官方提供的exporter。監(jiān)控機(jī)器或者jvm的,如果我們想監(jiān)控自己的業(yè)務(wù)呢?例如想監(jiān)控當(dāng)前有多少請(qǐng)求?每個(gè)請(qǐng)求的性能如何?或者其他一些自定義的監(jiān)控項(xiàng)?

在寫代碼之前,認(rèn)識(shí)幾個(gè)概念:prometheus中的四種指標(biāo)類型。Counter(計(jì)數(shù)器):Counter類型用于增加的值,例如請(qǐng)求計(jì)數(shù)或錯(cuò)誤計(jì)數(shù)。最重要的是,絕對(duì)不能將計(jì)數(shù)器用于可能減小的值。只增不減。

Gauges(儀表板(我自己的翻譯)):儀表類型可用于向下和向上的值,例如當(dāng)前內(nèi)存使用量或隊(duì)列中的項(xiàng)目數(shù),可增可減。

histogram(直方圖):這個(gè)概念比較難以理解。暫時(shí)我們認(rèn)為他就是統(tǒng)計(jì)分位樹的就好。例如你這個(gè)接口99%請(qǐng)求的耗時(shí),TP99。

summaries:本篇不講,感興趣自行查看官網(wǎng)。

這四種類型,都什么時(shí)候使用呢?Counter:

1、你想記錄一個(gè)只上升的值。

2、希望以后能夠查詢?cè)撝档脑鲩L速度(即增長率)。

Guage:

1、想要記錄一個(gè)可以上升或下降的值。

2、你不需要查詢它的增長率。

histogram:分桶計(jì)算,分位計(jì)算,計(jì)算TP99等。

OK,接下來寫代碼。

監(jiān)控Spring Boot應(yīng)用

用java,一般用Spring Boot項(xiàng)目開發(fā),這個(gè)很容易實(shí)現(xiàn),全部都是封裝好的。

從一個(gè)最基本的項(xiàng)目入手,只需要如下的依賴即可。注意到這里除了web模塊,還加了兩個(gè)監(jiān)控模塊。

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

然后寫一個(gè)簡單的controller。

package com.test.promethusmetrics;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 測試Counter
*
* @author fengkai
*/
@RestController
public class CounterController {
private final Counter requestCount;

public CounterController(CollectorRegistry collectorRegistry) {
requestCount = Counter.build()
.name("request_count")
.help("Number of hello requests.")
.register(collectorRegistry);
}
@GetMapping(value = "/hello")
public String hello() {
requestCount.inc();
return "Hi!";
}
}

注意到這里用了counter。這就完成了counter計(jì)數(shù)的代碼部分。

代碼完成后,還需要讓prometheus去拉取我們Spring Boot的監(jiān)控指標(biāo),配置和之前很相似。

添加如下配置,然后重啟prometheus。

- job_name: "spring"
metrics_path: /actuator/prometheus
static_configs:
- targets: ["192.168.181.1:8080"]

我們?cè)跒g覽器上多請(qǐng)求幾次。然后我們?nèi)rafana上配置監(jiān)控面板,首先添加。

然后配置指標(biāo)。

效果圖如下。

以上只是單純的計(jì)數(shù),實(shí)際用途不是很大,其實(shí)更關(guān)心的應(yīng)該是增長率。這又該如何統(tǒng)計(jì)呢?

只需要在外層包裹rate函數(shù)就可以了,具體的原理可以后續(xù)再解釋,這里先用起來。

接下來再試一下使用histogram,統(tǒng)計(jì)下Spring Boot服務(wù)的請(qǐng)求的耗時(shí)情況如何?

代碼部分:

package com.test.promethusmetrics;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Histogram;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import static java.lang.Thread.sleep;
@RestController
public class HistogramController {
private final Histogram requestDuration;
public HistogramController(CollectorRegistry collectorRegistry) {
requestDuration = Histogram.build()
.name("test_wait")
.help("Time for HTTP request.")
.register(collectorRegistry);
}
@GetMapping(value = "/wait")
public String makeMeWait() throws InterruptedException {
Histogram.Timer timer = requestDuration.startTimer();
long sleepDuration = Double.valueOf(Math.floor(Math.random() * 10 * 1000)).longValue();
sleep(sleepDuration);
timer.observeDuration();
return String.format("I kept you waiting for %s ms!", sleepDuration);
}
}

多訪問幾次:localhost:8080/wait然后grafana配置,這里用的是直方圖histogram,計(jì)算的性能。QL的語法本篇不講解,可以參考官網(wǎng)。

效果圖如下:

總結(jié)

現(xiàn)在,我們應(yīng)該清楚地了解prometheus中可以使用的不同監(jiān)控指標(biāo)類型,以及何時(shí)使用它們,如何查詢它們。并且能夠用grafna配置酷炫的監(jiān)控圖標(biāo)。有了這些知識(shí),可以更有效地發(fā)布應(yīng)用程序中的監(jiān)控,并確保它始終按預(yù)期運(yùn)行。

責(zé)任編輯:姜華 來源: 凱哥的Java技術(shù)活
相關(guān)推薦

2022-07-11 09:36:38

SpringJava開發(fā)

2023-12-27 18:05:13

2023-12-28 08:01:17

SpringAPI數(shù)據(jù)

2022-07-28 06:50:52

微服務(wù)業(yè)務(wù)系統(tǒng)

2020-12-01 08:32:12

Spring Boot

2018-10-22 15:34:31

Spring Boo監(jiān)控視化

2022-02-09 20:39:52

Actuator應(yīng)用監(jiān)控

2022-07-29 21:23:54

Grafana微服務(wù)

2020-11-10 09:19:23

Spring BootJava開發(fā)

2022-02-15 10:43:51

數(shù)據(jù)庫Druid連接池

2025-07-28 04:00:00

Spring框架應(yīng)用程序

2021-02-03 12:47:09

Spring Boot應(yīng)用監(jiān)控

2022-07-11 13:43:51

Prometheus監(jiān)控

2021-07-07 05:46:46

運(yùn)維監(jiān)控Prometheus

2020-12-02 10:38:13

Prometheus微服務(wù)架構(gòu)

2023-07-27 08:53:44

2016-11-03 09:59:38

kotlinjavaspring

2020-12-30 08:09:46

運(yùn)維Prometheus 監(jiān)控

2022-12-13 08:01:06

監(jiān)控黑盒集成

2020-11-20 08:15:40

Grafana + P
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)