搭建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

相关文章

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

springboot将lib和jar分离的操作方法

《springboot将lib和jar分离的操作方法》本文介绍了如何通过优化pom.xml配置来减小SpringBoot项目的jar包大小,主要通过使用spring-boot-maven-plugin... 遇到一个问题,就是每次maven package或者maven install后target中的ja

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装

如何利用Java获取当天的开始和结束时间

《如何利用Java获取当天的开始和结束时间》:本文主要介绍如何使用Java8的LocalDate和LocalDateTime类获取指定日期的开始和结束时间,展示了如何通过这些类进行日期和时间的处... 目录前言1. Java日期时间API概述2. 获取当天的开始和结束时间代码解析运行结果3. 总结前言在J

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明