SpringBoot使用prometheus监控的示例代码
作者:大老杨 发布时间:2021-05-29 02:37:41
本文介绍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
来源:https://blog.csdn.net/qq_33257527/article/details/88294016


猜你喜欢
- 程序生成的自定义文件,比如后缀是.test这种文件怎么直接启动打开程序,并打开本文件呢 1、
- 本文实例为大家分享了利用多线程和Socket实现猜拳游戏的具体代码,供大家参考,具体内容如下实例:猜拳游戏猜拳游戏是指小时候玩的石头、剪刀、
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串Given a string
- strcpy函数详解如下1.函数介绍1.1.函数接口char * __cdecl strcpy(char * dst, const char
- 基础配置新建module:cloudalibaba-config-nacos-client3377pom文件版本号已经由父工程控制<?
- 在项目中,经常会遇到页面分割,最常见的系统或网站的主界面。主页面分为,上面系统简介、下面作者简介、左边系统功能菜单、右边则是菜单真正展示的界
- 本文实例为大家分享了Android空心圆及层叠效果的具体代码,供大家参考,具体内容如下package com.bwei.test.zidin
- 前言OpenCVSharp是OpenCV的.NET wrapper,是一名日本工程师开发的,项目地址为:https://github.com
- 建库建表DROP DATABASE IF EXISTS mp;CREATE DATABASE mp DEFAULT CHARACTER SE
- idea删除模块后重新创建显示该模块已经被注册原因:注册信息没有删除干净解决方案:找到gradle.xml,modules.xml,work
- 我之前写了一篇关于美团网,大众点评的购买框效果的文章Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果,我自己感
- android:id 为控件指定相应的ID android:text 指定控件的文本,置尽量使用strings.xml android:gr
- 一元运算符,也叫单项算符,一目运算符,一元算符 ,英文名字:UnaryOperator。描述:接受一个参数为类型T,返回值类型也为T。源码:
- 在Android studio实现简易计算器App并实现加减乘除功能,供大家参考,具体内容如下结果activity_main.xml<
- 这篇文章主要介绍了Maven打包jar生成javadoc文件和source文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作
- 一、简介构造函数,基本用法是在类对象声明的时候完成初始化工作。二、实例构造函数1、构造函数的名字与类名相同。2、使用 new 表达式创建类的
- 1.顺
- 环境:maven+idea。1. 需要的jar包基本的spring和mybatis依赖包就不说了,在pom文件的build->plug
- 客户端import java.io.BufferedReader;import java.io.InputStreamReader;impo
- 前言;Apache common-pool对象池介绍:对象生命周期、Config详解、代码说明对象生命周期Config详解maxActive