应用监控以及告警实现

前言

一个Java应用 可以不优秀,但是一定不能没有监控方案。否则极大影响排查线上问题的效 以及系统故障的及时告警 。试想 核心应用挂了一个 但是没有配置告警 理想情况几个小时 被自己人发现了 但是万一自己人也没看到或者没关注 那难道让服务一直挂下去么 ?
客户:**服务

1.简介

目前市面上监控有很多种 在这里我们选用的是Promethus

2.Promethus 介绍

Prometheus是一个开源的系统监控和警报工具包,最初由SoundCloud构建。自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区。它现在是一个独立的开源项目,独立于任何公司进行维护。为了强调这一点,并澄清项目的治理结构,Prometheus于2016年加入了云原生计算基金会,成为Kubernetes之后的第二个托管项目。

普罗米修斯收集和存储它的指标作为时间序列数据,也就是说,指标信息与它被记录的时间戳一起存储,以及称为标签的可选键值对。

3. Promethus特性

普罗米修斯的主要特点是:

  • 一个多维数据模型,其时间序列数据由度量名称和键/值对标识
  • PromQL是一种灵活的查询语言,可以利用这个维度
  • 不依赖分布式存储;单个服务器节点是自治的
  • 时间序列收集通过HTTP上的拉模型进行
  • 通过中间网关支持推送时间序列
  • 目标是通过服务发现或静态配置发现的
  • 支持多种模式的图形化和仪表板

4. Promethus组件

Prometheus生态包括了很多组件,它们中的一些是可选的:

  • Prometheus主服务器,用于抓取和存储时间序列数据
  • 用于检测应用程序代码的客户端库
  • 用于支持短声明周期的push网关
  • 针对HAProxy,StatsD,Graphite等服务的特定exporters
  • 警告管理器
  • 各种支持工具

多数Prometheus组件是Go语言写的,这使得这些组件很容易编译和部署。

5. Promethus架构

下面这张图说明了Prometheus的整体架构,以及生态中的一些组件作用:

在这里插入图片描述
建议这个图 反复多看几遍 这样后边会有个整理的了解

6. Promethus适用性

普罗米修斯可以很好地记录任何纯数字时间序列。它既适合于以机器为中心的监视,也适合于高度动态的面向服务的体系结构的监视。在微服务的世界里,它对多维数据收集和查询的支持是一个特别的优势。

Prometheus是为可靠性而设计的,它是您在停机期间使用的系统,允许您快速诊断问题。每个Prometheus服务器都是独立的,不依赖于网络存储或其他远程服务。当基础设施的其他部分损坏时,您可以依赖它,并且不需要设置广泛的基础设施来使用它。

7. 环境搭建

  • 下载并解压 https://prometheus.io/download/
  • 配置 (详情见https://prometheus.io/docs/prometheus/latest/configuration/configuration/ 无特殊情况,首次配置 直接使用默认的进行修改即可)

Prometheus配置是YAML。 示例配置如下

global:scrape_interval:     15sevaluation_interval: 15srule_files:# - "first.rules"# - "second.rules"scrape_configs:- job_name: prometheusstatic_configs:- targets: ['localhost:9090']

全局块控制Prometheus服务器的全局配置。 我们有两种选择。 第一个是scrape_interval,它控制Prometheus抓取目标的频率。 您可以为单个目标重写此值。 在这种例子下,全局设置是每15s抓取一次。 evaluation_interval选项控制Prometheus评估规则的频率。 Prometheus使用规则创建新的时间序列并生成警报。

rule_files块指定我们希望Prometheus服务器加载的任何规则的位置。 现在我们没有规则。

最后一个块scrape_configs控制Prometheus监视的资源。 由于Prometheus还将自己的数据公开为HTTP端点,因此它可以抓取并监控自身的健康状况。 在默认配置中,有一个名为prometheus的作业,它会抓取Prometheus服务器公开的时间序列数据。 该作业包含一个静态配置的目标,即端口9090上的localhost。Prometheus希望指标在/metrics路径上的目标上可用。 所以这个默认的工作是通过URL抓取:http//localhost:9090/metrics。

返回的时间序列数据将详细说明Prometheus服务器的状态和性能。

  • 启动Prometheus使用刚才的配置文件 启动
./prometheus --config.file=prometheus.yml

8. Prometheus其他操作

  • 使用表达式浏览器
    让我们试着看一下Prometheus收集的关于自己的一些数据。 要使用Prometheus的内置表达式浏览器,请导航到http://localhost:9090/graph
    在这里插入图片描述
    正如您可以从http://localhost:9090/metrics收集的那样,Prometheus导出的一个度量标准称为promhttp_metric_handler_requests_total(Prometheus服务器已服务的/ metrics请求的总数)。
    继续并将其输入表达式输入框 并点击执行 :
    promhttp_metric_handler_requests_total
    在这里插入图片描述

这应该返回许多不同的时间序列(以及为每个记录的最新值),所有时间序列都使用度量标准名称promhttp_metric_handler_requests_total,但具有不同的标签。 这些标签指定不同的请求状态。

如果我们只对导致HTTP代码200的请求感兴趣,我们可以使用此查询来检索该信息:
promhttp_metric_handler_requests_total{code=“200”}
效果图如下
在这里插入图片描述
要计算返回的时间序列总数,您可以写:
count(promhttp_metric_handler_requests_total)
效果图如下
在这里插入图片描述

有关表达式语言的更多信息,请参阅表达式语言文档。

  • 适用图表接口
    要绘制表达式图表,请导航到http//localhost:9090/graph graph并使用“图表”选项卡。

例如,输入以下表达式来绘制在自我抓取的Prometheus中发生的返回状态代码200的每秒HTTP请求率:

rate(promhttp_metric_handler_requests_total{code=“200”}[1m])

您可以尝试图形范围参数和其他设置。
在这里插入图片描述

  • 监控其他目标
    仅从Prometheus那里收集指标并不能很好地反映Prometheus的能力。 为了更好地了解Prometheus可以做什么,我们建议您浏览有关其他exporter的文档。 使用node exporter指南监控Linux或macOS主机指标是一个很好的起点。 另外也可以使用官方/第三方的 其他的 exporter https://prometheus.io/docs/instrumenting/exporters/

(Exporter)导出器
导出器是暴露Prometheus度量指标的二进制文件,通常将非Prometheus数据格式转化为Prometheus支持的数据处理格式

以为到这里就结束了么 no~~~

如上的图形化操作界面 怎么能满足我们的需求 不能每次搜索都得用自己手动搜吧 接下来是 图形化的界面 Grafana 介绍

9. 图形化界面 Grafana

9.1什么是Grafana?

Grafana允许您查询、可视化、提醒和理解您的指标,无论它们存储在哪里。创建、探索并与团队共享漂亮的仪表盘,培养数据驱动的文化。

9.2 环境搭建

  • 下载 curl -O https://dl.grafana.com/oss/release/grafana-7.1.5.darwin-amd64.tar.gz
  • 解压
  • 启动 ./bin/grafana-server web
  • 配置修改(可选 https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/ )

9.3 首次配置

启动成功后 浏览器访问localhost:3000(默认端口 如果修改了 请替换)
打开首先会进入登录页面 让输入账号密码 admin/admin
在这里插入图片描述
登录时 会让修改默认的密码 看自己选择
登录成功后 会进入首页
在这里插入图片描述
然后 选择 配置
在这里插入图片描述
接下来我们会进行添加 一个数据源
在这里插入图片描述

数据源这里 这里我们选择 promethus

在这里插入图片描述
填入 promethus 的url 默认localhost:9090
在这里插入图片描述
添加完 选择Save&test

9.4 配置 dashboard

配置好数据源之后,我们就可以配置对应的监控信息了,常见的配置监控已经有对应的模板了,就不需要我们一个一个地去配置了。(如果不满足的话,那还是得自己去配)
因为需要配置数据暴露 然后 交给 promethus 最后 Grafana 进行展示
所以分为四步执行

  • 下载 记得选择与自己系统匹配的

首先我们需要下载node_exporter 用来采集服务器的数据 官方下载页面 https://prometheus.io/download/ 选择对应的操作系统以及架构 这里我们使用的时darwin amd64
在这里插入图片描述
选择 node_exporter
在这里插入图片描述

  • 启动
    下载完成 解压后进行启动 进行解压目录 直接运行 ./node_exporter 即可完成启动

  • 配置promethus 采集该数据
    添加 job_name node 以及 targets localhost:9100 (node_exporter启动的默认端口为9100)

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"]#node 是需要新加的- job_name: nodestatic_configs:- targets: ['localhost:9100'] 

配置修改完成 进行启动 ./prometheus --config.file=prometheus.yml

  • 然后直接import对应的模板,相关的模板可以在https://grafana.com/grafana/dashboards/ 这里查到。

选择dashboard 然后导入
在这里插入图片描述
服务器的监控直接选用8913 保存后即可看到 相关界面
在这里插入图片描述
界面效果图如下
在这里插入图片描述
tips: 查看promethus 采集的到端点 http://localhost:9090/targets 9090为promethus 默认的端口 看到都是自己所配置端点的状态为up,那就说明正常。

9.5 监控Java项目

  • maven项目添加依赖
<!--actuator 监控-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--适配prometheus-->
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
  • 对外暴露相关端点
#这里为了方面 对外暴露了所有的监控数据  生产环境切记不要这么干
management.endpoint.health.show-details=always
management.endpoint.metrics.enabled=true
management.endpoint.prometheus.enabled=true
management.endpoints.web.exposure.include=*
management.metrics.export.prometheus.enabled=true
  • 配置promethus采集数据
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: nodestatic_configs:- targets: ['localhost:9100'] - job_name: gateway-servicemetrics_path: '/actuator/prometheus'static_configs:#9091为启动应用端口号- targets: ['localhost:9091']          
  • 重新启动 promethus
  • dashboard 添加 JVM监控和Springboot监控
    在dashboard 添加 JVM监控和Springboot监控 这里选择4701模板的JVM监控和12900模板的SpringBoot监控
    分别看下效果
    JVM监控
    在这里插入图片描述
    springboot 监控
    在这里插入图片描述

配置 模板893来配置监控docker的信息:
在这里插入图片描述

难道又要结束了么 当然no~~~ 接下来进入到告警的环节

10.告警

这里使用告警 工具是 AlterManager

10.1 AlterManager介绍

Alertmanager处理客户端应用程序(如Prometheus服务器)发送的警报。它负责重复数据删除、分组,并将它们路由到正确的接收器集成,如电子邮件、PagerDuty或OpsGenie。它还负责静音和抑制警报。

10.2 告警流程介绍

在这里插入图片描述

告警流程大概有四大步骤

  • 在promethus配置告警信息 以及 alterManager
  • 告警信息触发.
  • promethus推送告警信息到alterManager
  • alterManager接收到告警信息并根据不同方式传输到指定位置(邮件 etc 钉钉 等等)

10.3 AlterManager下载安装 配置

  • 下载安装Alertmanager 地址 https://prometheus.io/download/ 选择操作系统和架构进行下载
    在这里插入图片描述
  • 解压
  • 配置修改(更为详细的文档 https://prometheus.io/docs/alerting/latest/configuration/)
route:group_by: ['test']group_wait: 30sgroup_interval: 5mrepeat_interval: 1hreceiver: 'web.hook'
receivers:- name: 'web.hook'webhook_configs:#这个地址又是另外一个东西  prometheus-webhook-dingtalk- url: 'http://localhost:8060/dingtalk/webhook1/send'
  • 启动 ./alertmanager --config.file=alertmanager.yml

10.4 配置告警规则 以及promethus

  • 配置告警规则 建议放在promethus安装目录
  • 在promethus安装目录下新建 alerts.yml
  • 内容如下
# This is the rules file.groups:
- name: examplerules:- alert: InstanceDownexpr: up == 0for: 3mlabels:severity: pageannotations:summary: "Instance {{ $labels.instance }} down"description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."- alert: AnotherInstanceDownexpr: up == 0for: 10mlabels:severity: pageannotations:summary: "Instance {{ $labels.instance }} down"description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

这个规则的含义 当应用下线后 第一个规则会等待3分钟 如果还没恢复就会发送告警邮件 恢复后就不发送了 第二个规则 等待10分钟 同理

  • 配置promethus
# 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:#alertmanager 服务 端口默认9093- targets: ['localhost:9093']
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:# - "first_rules.yml"# - "second_rules.yml"- "alerts.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: nodestatic_configs:- targets: ['localhost:9100'] - job_name: gateway-servicemetrics_path: '/actuator/prometheus'static_configs:- targets: ['localhost:9091'] 
  • 启动promethus
./prometheus --config.file=prometheus.yml

10.5 prometheus-webhook-dingtalk

这是啥 干嘛的 简单说就是帮助进行发送钉钉消息的
以前的流程
promethus—>触发规则-> alterManager->进行发送
现在的流程
promethus—>触发规则-> alterManager->prometheus-webhook-dingtalk->进行发送

本来我们可以在alterManager单独配置 发送钉钉消息的webhook 但是模板消息 以及 密钥 都无法配置 (可能我的姿势不对)
所以加了一层转发 告警消息先到alterManager 然后转发给 prometheus-webhook-dingtalk prometheus-webhook-dingtalk来负责真实发送告警到钉钉

  • 下载 https://github.com/timonwong/prometheus-webhook-dingtalk/releases/tag/v2.1.0
  • 解压 & 配置
 ## Request timeout
# timeout: 5s## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true## Customizable templates path
#templates:
#  - contrib/templates/legacy/template.tmpl## You can also override default template using `default_message`
## The following example to use the 'legacy' template from v0.3.0
#default_message:
#  title: '{{ template "legacy.title" . }}'
#  text: '{{ template "legacy.content" . }}'## Targets, previously was known as "profiles"
targets:webhook1:#钉钉机器人的地址 url: https://oapi.dingtalk.com/robot/send?access_token=xxx# 钉钉 secret for signaturesecret: xxxxx
  • 启动
./prometheus-webhook-dingtalk --config.file=config.yml

10.6 进行测试

把gateway-service服务 停止掉 然后等待3分钟 (这个时间可以调整)
然后在 prometheus-webhook-dingtalk 控制台 即可看到如下日志
表示 alterManager 调用 prometheus-webhook-dingtalk 的 webhook成功了

ts=2023-02-09T11:06:21.615Z caller=entry.go:26 level=info component=web http_scheme=http http_proto=HTTP/1.1 http_method=POST remote_addr=[::1]:62467 user_agent=Alertmanager/0.25.0 uri=http://localhost:8060/dingtalk/webhook1/send resp_status=200 resp_bytes_length=2 resp_elapsed_ms=501.124351 msg="request complete"

同时也可以在钉钉上收到该提示
在这里插入图片描述

11 tips

钉钉消息模板 还有很多 可以自行配置
另外 还可以发送 邮箱告警 等等 需要修改 alterManager的配置即可
告警的规则 还有很多 需要自行研究 也有类似的模板
promethus的扩展:https://prometheus.io/docs/operating/integrations/
promethus的官网: https://prometheus.io/

the end good day

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://xiahunao.cn/news/1381900.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

十四、应用监控(1)

本章概要 监控端点配置&#xff08;开启端点&#xff0c;暴露端点&#xff0c;端点保护&#xff0c;端点响应缓存&#xff0c;路径映射&#xff0c;CORS支持&#xff0c;健康信息&#xff0c;应用信息&#xff09; 当一个Spring Boot 项目运行时&#xff0c;开发者需要对 Spr…

一篇文章讲透线上应用监控

“线上服务停了&#xff0c;要重启一下”&#xff1f;久经职场做研发的程序员&#xff0c;视线会逐渐转移到线上应用的运行状态。设想一下&#xff0c;如果你在半夜两点正在酣眠美梦时&#xff0c;微信群里突然炸开锅&#xff1a;“服务停了&#xff0c;先重启。。。”&#xf…

如何设置时间倒计时

开发工具与关键技术&#xff1a;Visual Studio 2015、SQL Server 2014 作者&#xff1a;黄世豪 撰写时间&#xff1a;2019年01月31号相信很多小伙伴在做项目时会遇到过要用到时间倒计时的效果&#xff0c;下面我们来实现一下如何将自己设置的时间来让其进行时间倒计。 jsp调用…

js倒计时读秒

1.需求及分析 最近,接到一个接收手机验证码的功能&#xff0c;大体就是设置一个定时器&#xff0c;在每次执行的时候将秒数&#xff0c;逐个减去1&#xff0c;小于0的时候&#xff0c;清除定时器&#xff0c;然后完善一下功能&#xff0c;例如&#xff0c;加上一下状态的变化&…

C#实现倒计时的功能

软件界面&#xff1a; 软代码&#xff1a; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;na…

android studio高考倒计时,高考倒计时app排行榜

编辑推荐 时间管理倒计时 v1.3.0 大小&#xff1a;13.7MB 更新时间&#xff1a;21-04-14 下载 时间管理倒计时&#xff0c;一款专为小伙伴打造的手机时间规划工具&#xff0c;为小伙伴提供更强大的规划服务&#xff0c;在这里小伙伴可以自由规划自己的时间&#xff0c;为小伙伴…

VS2015+C#实现倒计时小程序

最近疫情严重&#xff0c;在家呆着也是呆着&#xff0c;于是就想学一下上位机的编写&#xff0c;权衡利弊之后&#xff0c;选择了C#语言&#xff0c;原因是据说它写出来的上位机比较小&#xff0c;不占内存。开发环境就用VS吧&#xff0c;用的人挺多的。好了&#xff0c;进入正…

C#之一个倒计时小应用程序的实现

只能开始计时&#xff0c;不能停止计时 代码&#xff1a; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.W…

易语言编写倒计时小程序

本人为编程爱好者&#xff0c;非专业人员。 最近工作中需要用到编写倒计时软件&#xff0c;故有此篇。 软件功能&#xff1a; 1.时间倒计时 2.倒计时会有一个透明放大的标签显示时间 3.各窗口可隐藏 以下为软件运行界面&#xff1a; 代码&#xff1a; .版本 2 .支持库 she…

DIY蓝牙小音响

PRE 我个人是比较喜欢手工做一些小东西&#xff0c;但是由于知识限制&#xff0c;我无法去制作一些复杂的东西&#xff0c;如果是一些简单的小玩具&#xff0c;如果我有能力做&#xff0c;我还是很乐意去做的。前段时间正好在看音响&#xff0c;我最后入手了一个便宜的EDIFIER…

树莓派系列二(语音识别)

树莓派的基本概念和安装系统在系列文章一中介绍了.这篇准备介绍一下语音识别. 一直想研究一下语音识别,用来做家庭物联网的控制入口,未来也许就是这样,讯飞的叮咚音响可以连接京东的物联平台,苹果的homekit平台,华为的平台暂时落后的有点多... 国内语音识别领域,我个人比较欣…

毕业设计 基于单片机的智能音响设计与实现 -物联网 嵌入式 stm32

文章目录 0 前言1 简介2 主要器件3 实现效果4 设计原理4.1 PAJ7620U2模块4.2 HC-05蓝牙模块4.3 JQ8900语音模块 5 部分核心代码6 最后 0 前言 &#x1f525; 这两年开始毕业设计和毕业答辩的要求和难度不断提升&#xff0c;传统的毕设题目缺少创新和亮点&#xff0c;往往达不到…

两台电脑共享一个音响方法

两台电脑共享一个音响方法 由于某种原因&#xff0c;需要两个电脑使用同一个扬声器&#xff0c;现分享一种简单的方案 原理&#xff1a;通过主机的音频输入功能&#xff0c;将另一台电脑的音频接收过来并播放。 连线如下&#xff1a; PC2作为主设备&#xff0c;正常连接音响…

Spring IOC DI - 整合MyBatis

Spring IOC目录 主要内容Spring 框架介绍Spring 框架的优势(对比以前项目的缺点)Spring 框架引入历史发展框架学习三要素Spring 模块介绍 Spring IoC/DI - 引入IoC/DI 概念辨析使用IoC/DI的好处IoC/DI具体应用场景 Spring IoC/DI - 代码实现环境准备Spring 框架环境搭建创建Mav…

Spring框架——IOC、DI

本篇博客主要介绍Java中的IOC和DI&#xff0c;以及在String框架中的应用。首先&#xff0c;我们将对IOC和DI进行概念介绍&#xff0c;然后讲解它们的关系及在String框架中的应用&#xff0c;最后通过一个实例来展示它们的具体用法。 IOC和DI的概念介绍 IOC&#xff08;Invers…

叮咚!你点的Spring套餐来了!

儒猿技术团队最新出品&#xff1a; 《Spring顶尖高手进阶&#xff1a;互联网教育系统项目实战》 长按扫描下方二维码了解&#xff1a; 课程背景 在技术日新月异的今天&#xff0c;Spring作为Java主流开发框架&#xff0c;出道十多年仍然稳坐C位&#xff0c;经久不衰&#xff0c…

乐鑫Esp32学习之旅 17 全网首发,esp32 sdk直连京东微联·小京鱼·IoT开放平台,实现叮咚音响语音智能控制。

本系列博客学习由非官方人员 半颗心脏 潜心所力所写&#xff0c;仅仅做个人技术交流分享&#xff0c;不做任何商业用途。如有不对之处&#xff0c;请留言&#xff0c;本人及时更改。 1、 爬坑学习新旅程&#xff0c;虚拟机搭建esp32开发环境&#xff0c;打印 “Hellow World”。…

计时器setTimeout()函数、setInterval()函数

文章目录 &#x1f412;个人主页&#x1f3c5;JavaEE系列专栏&#x1f4d6;前言&#xff1a;&#x1f3c5;计时器setTimeout&#xff08;函数名&#xff0c;延迟时间&#xff09;结束计时器setTimeout &#x1f3c5;计时器setInterval&#xff08;函数名&#xff0c;延迟时间&a…

UGUI源码深度剖析

源码下载后 直接嵌入工程&#xff0c;删除引擎extension里的&#xff1b; 自制UI&#xff0c;在一个空场景中显示一个图片&#xff0c;当鼠标点击图片&#xff0c;执行操作。 gameobject &#xff1a; mesh meshfilter meshrender maintexture meshcollider camera ray