springboot2+exporter+prometheus+grafana搭建监控体系

项目中需要监控系统指标(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看配置的监控是否正确:

springboot2+exporter+prometheus+grafana搭建监控体系

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

原文 

https://blog.51cto.com/thinklili/2360858

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » springboot2+exporter+prometheus+grafana搭建监控体系

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址