SpringBoot使用prometheus监控_spring security 只开启/actuator/promethues断点-程序员宅基地

技术标签: SpringBoot  springboot  

本文介绍SpringBoot如何使用Prometheus配合Grafana监控。

1.关于Prometheus

Prometheus是一个根据应用的metrics来进行监控的开源工具。相信很多工程都在使用它来进行监控,有关详细介绍可以查看官网:https://prometheus.io/docs/introduction/overview/

2.有关Grafana

Grafana是一个开源监控利器,如图所示。

从图中就可以看出来,使用Grafana监控很高大上,提供了很多可视化的图标。

官网地址:https://grafana.com/

3.SpringBoot使用Prometheus

3.1 依赖内容

在SpringBoot中使用Prometheus其实很简单,不需要配置太多的东西,在pom文件中加入依赖,完整内容如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.dalaoyang</groupId>
	<artifactId>springboot2_prometheus</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot2_prometheus</name>
	<description>springboot2_prometheus</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.micrometer</groupId>
			<artifactId>micrometer-registry-prometheus</artifactId>
			<version>1.1.3</version>
		</dependency>
	</dependencies>


	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

3.2 配置文件

配置文件中加入配置,这里就只进行一些简单配置,management.metrics.tags.application属性是本文配合Grafana的Dashboard设置的,如下所示:

spring.application.name=springboot_prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}

3.3 设置application

修改启动类,如下所示.

@SpringBootApplication
public class Springboot2PrometheusApplication {

	public static void main(String[] args) {
		SpringApplication.run(Springboot2PrometheusApplication.class, args);
	}
	@Bean
	MeterRegistryCustomizer<MeterRegistry> configurer(
			@Value("${spring.application.name}") String applicationName) {
		return (registry) -> registry.config().commonTags("application", applicationName);
	}
}

SpringBoot项目到这里就配置完成了,启动项目,访问http://localhost:8080/actuator/prometheus,如图所示,可以看到一些度量指标。

4.Prometheus配置

4.1 配置应用

在prometheus配置监控我们的SpringBoot应用,完整配置如下所示。

# 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:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['127.0.0.1:9090']
###以下内容为SpringBoot应用配置
  - job_name: 'springboot_prometheus'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['127.0.0.1:8080']

4.2 启动Prometheus

启动Prometheus,浏览器访问,查看Prometheus页面,如图所示。

点击如图所示位置,可以查看Prometheus监控的应用。

列表中UP的页面为存活的实例,如图所示。

也可以查看很多指数,如下所示。

5.Grafana配置

启动Grafana,配置Prometheus数据源,这里以ID是4701的Doshboard为例(地址:https://grafana.com/dashboards/4701)如图。

在Grafana内点击如图所示import按钮

在如图所示位置填写4701,然后点击load。

接下来导入Doshboard。

导入后就可以看到我们的SpringBoot项目对应的指标图表了,如图。

6.源码

源码地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_33257527/article/details/88294016

智能推荐

yolov5测试单张图片-程序员宅基地

文章浏览阅读4.1k次,点赞2次,收藏20次。yolov5测试单张图片,返回一个列表[类别,置信度,x,y,w,h]from numpy import randomimport torchfrom models.experimental import attempt_loadfrom utils.datasets import LoadStreams, LoadImagesfrom utils.general import ( check_img_size, non_max_suppression, apply_classifier_yolov5测试单张图片

SQL做的能改成Oracle吗,从SQL改写到SQL重写,什么样的SQL才是好SQL?(黄浩)-程序员宅基地

文章浏览阅读88次。从SQL改写到SQL重写,什么样的SQL才是好SQL?黄浩 2016-12-14 10:02:26作者介绍黄浩,现任职于中国惠普,从业十年,始终专注于SQL。十年一剑,十年磨砺。3年通信行业,写就近3万条SQL;5年制造行业,遨游在ETL的浪潮;2年性能优化,厚积薄发自成一家。在生活中,很多时候我们会有这样的体悟:问题要么不出,一旦出现,会像多诺米骨牌一样,会连锁引发诸多相关问题,让我们疲于应付。..._黄浩 sql

.NET 学习教程下载地址_.net课程下载-程序员宅基地

文章浏览阅读578次,点赞2次,收藏2次。==============================================================================================================教程&电子书==============================================================================================================C#入门经典_.net课程下载

STM32F7 + FREERTOS + LWIP 接收数据从网卡到应用层完整流程_stm32f7 lwip-程序员宅基地

文章浏览阅读3.8k次,点赞2次,收藏8次。来来来,这里解释下从网卡PHY到IP层的数据接收流程:这里是以函数调用方式来体现:netif_add——》ethernetif_init——》low_level_init——》ethernetif_input——》low_level_input和tcpip_input——》ethernet_input——》ip4_input(etharp_input、pppoe_disc_input)——》udp..._stm32f7 lwip

Linux服务器Input/output error错误_linux 写入文件到挂载的nas下提示failed to close...input/output-程序员宅基地

文章浏览阅读1.6w次。报错系统Centos报错提示Input/output error检查服务器机器中多硬盘是否其中有一块硬盘坏掉了。第二种可能:RAID阵列可能有问题。。。_linux 写入文件到挂载的nas下提示failed to close...input/output error

DevExpress SearchControl搜索框中的问题提示_devexpress searchcontrol1-程序员宅基地

文章浏览阅读4k次。SearchControl搜索框中的问题提示,代码为: this.searchControl1.Properties.NullValuePrompt = "请输入搜索关键字";_devexpress searchcontrol1

随便推点

对网站商城源码的研究分析 分享大量源码下载_chengren 电影-程序员宅基地

文章浏览阅读2k次。第一部分(1-6):前端纯静态网页模板无后台+大量网站设计素材 1:PC模板: 9900套响应式html5+css3网页模板【页面齐,二级,三级页均有,含中文模板】 2:PSD模板:3000套PSD模板+600套Flash酷站源文件+千套矢量ICO图标 3:手机模板:2000套各行业中文手机..._chengren 电影

ORA-39143: 转储文件 "F:\ora10G_expdp\ic_price_fromlufang.dmp" 可能是原始的导出转 储文件...-程序员宅基地

文章浏览阅读441次。连接到: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - ProductionWith the Partitioning, OLAP and Data Mining optionsORA-39001: 参数值无效ORA-39000: 转储文件说明错误ORA-39143: 转储文件 "F:\ora10G_expdp\ic_pri..._ora39143

8.4 输入某班学生某门课的成绩,(最多不超过40人,具体人数由用户键盘输入),用函数编程统计不及格人数-程序员宅基地

文章浏览阅读3.4w次,点赞16次,收藏28次。#includemain(){ int n,a[40],i,count=0; printf("请输入学生人数:"); scanf("%d",&n); printf("请输入各学生成绩:\n"); for(i=0;i

JAVASE笔记回顾-程序员宅基地

文章浏览阅读391次。第一部分,JAVA基础和面向对象 part01 入门与开发环境搭建 1: 计算机基础知识(了解)(1)计算机(2)计算机硬件(3)计算机软件系统软件:windows,linux,mac应用软件:QQ,YY,扫雷,CS/F(4)软件开发就是用开发工具和计算机语言做出软件(5)计算机语言人与计算机的交流方式(6)人机交互A:图像界面方便,简单,直观。B:DOS 窗口方式要有控制台, 要记住很多的命令,..._cuser.getchinese().equals("null")

将 PDF 转换为矢量图 emf_pdf转emf-程序员宅基地

文章浏览阅读2w次,点赞15次,收藏43次。此篇博客介绍了一种将PDF转换为矢量图emf、编辑emf的方法(需要Adobe Acrobat)_pdf转emf

各个 Android Gradle 插件版本所需的 Gradle 版本_gradle distributionurl 有那些版本-程序员宅基地

文章浏览阅读1.1k次。下表列出了各个 Android Gradle 插件版本所需的 Gradle 版本。要获得最佳性能,您应该使用 Gradle 和插件这两者的最新版本。插件版本 所需的 Gradle 版本 1.0.0 - 1.1.3 2.2.1 - 2.3 1.2.0 - 1.3.1 2.2.1 - 2.9 1.5.0 2.2.1 - 2.13 2.0.0 - 2.1...._gradle distributionurl 有那些版本

推荐文章

热门文章

相关标签