項目中須要監控系統指標(JVM,CPU,IO,MySQL等),這時候能夠使用Prometheus來作。若是是須要監控日誌系統,能夠使用ELK stack。監控=日誌(ELK)+指標(Prometheus)。node
step1 編寫springboot2程序mysql
<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安裝:git
在https://github.com/prometheus/node_exporter/releases找到最新的壓縮包,而後wget,解壓tar -zxvf到本地。
執行:./mysqld_exportergithub
step3 mysql_exporter安裝web
在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_exporterspring
先確保mysql啓動和有my.cnf文件,而後執行:./mysqld_exportersql
step4 prometheus安裝和配置springboot
下載最新版的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 參考:jvm
# 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 ide
訪問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相關指數