1. 程式人生 > >springboot2+exporter+prometheus+grafana搭建監控體系

springboot2+exporter+prometheus+grafana搭建監控體系

com 執行 下載 reat RoCE image art ons ica

項目中需要監控系統指標(JVM,CPU,IO,MySQL等),這時候可以使用Prometheus來做。如果是需要監控日誌系統,可以使用ELK stack。監控=日誌(ELK)+指標(Prometheus)。

step1 編寫springboot2程序

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

step2 node_exporter安裝:

在https://github.com/prometheus/node_exporter/releases找到最新的壓縮包,然後wget,解壓tar -zxvf到本地。
執行:./mysqld_exporter

step3 mysql_exporter安裝

在https://github.com/prometheus/mysqld_exporter找到最新的壓縮包,然後wget,解壓tar -zxvf到本地。
詳細安裝參考官方文檔。
1 mysql創建新用戶:
CREATE USER ‘exporter‘@‘localhost‘ IDENTIFIED BY ‘XXXXXXXX‘ WITH MAX_USER_CONNECTIONS 3;

GRANT PROCESS, REPLICATION CLIENT, SELECT ON . TO ‘exporter‘@‘localhost‘;
2設置環境變量:
export DATA_SOURCE_NAME=‘user:password@(hostname:3306)/‘
3 啟動
./mysqld_exporter

先確保mysql啟動和有my.cnf文件,然後執行:./mysqld_exporter

step4 prometheus安裝和配置

下載最新版的prometheus安裝和配置:wget https://github.com/prometheus/prometheus/releases/download/v2.8.0-rc.0/prometheus-2.8.0-rc.0.darwin-amd64.tar.gz

解壓到本地:
先配置prometheus.yml文件,需要定義一系列job和targets以連接到step1-3中的組件。
prometheus.yml 參考:

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global ‘evaluation_interval‘.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"

# 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‘
    # metrics_path defaults to ‘/metrics‘
    # scheme defaults to ‘http‘.
    static_configs:
      - targets: [‘localhost:9090‘]
  - job_name: ‘node‘
    static_configs:
      - targets: [‘localhost:9100‘]

  - job_name: ‘spring-boot‘
    metrics_path: /prometheus
    static_configs:
      - targets: [‘localhost:8080‘]

  - job_name: ‘mysql-monitor‘
    static_configs:
      - targets: [‘localhost:9104‘]

編寫好之後啟動prometheus
./prometheus --config.file=prometheus.yml

訪問localhost:9090,查看targets看配置的監控是否正確:
技術分享圖片

step5 grafana安裝

wget https://dl.grafana.com/oss/release/grafana-6.0.1.darwin-amd64.tar.gz,
tar -zxvf grafana-6.0.1.darwin-amd64.tar.gz
./bin/grafana-server web

step6 自定義panel:jvm,mysql,cpu相關指數

完整的項目鏈接:https://github.com/xiaozhiliaoo/monitor-base

springboot2+exporter+prometheus+grafana搭建監控體系