1. SpringBoot 配置

1.1 依赖内容

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.dalaoyang</groupId>
  12. <artifactId>springboot2_prometheus</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot2_prometheus</name>
  15. <description>springboot2_prometheus</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-actuator</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>io.micrometer</groupId>
  35. <artifactId>micrometer-registry-prometheus</artifactId>
  36. <version>1.1.3</version>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

1.2 配置文件

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

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

1.3 设置application

修改启动类,如下所示.

  1. @SpringBootApplication
  2. public class Springboot2PrometheusApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Springboot2PrometheusApplication.class, args);
  5. }
  6. @Bean
  7. MeterRegistryCustomizer<MeterRegistry> configurer(
  8. @Value("${spring.application.name}") String applicationName) {
  9. return (registry) -> registry.config().commonTags("application", applicationName);
  10. }
  11. }

SpringBoot项目到这里就配置完成了,启动项目,访问http://localhost:8080/actuator/prometheus

2. Prometheus配置

在 prometheus 的配置文件中加入如下配置:

  1. scrape_configs:
  2. ###以下内容为SpringBoot应用配置
  3. - job_name: springboot_prometheus
  4. metrics_path: /actuator/prometheus
  5. static_configs:
  6. - targets: ['[ip]:[port]']
  7. labels:
  8. instance: sprint-boot

3. Grafana配置

https://grafana.com/dashboards/4701

4. 源码

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

为了方便测试, 我们可以将此源码 mvn clean -DskipTests package 打成 jar 包来启动。