最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Prometheus監(jiān)控Springboot程序的實現(xiàn)方法

 更新時間:2021年03月16日 10:33:08   作者:csdn_freak_dd  
這篇文章主要介紹了Prometheus監(jiān)控Springboot程序的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 添加依賴

我本次使用的Springboot版本為1.5.12.RELEASE,如果是Springboot2.0+,那么監(jiān)控的配置以及吐出的監(jiān)控指標(biāo)會有所不同。
添加maven依賴,pom文件配置如下:

<dependency>
   <groupId>io.prometheus</groupId>
   <artifactId>simpleclient_spring_boot</artifactId>
   <version>${prometheus.client.version}</version>
  </dependency>
  <dependency>
   <groupId>io.prometheus</groupId>
   <artifactId>simpleclient</artifactId>
   <version>${prometheus.client.version}</version>
  </dependency>
  <dependency>
   <groupId>io.prometheus</groupId>
   <artifactId>simpleclient_hotspot</artifactId>
   <version>${prometheus.client.version}</version>
  </dependency>

其中Prometheus的版本號為:

<prometheus.client.version>0.5.0</prometheus.client.version>

2. 修改配置文件

修改application.properties配置文件,添加如下內(nèi)容:

server.port=8080

# 啟用基礎(chǔ)認證
security.basic.enabled = false

# 安全路徑列表,逗號分隔,此處只針對/admin路徑進行認證
security.basic.path = /admin

# 認證使用的用戶名
security.user.name = admin

# 認證使用的密碼。 默認情況下,啟動時會記錄隨機密碼。
security.user.password = 123456

# 可以訪問管理端點的用戶角色列表,逗號分隔
management.security.roles = SUPERUSER

# actuator暴露接口使用的端口,為了和api接口使用的端口進行分離
management.port = 8099

# actuator暴露接口的前綴
management.context-path = /admin

# actuator是否需要安全保證
management.security.enabled = false

# actuator的metrics接口是否需要安全保證
endpoints.metrics.sensitive = false

# actuator的metrics接口是否開啟
endpoints.metrics.enabled=true

# actuator的health接口是否需要安全保證
endpoints.health.sensitive=false

# actuator的health接口是否開啟
endpoints.health.enabled=true

application.yml 配置如下:

# actuator是否需要安全保證
management.security.enabled: false
endpoints:
 metrics:
 # actuator的metrics接口是否需要安全保證
 sensitive: false
 # actuator的metrics接口是否開啟
 enabled: true
 health:
 # actuator的health接口是否需要安全保證
 sensitive: false
 # actuator的health接口是否開啟
 enabled: true

3. 啟用Prometheus監(jiān)控

在Springboot啟動類上添加注解@EnablePrometheusEndpoint,同時使用simpleclient_hotspot中提供的DefaultExporter該Exporter會在metrics endpoint中放回當(dāng)前應(yīng)用JVM的相關(guān)信息

@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class CaseApplication implements CommandLineRunner {

 public static void main(String[] args) {
  SpringApplication.run(CaseApplication.class, args);
 }

 @Override
 public void run(String... strings) throws Exception {
  DefaultExports.initialize();
 }

}

4. 監(jiān)控埋點

4.1 新建攔截器

建立一個攔截器,用來攔截URL。

public class PrometheusMetricsInterceptor implements HandlerInterceptor {

 private Histogram.Timer histogramRequestTimer;
 private Histogram.Timer nacosTimer;
 private Histogram.Child nacosChild;

 static final Histogram requestLatencyHistogram = Histogram.build().labelNames("path", "method", "code")
   .name("io_namespace_http_requests_latency_seconds_histogram").help("Request latency in seconds.")
   .register();

 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  System.out.println("-------Histogram--------");
  histogramRequestTimer = requestLatencyHistogram
    .labels(request.getRequestURI(), request.getMethod(), String.valueOf(response.getStatus()))
    .startTimer();
  nacosTimer = MetricsMonitor.getConfigRequestMonitor(request.getMethod(), request.getRequestURI(), String.valueOf(response.getStatus()));
  nacosChild = MetricsMonitor.getNamingRequestMonitor(request.getMethod(), request.getRequestURI(), String.valueOf(response.getStatus()));
  return true;
 }

 @Override
 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

 }

 @Override
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  histogramRequestTimer.observeDuration();
  nacosTimer.observeDuration();
  nacosChild.startTimer();
 }
}

4.2 注冊攔截器

新建攔截器后需要注冊到服務(wù)中才可以攔截URL。

@Configuration
public class WebServletContextConfiguration extends WebMvcConfigurationSupport {

 @Override
 public void addInterceptors(InterceptorRegistry registry) {

  registry.addInterceptor(getInterceptor()).addPathPatterns("/**");
  super.addInterceptors(registry);
 }

 @Bean
 public HandlerInterceptor getInterceptor() {
  return new PrometheusMetricsInterceptor();
 }

}

5. 驗證監(jiān)控

啟動應(yīng)用程序,訪問地址
http://localhost:8099/admin/prometheus,驗證是否存在監(jiān)控指標(biāo)。

監(jiān)控結(jié)果

6. 指標(biāo)類型

普羅米修斯客戶端庫提供了四種核心度量類型。目前,它們只在客戶端庫(為了使api能夠根據(jù)特定類型的使用而定制)和wire協(xié)議中有所區(qū)別。Prometheus服務(wù)器還沒有利用類型信息,并將所有數(shù)據(jù)壓縮成無類型的時間序列。這種情況在未來可能會改變。

6.1 Counter

計數(shù)器是一個累積度量,它表示一個單調(diào)遞增的計數(shù)器,其值在重新啟動時只能遞增或重置為零。例如,您可以使用計數(shù)器來表示服務(wù)的請求、完成的任務(wù)或錯誤的數(shù)量。
不要使用計數(shù)器來暴露可能降低的值。例如,不要為當(dāng)前正在運行的進程的數(shù)量使用計數(shù)器;而是使用量規(guī)。
示例代碼:

import io.prometheus.client.Counter;
class YourClass {
 static final Counter requests = Counter.build()
  .name("requests_total").help("Total requests.").register();

 void processRequest() {
 requests.inc();
 // Your code here.
 }
}

6.2 Gauge

量規(guī)是一個度量單位,它表示一個可以任意上下移動的數(shù)值。
壓力表通常用于測量溫度或當(dāng)前內(nèi)存使用情況等測量值,但也用于“計數(shù)”,比如并發(fā)請求的數(shù)量。
示例代碼:

class YourClass {
 static final Gauge inprogressRequests = Gauge.build()
  .name("inprogress_requests").help("Inprogress requests.").register();

 void processRequest() {
 inprogressRequests.inc();
 // Your code here.
 inprogressRequests.dec();
 }
}

6.3 Histogram

直方圖對觀察結(jié)果(通常是請求持續(xù)時間或響應(yīng)大小之類的東西)進行采樣,并在可配置的桶中計數(shù)。它還提供所有觀測值的和。
示例代碼:

class YourClass {
 static final Histogram requestLatency = Histogram.build()
  .name("requests_latency_seconds").help("Request latency in seconds.").register();

 void processRequest(Request req) {
 Histogram.Timer requestTimer = requestLatency.startTimer();
 try {
  // Your code here.
 } finally {
  requestTimer.observeDuration();
 }
 }
}

6.4 Summary

與柱狀圖類似,摘要對觀察結(jié)果進行采樣(通常是請求持續(xù)時間和響應(yīng)大小之類的內(nèi)容)。雖然它還提供了觀察值的總數(shù)和所有觀察值的總和,但它計算了一個滑動時間窗口上的可配置分位數(shù)。

class YourClass {
 static final Summary receivedBytes = Summary.build()
  .name("requests_size_bytes").help("Request size in bytes.").register();
 static final Summary requestLatency = Summary.build()
  .name("requests_latency_seconds").help("Request latency in seconds.").register();

 void processRequest(Request req) {
 Summary.Timer requestTimer = requestLatency.startTimer();
 try {
  // Your code here.
 } finally {
  receivedBytes.observe(req.size());
  requestTimer.observeDuration();
 }
 }
}

6.5 自定義Collector

有時不可能直接測試代碼,因為它不在您的控制范圍內(nèi)。這要求您代理來自其他系統(tǒng)的指標(biāo)。
為此,您需要創(chuàng)建一個自定義收集器(需要將其注冊為普通度量)。

class YourCustomCollector extends Collector {
 List<MetricFamilySamples> collect() {
 List<MetricFamilySamples> mfs = new ArrayList<MetricFamilySamples>();
 // With no labels.
 mfs.add(new GaugeMetricFamily("my_gauge", "help", 42));
 // With labels
 GaugeMetricFamily labeledGauge = new GaugeMetricFamily("my_other_gauge", "help", Arrays.asList("labelname"));
 labeledGauge.addMetric(Arrays.asList("foo"), 4);
 labeledGauge.addMetric(Arrays.asList("bar"), 5);
 mfs.add(labeledGauge);
 return mfs;
 }
}

// Registration
static final YourCustomCollector requests = new YourCustomCollector().register()

7. 安裝配置Prometheus

7.1 安裝配置

下載安裝包

wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz

解壓文件

tar -zxvf prometheus-2.12.0.linux-amd64.tar.gz

修改配置文件prometheus.yml采集Nacos metrics數(shù)據(jù)。配置監(jiān)控的job以及目標(biāo)服務(wù)器,每一個目標(biāo)服務(wù)器都是一個實例。

cd prometheus-*

Prometheus配置文件修改

后臺啟動Prometheus服務(wù),并出到日志。

./prometheus --config.file=prometheus.yml > prometheus.log 2>&1 &

通過訪問http://{ip}:9090/graph可以看到prometheus的采集數(shù)據(jù),在搜索欄搜索監(jiān)控指標(biāo),例如:nacos_monitor可以搜索到Nacos數(shù)據(jù)說明采集數(shù)據(jù)成功

數(shù)據(jù)采集結(jié)果

在查詢條件框中輸入表達式,進行統(tǒng)計。例如:

sum(rate(nacos_client_request_seconds_count{url=~'/dialog/slu/nlp/parser', instance=~'39.97.161.102:30315|39.97.161.102:30316'}[1m])) by (method,url,instance)

結(jié)果如下圖:

查詢圖表結(jié)果

8. 安裝配置Grafana

 8.1 安裝配置

安裝grafana,下載安裝包

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

修改端口配置,復(fù)制一個配置文件,后續(xù)修改基于該自定義配置文件修改,不需要修改原始文件。

cd grafana-6.5.2/conf
cp sample.ini custom.ini
vi custom.ini

可以在該配置文件中修改端口號

在這里插入圖片描述

訪問grafana: http://{ip}:3000,用戶名密碼默認為:admin/admin。

在這里插入圖片描述

登錄時提示修改默認密碼,如果不想修改可以跳過。

8.2 配置數(shù)據(jù)源

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

8.3 配置監(jiān)控面板

監(jiān)控面板可以自己配置,也可以通過導(dǎo)入json文件來進行修改,推薦使用配置好的json文件,修改起來會非常方便。

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

修改后的展示效果如圖所示:

在這里插入圖片描述

注:此處grafana的模板文件是從別處下載的,可以根據(jù)需要導(dǎo)入自己的模板文件。

9. 參考文獻

SpringBoot 應(yīng)用監(jiān)控踩坑集錦

prometheus docs

到此這篇關(guān)于Prometheus監(jiān)控Springboot程序的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)Prometheus監(jiān)控Springboot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JVM GC 垃圾收集梳理總結(jié)

    JVM GC 垃圾收集梳理總結(jié)

    這篇文章主要介紹了JVM GC 垃圾收集梳理總結(jié),GC是一種自動的存儲管理機制。當(dāng)一些被占用的內(nèi)存不再需要時,就應(yīng)該予以釋放,這種存儲資源管理,稱為垃圾回收
    2022-07-07
  • IntelliJ IDEA 中g(shù)it的使用圖文教程

    IntelliJ IDEA 中g(shù)it的使用圖文教程

    本文通過圖文并茂的形式給大家介紹了IntelliJ IDEA 中g(shù)it的使用,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2018-02-02
  • Spring Web MVC和Hibernate的集成配置詳解

    Spring Web MVC和Hibernate的集成配置詳解

    這篇文章主要介紹了Spring Web MVC和Hibernate的集成配置詳解,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • python 與HFSS聯(lián)合仿真的教程講解

    python 與HFSS聯(lián)合仿真的教程講解

    這篇文章主要介紹了python 與HFSS聯(lián)合仿真的教程講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 詳解SpringBoot中的tomcat優(yōu)化和修改

    詳解SpringBoot中的tomcat優(yōu)化和修改

    這篇文章主要介紹了詳解SpringBoot中的tomcat優(yōu)化和修改,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • SpringBoot 的參數(shù)配置示例全解析

    SpringBoot 的參數(shù)配置示例全解析

    Spring Boot的參數(shù)配置系統(tǒng)通過application.properties和application.yml文件實現(xiàn),支持多種外部配置方式,本文介紹SpringBoot 的參數(shù)配置示例全解析,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • Java打包工具jar包詳解

    Java打包工具jar包詳解

    這篇文章主要介紹了Java打包工具jar包詳解,在本例中我們引入一個叫jaxen.jar的庫,并將所有以”org.jaxen”開頭的類重命名以”org.example.jaxen”開頭,具體實例代碼跟隨小編一起看看吧
    2021-10-10
  • java數(shù)組實現(xiàn)隊列及環(huán)形隊列實現(xiàn)過程解析

    java數(shù)組實現(xiàn)隊列及環(huán)形隊列實現(xiàn)過程解析

    這篇文章主要介紹了java數(shù)組實現(xiàn)隊列及環(huán)形隊列實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot整合thymeleaf 報錯的解決方案

    SpringBoot整合thymeleaf 報錯的解決方案

    這篇文章主要介紹了SpringBoot整合thymeleaf 報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java為什么會出現(xiàn)精度丟失這種現(xiàn)象你知道嗎

    java為什么會出現(xiàn)精度丟失這種現(xiàn)象你知道嗎

    這篇文章主要介紹了Java精度丟失的問題,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-08-08

最新評論

微博| 三门县| 比如县| 武功县| 四川省| 大宁县| 曲周县| 罗城| 裕民县| 乐亭县| 容城县| 白沙| 天柱县| 绥宁县| 濮阳县| 宁安市| 成安县| 清水河县| 宣城市| 和田市| 青岛市| 满洲里市| 礼泉县| 九台市| 红原县| 平南县| 突泉县| 沁阳市| 筠连县| 嘉义县| 修水县| 望奎县| 广东省| 历史| 隆化县| 乡宁县| 通榆县| 淳化县| 麻城市| 嘉定区| 台中县|