SpringBoot使用prometheus监控的示例代码

作者:大老杨 时间:2021-05-29 02:37:41 

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

1.关于Prometheus

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

2.有关Grafana

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

SpringBoot使用prometheus监控的示例代码

从图中就可以看出来,使用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,如图所示,可以看到一些度量指标。

SpringBoot使用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页面,如图所示。

SpringBoot使用prometheus监控的示例代码

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

SpringBoot使用prometheus监控的示例代码

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

SpringBoot使用prometheus监控的示例代码

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

SpringBoot使用prometheus监控的示例代码

5.Grafana配置

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

SpringBoot使用prometheus监控的示例代码

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

SpringBoot使用prometheus监控的示例代码

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

SpringBoot使用prometheus监控的示例代码

接下来导入Doshboard。

SpringBoot使用prometheus监控的示例代码

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

SpringBoot使用prometheus监控的示例代码

6.源码

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

来源:https://blog.csdn.net/qq_33257527/article/details/88294016

标签:SpringBoot,prometheus,监控
0
投稿

猜你喜欢

  • Java中包装类介绍与其注意事项

    2023-03-20 18:26:36
  • 一文带你入门SpringMVC的配置与使用

    2021-06-24 12:30:52
  • Android Build类的详解及简单实例

    2023-07-02 21:12:45
  • Java SpringMVC异步处理详解

    2021-08-10 15:03:58
  • 详解Java 集合系列(三)—— LinkedList

    2022-01-30 16:49:10
  • Mybatis注解实现多数据源读写分离详解

    2021-12-15 21:44:16
  • Struts2实现文件上传功能实例解析

    2023-11-04 04:56:13
  • IDEA使用GsonFormat完成JSON和JavaBean之间的转换

    2021-06-10 11:30:30
  • Spring如何处理注解的深入理解

    2023-09-09 14:35:49
  • Android 使用 SharedPreferences 保存少量数据的实现代码

    2023-07-03 01:00:11
  • SpringBoot整合SpringSecurity实现JWT认证的项目实践

    2022-06-23 22:09:09
  • 一文教会你使用jmap和MAT进行堆内存溢出分析

    2023-11-06 08:01:57
  • Java编程实现轨迹压缩之Douglas-Peucker算法详细代码

    2023-11-29 15:25:47
  • Jersey Restful接口如何获取参数的问题

    2023-10-29 14:44:16
  • Vs2022环境下安装低版本.net framework的实现步骤

    2023-07-04 02:58:12
  • java中transient关键字用法分析

    2022-01-22 04:27:05
  • Knife4j 3.0.3 整合SpringBoot 2.6.4的详细过程

    2023-11-25 03:54:34
  • Java Springboot的目的你知道吗

    2022-12-20 14:16:24
  • java教程之java注解annotation使用方法

    2023-11-13 20:18:57
  • Java设计模式之java备忘录模式详解

    2023-08-22 19:31:07
  • asp之家 软件编程 m.aspxhome.com