springboot集成Prometheus+Grafana+Micrometer监控JVM

目录

前提

引入pom依赖

yml配置

启动类中追加MeterRegistryCustomizer配置

测试端点

部署项目并配置prometheus

引入JVM(Micrometer)


前提

在此之前,需要先安装好prometheus、grafana,这些在上一篇已经配置好,参考如下:Linux(阿里云服务器)安装Prometheus、node_exporter、Grafana集成node exporter面板

引入pom依赖

<dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

yml配置

server:
  port: 8090
spring:
  application:
    name: prometheus-demo
management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"
  metrics:
    tags:
      application: ${spring.application.name}

启动类中追加MeterRegistryCustomizer配置

@SpringBootApplication
public class PrometheusApplication {

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

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer() {
        return registry -> registry.config().commonTags("application", "prometheus-demo");
    }

}

测试端点

为了方便一会查看效果,新建一个测试类,开放一个访问端点:

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping(path = "/hello")
    public String hello() {

        return "hello :" + new Random().nextInt(10);
    }

}

部署项目并配置prometheus

我们将项目打包放到服务器中启动后,并在prometheus.yml中追加如下配置对此项目进行监控:

  - job_name: "prometheus-demo"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ["localhost:8090"]   

此时完整配置如下:

# 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: node
    static_configs:
      - targets: ['localhost:9100']
      
  - job_name: "prometheus-demo"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ["localhost:8090"]       

重启prometheus后,访问 http://your ip:9090/targets ,界面中会出现刚才我们的测试程序:

 

引入JVM(Micrometer)

在grafana中导入Micrometer,https://grafana.com/grafana/dashboards页面中可看到Micrometer的ID为4701,我们直接load4701即可引入监控页面。

我们这 

如果使用ID无法load,可以在官网下载json文件后,将json文件的内容复制到panel json中进行加载:

下面的Name自定义一下,选择我们的Prometheus后点击Import进行监控页面的加载呈现:

 

我们频繁访问 /test/hello 端点,再查看监控页面,就可以看到监控画面的变化:

​​​​​​​

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值