SpringBoot項目使用Prometheus實時監控系統各項指標


環境:springboot2.3.11 + prometheus1.6.7 + grafana7.5.7


什么是Prometheus

Prometheus 是一個開源的服務監控系統和時間序列數據庫。

SpringBoot項目使用Prometheus實時監控系統各項指標

 

prometheus存儲的是時序數據,即按相同時序(相同名稱和標簽),以時間維度存儲連續的數據的集合。

時序(time series)是由名字(Metric)以及一組key/value標簽定義的,具有相同的名字以及標簽屬於相同時序。

配置依賴

<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>
  </dependency>
</dependencies>

配置文件

spring:
  application:
    name: app-prometheus
---
management:
  server:
    port: 9999
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: '*'

注冊MeterRegistry

@Bean
public MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String name) {
  return (registry) -> registry.config().commonTags("application", name);
}

訪問Prometheus actuator

SpringBoot項目使用Prometheus實時監控系統各項指標

 

Springboot與Prometheus的整合完成。

Prometheus配置安裝

Prometheus下載

SpringBoot項目使用Prometheus實時監控系統各項指標

 

通過如上地址下載自己需要的版本。

配置Prometheus

scrape_configs:
  - job_name: 'app-prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['localhost:9999']

localhost:9999為項目的Actuator訪問地址。

啟動Prometheus

SpringBoot項目使用Prometheus實時監控系統各項指標

 

訪問

SpringBoot項目使用Prometheus實時監控系統各項指標

 

查看監控的應用

SpringBoot項目使用Prometheus實時監控系統各項指標

 

SpringBoot項目使用Prometheus實時監控系統各項指標

 

自定義meter

@Resource private MeterRegistry registry ; private Counter counter ; @PostConstruct public void init() { counter = this.registry.counter("vistor") ; } @GetMapping("/count") public String count() { this.counter.increment() ; return "訪問次數:" + this.counter.count() ; }

先多訪問幾次該接口,通過Prometheus查看

SpringBoot項目使用Prometheus實時監控系統各項指標

 

Grafana安裝配置

下載

SpringBoot項目使用Prometheus實時監控系統各項指標

 

通過上面的地址下載grafana

啟動服務

SpringBoot項目使用Prometheus實時監控系統各項指標

 

默認用戶名密碼:admin/admin

SpringBoot項目使用Prometheus實時監控系統各項指標

 

添加Prometheus數據源

SpringBoot項目使用Prometheus實時監控系統各項指標

 

查看數據

SpringBoot項目使用Prometheus實時監控系統各項指標

 

SpringBoot項目使用Prometheus實時監控系統各項指標

 

這里展示了visitor中的統計信息

監控數據庫連接池

SpringBoot項目使用Prometheus實時監控系統各項指標

 

先在grafana上搜索

SpringBoot項目使用Prometheus實時監控系統各項指標

 

通過id導入

SpringBoot項目使用Prometheus實時監控系統各項指標

 

SpringBoot項目使用Prometheus實時監控系統各項指標

 

SpringBoot項目使用Prometheus實時監控系統各項指標

 

項目中配置hikari數據庫連接池,grafana自動會展示數據庫連接信息

SpringBoot項目使用Prometheus實時監控系統各項指標

 

完畢!!!

給個關注+轉發唄謝謝

公眾:Springboot實戰案例錦集

 
標簽:  springaopspringboot
好文要頂  關注我  收藏該文   
0
0
 
 
 
« 上一篇:  Springboot整合百度開源分布式ID生成器UIDGenerator
posted @  2021-07-10 07:42  FastCoder  閱讀(4)  評論(0)  編輯  收藏  舉報

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM