栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot集成prometheus和grafana

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot集成prometheus和grafana

ubuntu20.04系统中springboot集成prometheus和grafana

pom.xml 注意依赖引入版本,避免冲突


	 org.springframework.boot
	 spring-boot-starter-web


		org.springframework.boot
		spring-boot-starter-actuator


		io.micrometer
		micrometer-registry-prometheus

application-dev.yml

spring:
  application:
    name: springboot  #项目名
management:
  endpoints:
    web:
      exposure:
        include: 'prometheus'
  metrics:
    tags:
      application: ${spring.application.name}

启动项目 , 访问 http://localhost:8080/actuator/prometheus
如果下载这样的文件,那就没问题了:

开始下载安装 prometheus
下载地址:https://prometheus.io/download/
sudo tar zxvf prometheus-2.31.1.linux-amd64.tar.gz
修改配置文件:sudo vim prometheus.yml

global:
  scrape_interval:     15s # By default, scrape targets every 15 seconds.
  # Attach these labels to any time series or alerts when communicating with
  # external systems (federation, remote storage, alertmanager).
  external_labels:
    monitor: 'codelab-monitor'
# 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=` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'springboot'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['127.0.0.1:8080']

启动 sudo ./prometheus --config.file=prometheus.yml

Prometheus———Linux系统安装,配置后台启动方式
https://blog.csdn.net/qq_32415063/article/details/105607008

启动类:

@Bean
MeterRegistryCustomizer configurer(@Value("${spring.application.name}") String applicationName) {
    return (registry) -> registry.config().commonTags("application", applicationName);
}

项目中写了两个测试方法
请求执行接口(多请求几次)
http://localhost:8080/test

package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class PrometheusTestController {

    Logger logger = LoggerFactory.getLogger(PrometheusTestController.class);

    @GetMapping("/test")
    public String test() {
        logger.info("test");
        return "ok";
    }

    @GetMapping("")
    public String home() {
        logger.info("home");
        return "ok";
    }

}

然后访问:
http://localhost:9090/graph

第一步选择:graph
第二部选择:http_server_requests_seconds_count
第三步执行:Execute

这样prometheus就集成完毕了。
然后是安装grafana。
grafana-5.1.3-1.x86_64.rpm

访问http://localhost:3000/,初始密码:admin/admin
配置数据源:


这一步可以选择中间一个,也可以全选

https://grafana.com/grafana/dashboards/ 这个地址进行模板选择

点 + 号,选择import


这样模板就选择好了。
从home中选择模板

一下就是效果图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/530759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号