Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >springboot集成普罗米修斯

springboot集成普罗米修斯

作者头像
方志朋
发布于 2022-01-06 08:29:50
发布于 2022-01-06 08:29:50
1.8K00
代码可运行
举报
运行总次数:0
代码可运行

Prometheus 是一套开源的系统监控报警框架。它由工作在 SoundCloud 的 员工创建,并在 2015 年正式发布的开源项目。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,非常的受欢迎。

简介

Prometheus 具有以下特点:

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

Prometheus 组成及架构

声明:该小节参考了文章[Prometheus 入门与实践]

Prometheus 生态圈中包含了多个组件,其中许多组件是可选的:

  • Prometheus Server: 用于收集和存储时间序列数据。
  • Client Library: 客户端库,为需要监控的服务生成相应的 metrics 并暴露给 Prometheus server。当 Prometheus server 来 pull 时,直接返回实时状态的 metrics。
  • Push Gateway: 主要用于短期的 jobs。由于这类 jobs 存在时间较短,可能在 Prometheus 来 pull 之前就消失了。为此,这次 jobs 可以直接向 Prometheus server 端推送它们的 metrics。这种方式主要用于服务层面的 metrics,对于机器层面的 metrices,需要使用 node exporter。
  • Exporters: 用于暴露已有的第三方服务的 metrics 给 Prometheus。
  • Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进行去除重复数据,分组,并路由到对收的接受方式,发出报警。常见的接收方式有:电子邮件,pagerduty,OpsGenie, webhook 等。
  • 一些其他的工具。

其大概的工作流程是:

  1. Prometheus server 定期从配置好的 jobs 或者 exporters 中拉 metrics,或者接收来自 Pushgateway 发过来的 metrics,或者从其他的 Prometheus server 中拉 metrics。
  2. Prometheus server 在本地存储收集到的 metrics,并运行已定义好的 alert.rules,记录新的时间序列或者向 Alertmanager 推送警报。
  3. Alertmanager 根据配置文件,对接收到的警报进行处理,发出告警。
  4. 在图形界面中,可视化采集数据。

springboot 集成prometheus

在spring boot工程中引入actuator的起步依赖,以及micrometer-registry-prometheus的依赖。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
		<groupId>io.micrometer</groupId>
		<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

暴露prometheus的接口;暴露metrics.tags,和spring.application.name一致。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
server:
  port: 8081
spring:
  application:
    name: my-prometheus
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus'
  metrics:
    tags:
      application: ${spring.application.name}

写一个API接口,用作测试。代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RestController
public class TestController {
    Logger logger = LoggerFactory.getLogger(TestController.class);

    @GetMapping("/test")
    public String test() {
        logger.info("test");
        return "ok";
    }

    @GetMapping("")
    public String home() {
        logger.info("home");
        return "ok";
    }
}

在浏览器上访问http://localhost:8081/actuator/prometheus,展示的信息如下,这些信息都是actuator的一些监控信息。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="my-prometheus",} 2.863661056E9
# HELP http_server_requests_seconds  
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0
http_server_requests_seconds_sum{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327
# HELP http_server_requests_seconds_max  
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="my-prometheus",state="waiting",} 12.0
jvm_threads_states_threads{application="my-prometheus",state="runnable",} 8.0
jvm_threads_states_threads{application="my-prometheus",state="timed-waiting",} 2.0
jvm_threads_states_threads{application="my-prometheus",state="terminated",} 0.0
jvm_threads_states_threads{application="my-prometheus",state="blocked",} 0.0
jvm_threads_states_threads{application="my-prometheus",state="new",} 0.0
# HELP process_files_open_files The open file descriptor count
# TYPE process_files_open_files gauge
...省略更多

安装Prometheus

安装Prometheus很简单,在linux系统上安装,执行以下的安装命令。其他的操作系统,比如windows、mac等在官网上(https://prometheus.io/download/)下载并安装。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.darwin-amd64.tar.gz
tar xvfz prometheus-*.tar.gz
cd prometheus-*

修改Prometheus的配置文件prometheus.yml,代码如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.

  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, Alertmanager).
  external_labels:
    monitor: 'codelab-monitor'

# 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'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'springboot_prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['127.0.0.1:8081']
  • config.job_name,配置job的名称
  • config.scrape_interval,配置多久抓一次监控信息
  • config.metrics_path,获取监控信息的接口
  • config.static_configs.targets配置获取监控信息的地址。

使用以下的命令启动prometheus,并通过–config.file指定配置文件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
./prometheus --config.file=prometheus.yml

多次请求springboot项目的接口http://localhost:8081/test , 并访问prometheus的控制台http://localhost:9090/,展示的界面如下:

prometheus提供了一些可视化图,比如使用柱状图来展示每秒请求数:

安装grafana

grafana 是一款采用 go 语言编写的开源应用,主要用于大规模指标数据的可视化展现,是网络架构和应用分析中最流行的时序数据展示工具,目前已经支持绝大部分常用的时序数据库。使用grafana去展示prometheus上的数据。先安装,安装命令如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
wget https://dl.grafana.com/oss/release/grafana-7.0.4.darwin-amd64.tar.gz
tar -zxvf grafana-7.0.4.darwin-amd64.tar.gz
./grafana-server

访问http://localhost:3000/,初始密码:admin/admin。

配置数据源,如图:

配置prometheus的地址:http://localhost:9090 ,如图所示:

在dashboard界面新建panel,展示的metrics为http_server_request_seconds_count,即展示了以时间为横轴,请求数为纵轴的请求曲线,如图所示:

参考资料

[Prometheus 入门与实践](

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Actuator + Prometheus + Grafana搭建微服务监控平台
本文的主要目的是实现微服务的监控,简单了解了上述工具的概念后,我们就来动手实践一下。首先创建一个简单的Spring Boot项目,其主要依赖如下:
端碗吹水
2020/09/23
2.7K0
Actuator + Prometheus + Grafana搭建微服务监控平台
Spring Boot 2.x监控数据可视化(Actuator + Prometheus + Grafana手把手图文教程)
众所周知,Spring Boot有个子项目Spring Boot Actuator,它为应用提供了强大的监控能力。从Spring Boot 2.0开始,Actuator将底层改为Micrometer,提供了更强、更灵活的监控能力。Micrometer是一个监控门面,可以类比成监控界的 Slf4j 。
用户1516716
2019/05/13
2.3K0
Spring Boot 2.x监控数据可视化(Actuator + Prometheus + Grafana手把手图文教程)
Spring Boot服务监控(Prometheus)
我生命里的的最大突破之一,就是我不再为别人的看法而担忧。此后,我真的能自由的去做我认为对自己最好的事,只有在我们不需要外来的赞许时,才变得自由。
凯哥的Java技术活
2022/07/08
7110
Spring Boot服务监控(Prometheus)
Prometheus + Grafana 监控 SpringBoot
Prometheus 是监控系统,可以从 Springboot 获取监控数据,以时序数据的形式存储,并提供了监控数据的查询服务。
dys
2020/02/19
2K0
彻底搞懂监控系统,使用Prometheus监控Spring Boot应用,自定义应用监控指标!
前面我们介绍了使用Prometheus + Grafana 构建了监控系统,那么我们的应用平台怎么监控呢?应用平台中的核心业务的执行情况能否监控呢?那么接下来我们使用Actuator,Micrometer,Prometheus和Grafana监控Spring Boot应用程序,自定义应用监控指标。
架构师精进
2023/03/23
14.4K2
彻底搞懂监控系统,使用Prometheus监控Spring Boot应用,自定义应用监控指标!
【监控利器Prometheus】——Prometheus+Grafana监控SpringBoot项目JVM信息
(4)启动springboot项目,访问 http://localhost:6001/actuator/prometheus 可以看到一些统计指标
DannyHoo
2021/12/23
1.3K0
【监控利器Prometheus】——Prometheus+Grafana监控SpringBoot项目JVM信息
Spring Boot 使用 Micrometer 集成 Prometheus 监控 Java 应用性能
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
哎_小羊
2019/09/18
10.2K0
Spring Boot 使用 Micrometer 集成 Prometheus 监控 Java 应用性能
使用Prometheus实现大规模的应用程序监视【Containers】
我们有充分的理由证明Prometheus是一个日益流行的开源工具。开源工具可以为应用程序和服务器提供监视和警报。 Prometheus的强大优势在于监视服务器端指标,并将其存储为时间序列数据。尽管Prometheus并不适合于应用程序性能管理,主动控制或用户体验监视(尽管GitHub扩展确实使Prometheus可以使用用户浏览器指标),但Prometheus作为监视系统的能力是很强的,并且能够通过联盟实现高可扩展性服务器的数量使Prometheus成为各种使用案例的强大选择。
王欣壳
2019/11/09
1.6K0
使用Prometheus实现大规模的应用程序监视【Containers】
号称下一代可视化监控系统,结合SpringBoot使用,贼爽!
Grafana是一款开源的数据可视化和分析工具,不管你的指标信息存储在哪里,你都可以用它来可视化这些数据。同时它还具有告警功能,当指标超出指定范围时会提醒你。
macrozheng
2021/07/27
5350
号称下一代可视化监控系统,结合SpringBoot使用,贼爽!
普罗米修斯安装
Prometheus是一个最初在SoundCloud上构建的监控系统。自2012年成为社区开源项目,拥 有非常活跃的开发人员和用户社区。为强调开源及独立维护,Prometheus于2016年加入云原生云计算基金 会(CNCF),成为继Kubernetes之后的第二个托管项目。
summerking
2022/09/19
5030
普罗米修斯安装
普罗米修斯监控openGauss
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/161777.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/09
1.4K0
普罗米修斯监控openGauss
聊聊springboot2的micrometer
springboot2在spring-boot-actuator中引入了micrometer,对1.x的metrics进行了重构,另外支持对接的监控系统也更加丰富(Atlas、Datadog、Ganglia、Graphite、Influx、JMX、NewRelic、Prometheus、SignalFx、StatsD、Wavefront)。1.x的metrics都有点对齐dropwizard-metrics的味道,而micrometer除了一些基本metrics与dropwizard-metrics相类似外,重点支持了tag。这是一个很重要的信号,标志着老一代的statsd、graphite逐步让步于支持tag的influx以及prometheus。
code4it
2018/09/17
2.2K0
普罗米修斯
Prometheus(普罗米修斯)是一套开源的监控系统,其基本原理是通过 HTTP 协议周期性抓取被监控组件的状态,不需要任何 SDK 或者其他的集成过程,其架构如图:
六个核弹
2022/12/23
2.8K0
普罗米修斯
基于Prometheus搭建SpringCloud全方位立体监控体系
最近公司在联合运维做一套全方位监控的系统,应用集群的技术栈是SpringCloud体系。虽然本人没有参与具体基础架构的研发,但是从应用引入的包和一些资料的查阅大致推算出具体的实现方案,这里做一次推演,详细记录一下整个搭建过程。
Throwable
2020/06/23
2.7K0
SpringBoot掌握的差不多了,就剩下一个Actuator没搞定了,本文详细来介绍!!!
  通过前面的介绍我们明白了SpringBoot为什么能够很方便快捷的构建Web应用,那么应用部署上线后的健康问题怎么发现呢?在SpringBoot中给我们提供了Actuator来解决这个问题。
用户4919348
2021/09/08
1.6K0
SpringBoot掌握的差不多了,就剩下一个Actuator没搞定了,本文详细来介绍!!!
Spring Boot Actuator 整合 Prometheus
Spring Boot 自带监控功能 Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等。这一节结合 Prometheus 、Grafana 来更加直观的展示这些信息。
程序员果果
2019/10/23
2.6K0
Prometheus for Spring Boot
前面两篇文章已经介绍如何搭建Prometheus和Grafana,本文介绍如何快速监控Spring Boot进程 快速接入 引入Jar包(pom.xml) <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</gr
十毛
2019/03/27
9220
Prometheus for Spring Boot
使用prometheus和grafana监控springboot应用
-v 参数,挂载本地~/spring-boot-on-kubernetes/docker/prometheus.yml
全栈程序员站长
2021/05/19
8120
Prometheus监控学习笔记之Prometheus普罗米修斯监控入门
视频讲解通过链接网易云课堂·IT技术快速入门学院进入,更多关于Prometheus的文章。
Jetpropelledsnake21
2018/12/25
9.7K0
十分钟就能上手Prometheus与Grafana监控SpringBoot项目
🍁 作者:知识浅谈,CSDN签约讲师,CSDN原力作者,后端领域优质创作者,热爱分享创作 💒 公众号:知识浅谈 📌 擅长领域:全栈工程师、爬虫、ACM算法 🤞这次都给他拿下🤞 十分钟快速上手Prometheus与Grafana监控SpringBoot项目 先来捋一下数据流的传输 正菜来了⛳⛳⛳ 环境: springboot项目:127.0.0.1:8081 prometheus:127.0.0.1:9090 grafana:127.0.0.1:3000 🎈项目的创
知识浅谈
2022/12/18
7270
十分钟就能上手Prometheus与Grafana监控SpringBoot项目
推荐阅读
相关推荐
Actuator + Prometheus + Grafana搭建微服务监控平台
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验