当前位置: 首页 > article >正文

[ Spring ] Integrate Spring Boot Service Monitor Prometheus and Grafana

文章目录

          • Install Prometheus Server
          • Install Grafana Server
          • Spring Prometheus Project
          • Spring Prometheus Properties
          • Spring Prometheus Application
          • Spring Prometheus Configuration
          • Spring Prometheus Controller
          • Configure Grafana Dash Board

Install Prometheus Server
# download package
https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz
# edit prometheus config
prometheus-linux-amd64/prometheus.yml
scrape_configs:
  - job_name: "prometheus"
    metrics_path: '/actuator/prometheus'
    scheme: 'http'
    static_configs:
      - targets: ["localhost:10003"]
# run server
sudo ./prometheus
# visit admin page
http://localhost:9090
Install Grafana Server
# download package
https://dl.grafana.com/enterprise/release/grafana-enterprise-11.5.0.linux-amd64.tar.gz
# run server
sudo bin/grafana server
# visit admin page
http://localhost:3000 admin/admin
Spring Prometheus Project
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode = RepositoriesMode.PREFER_SETTINGS
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

plugins {
    id("org.jetbrains.kotlin.jvm") version "2.0.21" apply false
    id("org.jetbrains.kotlin.kapt") version "2.0.21" apply false
    id("org.jetbrains.kotlin.plugin.spring") version "2.0.21" apply false
    id("org.springframework.boot") version "3.4.1" apply false
}

include("prometheus-app")
Spring Prometheus Properties
# service
server.port=10003
spring.application.name=prometheus-app
spring.profiles.active=dev
spring.devtools.add-properties=false
management.endpoints.web.exposure.include=*
# prometheus
management.prometheus.metrics.export.enabled=true
Spring Prometheus Application
package x.spring.hello

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class PrometheusApplication

fun main(args: Array<String>) {
    runApplication<PrometheusApplication>(*args)
}
Spring Prometheus Configuration
package x.spring.hello.component

import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component

@Component
class MetricsProvider {

    @Bean(name = ["count"])
    fun getCounterMetrics(
        registry: MeterRegistry,
        @Value("\${spring.application.name}") name: String
    ): Counter {
        return registry.counter("count_api_calling_times", "application", name)
    }
}
Spring Prometheus Controller
package x.spring.hello.controller

import io.micrometer.core.instrument.Counter
import jakarta.annotation.Resource
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class MetricsController {

    @Resource(name = "count")
    private lateinit var counterMetrics: Counter

    @GetMapping("/count")
    fun count(): String {
        counterMetrics.increment()
        return "count_api_calling_times_total +1"
    }
}
Configure Grafana Dash Board
# add data source
Connections -> Data Sources -> Add Data Source -> Prometheus
settings.name=prometheus
connection.url=http://localhost:9090
# create dash board
Dashboards -> Create Dashboard -> Add Visualization -> Add Queries -> Run Queries
datasource=prometheus
metrics=count_api_calling_times_total

http://www.kler.cn/a/538628.html

相关文章:

  • A股level2高频数据分析20250205
  • 【WB 深度学习实验管理】使用 PyTorch Lightning 实现高效的图像分类实验跟踪
  • 网络安全:挑战、技术与未来发展
  • 求助DeepSeek帮我开发一个直线审批流程设计页面Vue2.0
  • 深入理解小波变换:信号处理的强大工具
  • 使用redis实现 令牌桶算法 漏桶算法
  • 12.8 LangChain Agent 的灵魂:ReAct 框架理论与实战解析
  • 深入理解并行与并发:C++/Python实例详解
  • 迅为RK3568开发板篇OpenHarmony实操HDF驱动控制LED-在产品中新增子系统
  • 链表专题-03
  • vmware虚拟机可以使用Windows的GPU吗
  • 【JavaScript】《JavaScript高级程序设计 (第4版) 》笔记-Chapter6-集合引用类型
  • git撤销上一次的提交
  • C# Winform怎么设计串口,客户端和相机控件界面显示
  • Win11下搭建Kafka环境
  • 2. UVM的基本概念和架构
  • 深度学习在医疗影像分析中的应用
  • [Meet DeepSeek] 如何顺畅使用DeepSeek?告别【服务器繁忙,请稍后再试。】
  • 前端【技术方案】浏览器兼容问题(含解决方案、CSS Hacks、条件注释、特性检测、Polyfill 等)
  • SQL写法:行行比较
  • 【R语言】数据分析
  • fatal:Authentication failed for “http....
  • 现在中国三大运营商各自使用的哪些band频段
  • 【Pycharm+Git+Gitlab】安装部署(粗糙版)
  • 电脑黑屏按什么键恢复?电脑黑屏的解决办法
  • [Redis] Redis分布式锁与常见面试题