首页 > 其他分享 >springboot+Prometheus+grafana 实现自定义监控(请求数、响应时间、JVM性能)

springboot+Prometheus+grafana 实现自定义监控(请求数、响应时间、JVM性能)

时间:2023-05-10 17:44:08浏览次数:37  
标签:JVM 自定义 Prometheus grafana metrics prometheus registry 监控 springboot

自定义监控
1.Spring Boot 工程集成 Micrometer
1.1引入依赖
1.2配置
1.3监控jvm信息
1.4创建自定义监控
1.5添加具体业务代码监控
2.集成 Prometheus
2.1安装
2.2集成配置
3.使用 Grafana Dashboard 展示监控项
3.1安装grafana
3.2配置prometheus数据源
3.3增加jvm面板
3.4配置业务接口监控面板
1.Spring Boot 工程集成 Micrometer
1.1引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

1
2
3
4
5
6
7
8
9
1.2配置
management.server.port=9003
management.endpoints.web.exposure.include=*
management.endpoint.metrics.enabled=true
management.endpoint.health.show-details=always
management.endpoint.health.probes.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
management.metrics.tags.application=voice-qc-backend
1
2
3
4
5
6
7
8
这里 management.endpoints.web.exposure.include=* 配置为开启 Actuator 服务,因为Spring Boot Actuator 会自动配置一个 URL 为 /actuator/Prometheus 的 HTTP 服务来供 Prometheus 抓取数据,不过默认该服务是关闭的,该配置将打开所有的 Actuator 服务。
management.metrics.tags.application 配置会将该工程应用名称添加到计量器注册表的 tag 中去,方便后边 Prometheus 根据应用名称来区分不同的服务。

1.3监控jvm信息
然后在工程启动主类中添加 Bean 如下来监控 JVM 性能指标信息:

@SpringBootApplication
public class GatewayDatumApplication {

public static void main(String[] args) {
SpringApplication.run(GatewayDatumApplication.class, args);
}

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.4创建自定义监控
监控请求次数与响应时间

package com.lianxin.gobot.api.monitor;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
* @Author: GZ
* @CreateTime: 2022-08-30 10:50
* @Description: 自定义监控服务
* @Version: 1.0
*/
@Component
public class PrometheusCustomMonitor {
/**
* 上报拨打请求次数
*/
@Getter
private Counter reportDialRequestCount;
/**
* 上报拨打URL
*/
@Value("${lx.call-result-report.url}")
private String callReportUrl;

/**
* 上报拨打响应时间
*/
@Getter
private Timer reportDialResponseTime;
@Getter
private final MeterRegistry registry;


@Autowired
public PrometheusCustomMonitor(MeterRegistry registry) {
this.registry = registry;
}

@PostConstruct
private void init() {
reportDialRequestCount = registry.counter("go_api_report_dial_request_count", "url",callReportUrl);
reportDialResponseTime= registry.timer("go_api_report_dial_response_time", "url",callReportUrl);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
1.5添加具体业务代码监控
//统计请求次数
prometheusCustomMonitor.getReportDialRequestCount().increment();
long startTime = System.currentTimeMillis();
String company = HttpUtils.post(companyUrl,"");
//统计响应时间
long endTime = System.currentTimeMillis();
prometheusCustomMonitor.getReportDialResponseTime().record(endTime-startTime, TimeUnit.MILLISECONDS);
1
2
3
4
5
6
7
在浏览器访问 http://127.0.0.1:9001/actuator/prometheus ,就可以看到服务的一系列不同类型 metrics 信息,例如jvm_memory_used_bytes gauge、jvm_gc_memory_promoted_bytes_total counter ,go_api_report_dial_request_count等

 到此,Spring Boot 工程集成 Micrometer 就已经完成,接下里就要与 Prometheus 进行集成了。

2.集成 Prometheus
2.1安装
docker pull prom/prometheus
1
mdkir /usr/local/prometheus
1
vi prometheus.yml
1
# 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: ['192.168.136.129:9090']

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
docker run -d --name prometheus -p 9090:9090 -v/usr/local/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
1


2.2集成配置
global:
scrape_interval: 15s

scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "metricsLocalTest"
metrics_path: "/actuator/prometheus"
static_configs:
- targets: ["localhost:9003"]
1
2
3
4
5
6
7
8
9
10
11
这里 localhost:9001 就是上边本地启动的服务地址,也就是 Prometheus 要监控的服务地址。同时可以添加一些与应用相关的标签,方便后期执行 PromSQL 查询语句区分。最后重启 Prometheus 服务

 

 

3.使用 Grafana Dashboard 展示监控项
3.1安装grafana
docker pull grafana/grafana
1
docker run -d --name grafana -p 3000:3000 -v /usr/local/grafana:/var/lib/grafana grafana/grafana
1
默认用户名/密码 admin/admin


3.2配置prometheus数据源


3.3增加jvm面板
模板编号为4701

 


3.4配置业务接口监控面板

————————————————
版权声明:本文为CSDN博主「我叫果冻」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/GZ946/article/details/126619218

标签:JVM,自定义,Prometheus,grafana,metrics,prometheus,registry,监控,springboot
From: https://www.cnblogs.com/gaoyanbing/p/17388671.html

相关文章

  • [系统性能优化实践]JVM进阶实战之监控工具(Prometheus)
    1Prometheus监控SpringCloudGateway1.1简述API网关作为应用服务与外部交互的入口,通过对API网关的监控,可以清晰的知道应用整体的请求量,以便根据不同的并发情况进行扩容处理。对API网关的监控也是相当必要的。通过Prometheus监控Gateway与监控普通Springboot项目几乎......
  • 【SpringBoot】【自动装配】 SpringBoot自动装配原理
    1 前言我们都知道SpringBoot有个自动装配的机制,那你们知道平时如何使用么,以及他是什么时候执行的么,那么本节我们就来看看。2  为什么要有自动装配我们经历过SSM时期的时候,比如要引进Redis,是不是要先要导Maven依赖,还要进行大量的配置Bean,然后才能使用,而使用SpringBoot的......
  • springboot 项目中返回前端对象错误显示是string格式
    原因是返回json对象后面跟了一段,如下图这个错误藏的比较隐蔽,有个小的对象没有实现getter方法。在返回前端对象里,所有对象都得可以序列化和反序列化,对应的对象中所有属性是否都实现getter和setter等序列化。......
  • 百度地图绘制地区的棱柱效果-定位-自定义点-文本标记-信息弹窗
    @目录百度地图webgl使用自定义地图样式地区镂面棱柱效果绘制点信息以及信息弹窗百度地图webgl使用在项目的index.html中引入<scripttype="text/javascript"src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=你的AK秘钥"></script>注意,百度webgl的引入和百度地图的引入......
  • SpringBoot 配置文件加载优先级
    我们在使用springboot开发的时候,经常会从外部获取属性值,为了记住这些规则,特此做如下记录~~~一、为什么要做外部化配置本地开发的时候,上传文件的时候,每个人想上传的路径不一样,使用外部配置,就可以单独设置自己的上传路径项目部署的时候,不同的环境使用不同的配置,使用外部挂载配置这......
  • SpringBoot定义优雅全局统一Restful API 响应框架四
    如果没有看前面几篇文章请先看前面几篇SpringBoot定义优雅全局统一RestfulAPI响应框架SpringBoot定义优雅全局统一RestfulAPI响应框架二SpringBoot定义优雅全局统一RestfulAPI响应框架三目前我们好像似乎解决所有问题,达到了我们理想的效果如下但是在业务错误返回时候......
  • Android自定义Dialog(zhuang)
    Android自定义Dialog这段时间在做一个项目,需要使用到自定义Dialog,先在网上找了一下资料,发现还是有很多没有讲清楚的,在此给出一个Demo,一来可以方便广大码农,二来也可以方便自己,以备不时之需。。。先来一张图吧,很简单,只有一个Activity,当点击Button的时候就弹出这个自定义的Dialo......
  • 自定义 li 标签序列的样式
    第一步删除li标签的默认样式,取消::mark代理样式(默认样式)。第一步已经把默认样式取消了,自然没有了序号,使用CSS的counter()函数和counter-increment属性来自定义序列号。通过list-style-type:none删除li标签的默认样式在li标签样式中设置counter-increment:st......
  • SpringBoot+Redis+自定义注解实现接口防刷(限制不同接口单位时间内最大请求次数)
    场景SpringBoot搭建的项目需要对开放的接口进行防刷限制,不同接口指定多少秒内可以请求指定次数。比如下方限制接口一秒内最多请求一次。 注:博客:https://blog.csdn.net/badao_liumang_qizhi实现1、实现思路首先自定义注解,添加时间区间和最大请求次数字段。然后自定义......
  • Springboot创建多module项目--转载
    1)createnewproject(或File-->new-->project)2)选中SpringInitializr,点击Nextimage.png3)填写必要信息,点击Nextimage.png4)依赖页不用勾选,点击Nextimage.png5)选择项目存储目录,点击Finishimage.png6)在pom.xml中加入packaging......