微服務監控和報警(二)-Prometheus簡介及環境搭建


1、Prometheus簡介

  Prometheus是具有活躍生態系統的開源系統監視和警報工具包。下圖是Prometheus的體系結構及其某些生態系統組件。最核心的位置就是Prometheus server,主要的作用就是根據我們的配置去用於收集和存儲時間序列數據。Service discovery服務的發現,通過Service discovery,Prometheus server就會知道去哪里采集數據,有兩種方式,一種是靜態的,通過文件去配;另外一種是動態的,可以通過zookeeper或者其他的配置中心,當里面的數據變化時,去不同的地方抓取數據。Jobs/exporters,一般是我們的應用提供的,供Prometheus server抓取數據,這里是拉模式,好處是,對於我們的應用來說,不需要知道Prometheus的服務在哪,只需要暴漏我們的數據就可以了。Pushgateway,是用來支持推模式的,因為有些時候,我們的一些數據並不是一直存在的,比如說定時任務的數據,我們把短活的數據推送到Pushgateway,供Prometheus server從Pushgateway拉取數據。到這里數據采集的組件一件介紹完畢了。數據采集來了之后都放到Prometheus server中,通過HTTP server將數據暴漏出來供前端的一些應用通過PromQL來查詢使用,進行數據的可視化和導出,推薦使用的組件時Grafana。Alertmanager來做告警,告警的方式有很多種,email、微信、釘釘或者自己寫的接口等,可以對Prometheus server中的時間序列數據定制一些規則,出發了規則會推送到Alertmanager,但是它並不會立刻告警,而是會評估幾次,防止誤報。

2、Prometheus環境搭建

2.1、使用docker安裝Prometheus文件結構

  2.1.1、docker-compose.yml

version: "3"
services:
  prometheus:
    image: prom/prometheus:v2.4.3
    container_name: 'prometheus'
    volumes:
    - ./prometheus/:/etc/prometheus/
    ports:
    - '8999:9090'

  2.1.2、prometheus.yml

#全局配置
global:
  #間隔多久去拉取一次數據
  scrape_interval:   15s

# 要拉取的目標是哪里
scrape_configs:
# 我們的springboot項目
- job_name:       'springboot-app'
  #間隔10s拉取一次數據,覆蓋全局配置
  scrape_interval: 10s
  #請求的路徑
  metrics_path: '/actuator/prometheus'

  static_configs:
  #去哪里抓取,因為我們的項目跑在本地電腦,所以配置運行docker的宿主機
  - targets: ['host.docker.internal:9080']
    #把抓取來的數據,添加一個標簽
    labels:
      application: 'springboot-app'

#prometheus本機的監控
- job_name: 'prometheus'

  scrape_interval: 5s

  static_configs:
  - targets: ['localhost:9090']

  2.1.3、啟動命令行,進入monitoring目錄, 執行 docker-compose -f docker-compose.yml up 命令

   2.1.4、訪問http://127.0.0.1:8999/ 可以訪問,容器中部署的prometheus,通過Status->Targets可以看到我們配置的兩個數據采集目標。Endpoint,代表數據端點、State代表當前狀態,Labels代表標簽,Last Scrape代表距上次拉去時間,Error代表錯誤信息。

3、SpringBoot整合Prometheus,我們以order服務為例

3.1、添加SpringBoot Actuator 監控端點依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

3.2、添加micrometer-registry-prometheus依賴,在actuator中增加prometheus端點

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

3.3、application.yml配置對外暴漏端點,這里我們控制只暴露三個

3.4、資源服務配置端點請求,不用身份驗證

package cn.caofanqi.security.config;

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

/**
 * 資源服務器配置
 *
 * @author caofanqi
 * @date 2020/2/14 14:07
 */
@Configuration
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().requestMatchers(EndpointRequest.toAnyEndpoint()).permitAll()
                .anyRequest().authenticated();
    }
}

3.5、啟動order服務,刷新http://127.0.0.1:8999/targets 頁面,可以看到我們配置的端點,狀態為up了

 3.6、我們可以通過http://order.caofanqi.cn:9080/actuator/prometheus,看到服務為Prometheus提供的數據,都是一個數據名稱跟着一個數字,有的數據名稱帶{},里面是這個數據名稱的標簽。

 3.7、我們可以通過Prometheus的Graph來查看這些數據,這里{}中的標簽,比我們項目中的多,那是因為它把prometheus.yml配置文件中一些配置也添加成標簽了,job_name -> job、static_configs.labels.application -> application、static_configs.targets -> instance ,有利於我們對數據進行過濾。

 3.8、還可以通過標簽進行過濾、通過Graph看圖分析

但是它自己提供的這個界面屬實不太好看,下節我們用grafana來代替

 

 項目源碼:https://github.com/caofanqi/study-security/tree/dev-prometheus1


免責聲明!

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



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