搭建Promethues + grafana +alertManager+blakbox 监控springboot 健康和接口情况

本文主要是介绍搭建Promethues + grafana +alertManager+blakbox 监控springboot 健康和接口情况,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一。概述

架构图:

拓扑图:

prometheus 是一个开源系统,用于构建监控和报警的工具包。

Prometheus 收集其指标并将其存储为时间序列数据,即指标信息与记录它的时间戳一起存储,以及称为标签的可选键值对。

特点:

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

  如上图所示:采集层负责数据的获取 ,支持多种exporters   Download | Prometheus支持什么可以去官网查看  应用层支持报表展示和报警发出

二。部署

部署prometheus

        (1) docker 部署 我这个是windows环境后面也可以是linux 一样的 或者去下载安装包

Download | Prometheus

        

docker run -p 9090:9090  -d  -v C:/Users/E2/Desktop/dockerv/prometheus:/opt/bitnami/prometheus/conf  bitnami/prometheus

 在docker 挂载目录添加配置文件prometheus.yml :

# my global config
global:scrape_interval:     15s # 设置多久获取一次数据evaluation_interval: 15s # 多久更新一次 报警规则# scrape_timeout is set to the global default (10s).# Alertmanager 配置
alerting:alertmanagers:- static_configs:- targets:#alertManager 服务因为我这个alertManager 没有容器部署放到本地所以这样- host.docker.internal:9093# 加载报警规则文件
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'# 监管自身 # scheme defaults to 'http'.static_configs:- targets: ['localhost:9090']#springboot的监控  - job_name: guanwang-servicemetrics_path: /actuator/prometheus# spring boot 项目地址和端口注意下监控 static_configs:- targets: ['host.docker.internal:2020']labels:applicaton: guanwang-kelkenv: dev# 黑盒测试配置文件这个只使用了http_2xx 来测接口可用性 - job_name: 'http_status'metrics_path: /probeparams:module: [http_2xx]file_sd_configs:- files:- '/opt/bitnami/prometheus/conf/job_web.yaml'refresh_interval: 30srelabel_configs:- source_labels: [__address__]target_label: __param_target- target_label: __address__#blackbox_exporter 的服务端口replacement: host.docker.internal:9115#云场站admin模块- job_name: yun-admin-servicemetrics_path: /sys/actuator/prometheusstatic_configs:- targets: ['host.docker.internal:8080']labels:applicaton: yun-admin-serviceenv: dev      

     添加报警规则 (这个需要 alertManager 服务的支持 安装在后面说明 )配置文件 就是上一个配置文件引入的那个 first_rules.yml 其他的可以看看文档自己去判断筛选和数值 

groups:
- name: 生产GIS接口监控rules:- alert: geoServer接口功能报警#筛选数据 查出来就会报警 这个判断黑盒测试的接口状态不为200 的expr: probe_http_status_code{project="geoserver"} !=200for: 15slabels:severity: 严重annotations:  description: "接口出现异常:{{ $labels.project }}"summary: "Web 访问异常:{{ $labels.desc }}:已无法访问"

添加黑盒测试配置文件 (这个需要你去下载 blackbox_exporter 安装支持可以帮你去测试接口可用性和接口时间) job_web.yaml

- targets:- https://*/geoserver/ows?service=WFS&version=1.1.0&request=GetFeature&typeName=ne%3Acloud_pipe_line_1&startIndex=0&propertyName=shape%2Cconduit_material%2Cconduit_type%2Cconduit_id%2Csection_no%2Cburying_type%2Csection_length%2Cstart_burying%2Cend_burying&outputFormat=application%2Fjson&maxFeatures=5000&srsName=EPSG%3A4326&cql_filter=tenant_id%3D871009%20and%20conduit_id%20IS%20NULL&_t=1688970060542labels:env: testapp: webproject: geoserverdesc: geoserver要素查询
- targets:- https://*/geoserver/ow?service=WFS&version=2.0.0&request=GetFeature&typeName=ne%3Acloud_customer_1&propertyName=shape%2Ccustomer_name%2Ccustomer_type%2Ctelephone%2Ccustomer_addr%2Cdoc_number&outputFormat=application%2Fjson&maxFeatures=100000&srsName=EPSG%3A4326&startIndex=0&service=WFS&version=2.0.0&request=GetFeature&typeName=ne%3Acloud_customer_1&propertyName=shape%2Ccustomer_name%2Ccustomer_type%2Ctelephone%2Ccustomer_addr%2Cdoc_number&outputFormat=application%2Fjson&maxFeatures=100000&srsName=EPSG%3A4326&startIndex=0labels:env: testapp: webproject: geoserverdesc: geoserver爆管分析not_200: yes # 这个自定义标签是为了标识某些地址在正常情况下不是返回200状态码

2.搭建 blackBox_exproter 先去官网下载 完成后启动项目 默认的配置文件

modules:http_2xx:prober: httphttp:preferred_ip_protocol: "ip4"http_post_2xx:prober: httphttp:method: POSTtcp_connect:prober: tcppop3s_banner:prober: tcptcp:query_response:- expect: "^+OK"tls: truetls_config:insecure_skip_verify: falsegrpc:prober: grpcgrpc:tls: truepreferred_ip_protocol: "ip4"grpc_plain:prober: grpcgrpc:tls: falseservice: "service1"ssh_banner:prober: tcptcp:query_response:- expect: "^SSH-2.0-"- send: "SSH-2.0-blackbox-ssh-check"irc_banner:prober: tcptcp:query_response:- send: "NICK prober"- send: "USER prober prober prober :prober"- expect: "PING :([^ ]+)"send: "PONG ${1}"- expect: "^:[^ ]+ 001"icmp:prober: icmpicmp_ttl5:prober: icmptimeout: 5sicmp:ttl: 5

代表黑盒测试支持那些类型的测试一般不用改,之前有dns 测试需要添加额外的配置

3.搭建alertManager 也是去官方下载 启动  修改配置文件 

global:smtp_smarthost: 'smtp.163.com:25'smtp_from: 'XX@163.com'smtp_auth_username: 'XX@163.com'smtp_auth_password: 'XX'smtp_require_tls: false
templates:- 'C:/Users/E2/Desktop/dockerv/alertmanager-0.25.0.windows-amd64/alertmanager-0.25.0.windows-amd64/data/tml/*.tmpl' 
route:group_by: ['alertname']group_wait: 30sgroup_interval: 5mrepeat_interval: 1hreceiver: 'mail'
receivers:- name: 'mail'email_configs:#可以配置多个用,链接- to: 'XX@qq.com'send_resolved: truehtml: '{{ template "email.tmpl" . }}'
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']

我这面只配置了邮件 默认是web_hook 需要你自己去实现接口还支持企业微信这个要自己去看下怎么选择和配置了

配置邮件发送显示的模版 :

{{ define "email.tmpl" }}
{{ range .Alerts }}<pre>
实例: {{ .Labels.instance }}
信息: {{ .Annotations.summary }}
详情: {{ .Annotations.description }}
时间: {{ .StartsAt.Format "2006-01-02 15:04:05" }}</pre>
{{ end }}
{{ end }}

C:/Users/E2/Desktop/dockerv/alertmanager-0.25.0.windows-amd64/alertmanager-0.25.0.windows-amd64/data/tml/*.tmpl 我的放置路径文件当中也有配置

4.spring boot 接入  刚刚prometheus.yml 配置了一个spring boot项目的监管,现在项目需要做支持

引入依赖 项目

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

使用了springfox swagger 可能会报错 要通过代码配置暴露所有

    @Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath = webEndpointProperties.getBasePath();EndpointMapping endpointMapping = new EndpointMapping(basePath);boolean shouldRegisterLinksMapping = webEndpointProperties.getDiscovery().isEnabled() &&(StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, null);}
/*** @author chenkang* @date 2023/7/11 16:02*/
@Configuration
public class PrometheusConfig {@Resourceprivate ModuleConfig moduleConfig;@BeanMeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {return registry -> registry.config().commonTags("application",moduleConfig.getName());}
}

不然yaml 配置就可以:

management:endpoint:prometheus:enabled: truemetrics:enabled: truemetrics:export:prometheus:enabled: truetags:#这个尽量去配置不然你用好多面板模版的时候 会只认这个标签 application: menhu-guanwang

5.安装 grafana 也是docker  这面--link 了刚刚安装的prometheus 不然连不上

docker run -d --name grafana -p 3000:3000  -v C:/Users/E2/Desktop/dockerv/grafana/data:/var/lib/grafana --link d13cacf78b0ecd41271542c3bd393948aee07b31ebfe880d4b4ad5b73e66d02f:prometheus grafana/grafana 

启动后访问3000 默认的账号密码是admin/admin 登陆后修改密码 现在启动所有项目

 选择创建数据源:

 因为我这个是docker装的host 就是填写link 起的别名如果你是安装包直接填写ip 我的是最新版本所以版本

 链接成功了就:

6.配置grafana 面板

官方已经做好了很多的面板模版 不用你自己配置,自己想配置也行  官方的模版地址

Dashboards | Grafana Labs

spring boot 那块:

我用的模版ID 10280

仪表盘选择导入选择 模版ID 点击确定 

想监管服务的响应时间 可以使用 micrometer-registry-prometheus @Timed注解 和 @Counted 注解 不然也是会带的 只不过不细分

黑盒测试:新增一个面板  

模版ID 找不到了 

这篇关于搭建Promethues + grafana +alertManager+blakbox 监控springboot 健康和接口情况的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683544

相关文章

Java Stream流与使用操作指南

《JavaStream流与使用操作指南》Stream不是数据结构,而是一种高级的数据处理工具,允许你以声明式的方式处理数据集合,类似于SQL语句操作数据库,本文给大家介绍JavaStream流与使用... 目录一、什么是stream流二、创建stream流1.单列集合创建stream流2.双列集合创建str

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。