springboot集成promutheus监控

promutheus 与nginx 对比

优势

  • 安装简单
  • 时序数据库,对于监控场景,以时间维度查询性能高
  • 告警条件多样化

多维度数据模型。
灵活的查询语言。
不依赖分布式存储,单个服务器节点是自主的。
通过基于HTTP的pull方式采集时序数据。
可以通过中间网关进行时序列数据推送。
通过服务发现或者静态配置来发现目标服务对象。
支持多种多样的图表和界面展示,比如Grafana等。

promutheus概念与配置

中文文档

时间序列:

  • 指标名称
  • 标签
  • 时间戳
  • 样本值

表达式:


<metric name>{<label name>=<label value>, ...}

例如:


api_http_requests_total{method="POST", handler="/messages"}

指标类型
计数器(counter),从开始监控,到指定时间点,单调递增,只增不减;
仪表盘(gauge),瞬时值,例如温度;
直方图(Histogram),按时间区间统计;
汇总(Summary)

docker 方式启动


docker run -p 9090:9090 -v /prometheus-data \
prom/prometheus --config.file=/prometheus-data/prometheus.yml

springboot项目暴露监控信息

让 springboot 输出 prometheus 支持的监控信息格式;

添加依赖:

image

开启监控

image

image

在promutheus 的配置文件 /etc/alertmanager/alertmanager.yml 中配置要监控的应用

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']

  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prod_k12'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: '/prometheus'
    static_configs:
    - targets: ['ip:port']

  - job_name: 'prod_ngx'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: '/status/format/prometheus'
    static_configs:
    - targets: ['ip:port']

promutheus 通过 eureka 动态发现服务并进行监控

在eureka server 中加入依赖

<!-- prometheus 服务发现用 -->
       <dependency>
           <groupId>at.twinformatics</groupId>
           <artifactId>eureka-consul-adapter</artifactId>
           <version>1.0.1</version>
       </dependency>
       

配置 promutheus.yml

image

grafana配置

在官网 搜索 模板 Spring Boot Statistics 添加

alertmanager 邮件告警配置

prometheus.yml 中加入告警配置

image

告警规则文件 alert_rules.yml 内容

image

alertmanager.yml 文件 内容:

image

示例项目:
https://ape2zhangjian.coding.net/p/samplecode/d/samplecode/git/tree/master/java/SpringBootPrometheusSample/README.md

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 夜莺2517阅读 127,801评论 1 9
  • 版本:ios 1.2.1 亮点: 1.app角标可以实时更新天气温度或选择空气质量,建议处女座就不要选了,不然老想...
    我就是沉沉阅读 11,843评论 1 6
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 12,728评论 28 53
  • 兔子虽然是枚小硕 但学校的硕士四人寝不够 就被分到了博士楼里 两人一间 在学校的最西边 靠山 兔子的室友身体不好 ...
    待业的兔子阅读 7,508评论 2 9