使用Prometheus监控Java应用性能

server/2024/9/23 4:38:17/

使用Prometheus监控Java应用性能

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!今天,我们将探讨如何使用Prometheus监控Java应用的性能。

一、引入Prometheus客户端库

在Java应用中使用Prometheus进行监控,首先需要引入Prometheus的客户端库。在Maven项目中,可以在pom.xml文件中添加以下依赖:

<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient</artifactId><version>0.10.0</version>
</dependency>
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_httpserver</artifactId><version>0.10.0</version>
</dependency>
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_hotspot</artifactId><version>0.10.0</version>
</dependency>
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_common</artifactId><version>0.10.0</version>
</dependency>

二、创建指标

Prometheus使用不同类型的指标来收集和暴露应用的性能数据。主要有以下几种:

  • Counter:用于计数,例如请求的次数。
  • Gauge:用于表示一个可以上下浮动的值,例如当前的内存使用量。
  • Histogram:用于观察一段时间内的值的分布,例如请求的延迟时间。
  • Summary:与Histogram类似,但更注重百分位数的数据。

下面是一个示例代码,演示如何在Java应用中创建和使用这些指标:

java">package cn.juwatech.monitoring;import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import io.prometheus.client.Summary;
import io.prometheus.client.exporter.HTTPServer;
import io.prometheus.client.hotspot.DefaultExports;import java.io.IOException;public class MonitoringDemo {static final Counter requestCounter = Counter.build().name("requests_total").help("Total number of requests.").register();static final Gauge inprogressRequests = Gauge.build().name("inprogress_requests").help("Number of in-progress requests.").register();static final Histogram requestLatency = Histogram.build().name("requests_latency_seconds").help("Request latency in seconds.").register();static final Summary requestSize = Summary.build().name("requests_size_bytes").help("Request size in bytes.").register();public static void main(String[] args) throws IOException {// 启动Prometheus HTTP ServerHTTPServer server = new HTTPServer(1234);// 默认的JVM指标DefaultExports.initialize();// 模拟处理请求while (true) {processRequest();}}private static void processRequest() {requestCounter.inc();inprogressRequests.inc();Histogram.Timer requestTimer = requestLatency.startTimer();try {// 模拟请求处理int sleepTime = (int) (Math.random() * 1000);Thread.sleep(sleepTime);} catch (InterruptedException e) {e.printStackTrace();} finally {requestTimer.observeDuration();inprogressRequests.dec();requestSize.observe(Math.random() * 1000);}}
}

三、暴露指标

上面的代码已经演示了如何通过HTTPServer在端口1234上暴露Prometheus指标。运行Java程序后,可以通过浏览器访问http://localhost:1234/metrics来查看暴露的指标。

四、配置Prometheus服务器

接下来,需要配置Prometheus服务器来抓取这些暴露的指标。在Prometheus的配置文件prometheus.yml中,添加如下内容:

scrape_configs:- job_name: 'java_app'static_configs:- targets: ['localhost:1234']

保存配置文件后,启动Prometheus服务器:

./prometheus --config.file=prometheus.yml

五、创建Grafana仪表盘

Prometheus收集到数据后,可以通过Grafana来进行可视化。安装和启动Grafana后,按照以下步骤操作:

  1. 添加Prometheus数据源。
  2. 创建新的仪表盘,并添加相应的图表。
  3. 在图表的查询框中输入Prometheus的查询语言(PromQL)来获取和显示指标数据。

例如,查询总请求次数的PromQL语句为:

requests_total

查询请求延迟直方图的PromQL语句为:

histogram_quantile(0.95, sum(rate(requests_latency_seconds_bucket[5m])) by (le))

六、总结

通过以上步骤,我们实现了使用Prometheus监控Java应用性能。我们介绍了如何在Java应用中创建和使用Prometheus指标,如何配置Prometheus服务器抓取指标,以及如何通过Grafana进行数据的可视化。

本文著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!


http://www.ppmy.cn/server/93691.html

相关文章

Docker安装rocketMq

一、概述 RocketMQ是阿里巴巴开源的一款分布式消息中间件&#xff0c;用于处理大规模消息传输与存储。它使用Java语言编写&#xff0c;是阿里巴巴内部历经双十一等高并发场景考验的成熟产品。2016年开源后&#xff0c;RocketMQ捐赠给Apache&#xff0c;并成为了Apache的一个顶…

<数据集>棉花识别数据集<目标检测>

数据集格式&#xff1a;VOCYOLO格式 图片数量&#xff1a;13765张 标注数量(xml文件个数)&#xff1a;13765 标注数量(txt文件个数)&#xff1a;13765 标注类别数&#xff1a;4 标注类别名称&#xff1a;[Partially opened, Fully opened boll, Defected boll, Flower] 序…

堆的实现-向上调整算法-向下调整算法-堆排序-TopK问题 C语言

堆的实现与堆排序及TopK问题的C语言代码 下面是详细的堆实现&#xff0c;包括向上调整、向下调整算法&#xff0c;以及堆排序和解决TopK问题的完整C语言示例代码。 1. 堆的实现 首先&#xff0c;定义堆的数据结构&#xff1a; #include <stdio.h> #include <stdli…

IIS解析漏洞~ IIS7.漏洞分析

IIS解析漏洞 文件解析漏洞是由于中间件错误的将特殊格式的文件解析成可执行网页文件(脚本)&#xff0c;配合文件上传漏洞进行GetShell的漏洞&#xff01; 1.2&#xff1a;IIS7.X 在IIS7.0和IIS7.5版本下也存在解析漏洞&#xff0c;在默认Fast-CGI开启状况下&#xff0c;在一个文…

《看漫画学Python》全彩PDF教程,495页深度解析,零基础也能轻松上手!

前言 说起编程语言&#xff0c;Python 也许不是使用最广的&#xff0c;但一定是现在被谈论最多的。随着近年大数据、人工智能的兴起&#xff0c;Python 越来越多的出现在人们的视野中。 在各家公司里&#xff0c;Python 还常被用来做快速原型开发&#xff0c;以便更快验证产品…

python-自动化办公-Excel-Openpyxl

Python处理Excel数据之Openpyxl 1.1 Openpyxl库的安装使用 openpyxl模块是一个读写Excel 2010文档的 Python 库&#xff0c;如果要处理更早格式的Excel文档&#xff0c;需要用到额外的库&#xff0c;openpyxl是一个比较综合的工具&#xff0c;能够同时读取和修改Excel文档。其…

redis:清除缓存的最简单命令示例

清除redis缓存命令(执行命令列表见截图) 1.打开cmd窗口&#xff0c;并cd进入redis所在目录 2.登录redis redis-cli 3.查询指定队列当前的记录数 llen 队列名称 4.清除指定队列所有记录 ltrim 队列名称 1 0 5.再次查询&#xff0c;确认队列的记录数是否已清除

第十九次(安装nginx代理tomcat)

回顾 1.安装nodejs---jdk一样你的软件运行环境 yum -y list install|grep epel $? yum -y install nodejs #版本号 node -v 2.下载对应的nodejs软件npm yum -y install npm npm -v npm set config ...淘宝镜像 3.安装vue/cli command line interface 命令行接口 npm ins…