普罗米修斯java_springboot集成普罗米修斯

普罗米修斯java_springboot集成普罗米修斯点击上方“方志朋”,选择“设为星标”回复”666“获取新整理的面试文章Prometheus是一套开源的系统监控报警框架。它由工作在SoundCloud的员工创建,并在2015年正式发布的开源项目。2016年,Prometheus正式加入CloudNativeComputingFoundation,非常的受欢迎。简介Prometheus具有以下特点:一个多维数据模型,其中…

大家好,又见面了,我是你们的朋友全栈君。

点击上方“方志朋”,选择“设为星标”

回复”666“获取新整理的面试文章

0fbad084dc3c3bfa49347c853f56b47f.png

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 等。

一些其他的工具。

54b99fdf29164c485f2f8f7d6e4d001c.png

其大概的工作流程是:

Prometheus server 定期从配置好的 jobs 或者 exporters 中拉 metrics,或者接收来自 Pushgateway 发过来的 metrics,或者从其他的 Prometheus server 中拉 metrics。

Prometheus server 在本地存储收集到的 metrics,并运行已定义好的 alert.rules,记录新的时间序列或者向 Alertmanager 推送警报。

Alertmanager 根据配置文件,对接收到的警报进行处理,发出告警。

在图形界面中,可视化采集数据。

springboot 集成prometheus

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

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

io.micrometer

micrometer-registry-prometheus

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

server:

port: 8081

spring:

application:

name: my-prometheus

management:

endpoints:

web:

exposure:

include: ‘prometheus’

metrics:

tags:

application: ${spring.application.name}

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

@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的一些监控信息。

# 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/)下载并安装。

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,代码如下:

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=` 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指定配置文件

./prometheus –config.file=prometheus.yml

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

7e26f06e6538c8ac617e988d7958ae80.png

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

b23861eab6036024a617921f7662001b.png

安装grafana

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

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。

配置数据源,如图:

520793111c667864f6f6649d47339f21.png

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

c5f1aa360af126dbf025b34b5fe83377.png

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

f655418b6b683ded24d93f6705a9d053.png

参考资料

[Prometheus 入门与实践]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/161775.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 读写TGA文件

    偶尔会遇到处理TGA文件的需求,封装成类以后再用到会很方便。    类的名字叫做myTGA,提供以下功能:    1:读取文件;    2:保存文件到指定目录;    3:获取图像信息(宽,高,深度/像素占用比特数,像素通道数);    4:访问像素;    5:转换到AUX_RGBImageRec 格式;    6:设计优良的结构易于扩展(目前只支

    2022年4月6日
    50
  • JAVA之文件操作[通俗易懂]

    JAVA之文件操作[通俗易懂]Console此类包含多个方法,可访问与当前Java虚拟机关联的基于字符的控制台设备(如果有)。虚拟机是否具有控制台取决于底层平台,还取决于调用虚拟机的方式。如果虚拟机从一个交互式命令行开始启动,且没有重定向标准输入和输出流,那么其控制台将存在,并且通常连接到键盘并从虚拟机启动的地方显示。如果虚拟机是自动启动的(例如,由后台作业调度程序启动),那么它通常没有控制台。如果此虚拟机具

    2025年7月17日
    0
  • Aria2安装(aria2安卓版手机配置教程)

    1安装sudopacman-Saria22创建配置文件cd~mkdir.aria2cd.aria2toucharia2.confaria2.logaria2.session3修改配置文件aria2.conf需要自己配置rpc-secret和bt-trackerbt-tracker可以去trackerslist网站获取#文件的保存路径(可使用绝对路径或相对路径),默认:当前启动位置dir=${HOME}/Downloads#启用磁盘缓存,0为

    2022年4月14日
    1.1K
  • date和localdatetime转换_localDate

    date和localdatetime转换_localDatejava.util.Date和SimpleDateFormatter都不是线程安全的,而LocalDate和LocalTime和最基本的String一样,是不变类型,不单线程安全,而且不能修改。java.util.Date月份是从0开始,一月是0,十二月是11。java.time.LocalDate月份和星期都改成了enum,就不可能再用错了。java.util.Date包含日期,时间,还有毫秒数,在新的java8中,日期和时间被明确划分为LocalDate和LocalTime,LocalDat

    2022年10月3日
    3
  • 【剑指offer】删除字符也出现在一个字符串

    【剑指offer】删除字符也出现在一个字符串

    2022年1月3日
    43
  • pycharm中html怎么运行_pycharm如何调试代码

    pycharm中html怎么运行_pycharm如何调试代码PyCharm调试程序,cmd中输入的变量怎么设置PyCharm调试程序,cmd中输入的变量的设置方法CMDdos定义变量,DOS下也只有环境变量可以用;SET[variable=[string]]variable指定环境变量名。string指定要指派给变量的一系列字符串。要显示当前环境变量,键入不带参数的SET。使用pycharm添加py文件,怎么调试PyCharm安装1…

    2022年8月25日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号