记录如何配置与启动

1.在搭建好的应用加上依赖

        <!-- 实现对 Actuator 的自动化配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Micrometer 对 Prometheus 的支持 -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

2.配置文件修改

spring:
  application:
    name: demo-application # 应用名

management:
  endpoints:
    # Actuator HTTP 配置项,对应 WebEndpointProperties 配置类
    web:
      exposure:
        include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。

  metrics:
    tags: # 通用标签
      application: ${spring.application.name}
  • 【重要】配置项 management.metrics.tags ,设置 Metrics 通用标签。这里,我们配置了一个通过用标签键为 application ,值为 ${spring.application.name} 。我们来试着想下,应用 A 和应用 B 都有相同的 Metrics 名,那么如果我们需要去区分它们,则需要通过给 Metrics 打上不同的标签来区分,而一般情况下,我们会选择 application 作为标签。如果胖友有使用过 Prometheus + Grafana 来做监控报表,没这个配置granfana识别不到

3.一个正经的应用

// Application.java

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
}

4.测试应用的ip:端口/actuator/prometheus ,例如http://127.0.0.1:8080/actuator/prometheushttp://localhost:18084/actuator/prometheushttp://127.0.0.1:8080/actuator/prometheus

本地spingboot配置Promethus+granfana监控-LMLPHP

5. 配置启动prometheus

# 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:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'demo-application'
    # 采集地址
    metrics_path: '/actuator/prometheus'
    # 目标服务器
    static_configs:
    - targets: ['127.0.0.1:18084']

localhost:9090为prometheus启动端口及数据源端口,添加一个采集地址即可,打开prometheus验证,访问localhost:9000,看监控对象status/Targets

本地spingboot配置Promethus+granfana监控-LMLPHP

 至此prometheus结束

6.启动配置granfana

启动granfana,访问localhost:3000,admin/admin登录

添加数据源datasource,点击add,选择prometheus数据源,输入它提示的localhost:9090,点击下方测试保存通过即可

本地spingboot配置Promethus+granfana监控-LMLPHP

配置监控面板,grafana.com/grafana/dashboards/?search=JVM,进入granfana官网监控表盘选择任意一款喜欢的,通过load id或者json导入,例如14370监控jvm

本地spingboot配置Promethus+granfana监控-LMLPHP

 load后选择刚刚创建好的数据源,import

本地spingboot配置Promethus+granfana监控-LMLPHP

进入监控面板

本地spingboot配置Promethus+granfana监控-LMLPHP

 

 

04-26 15:38