[ Spring ] Integrate Spring Boot Service Monitor Prometheus and Grafana

文章目录

          • [Install Prometheus Server](#Install Prometheus Server)
          • [Install Grafana Server](#Install Grafana Server)
          • [Spring Prometheus Project](#Spring Prometheus Project)
          • [Spring Prometheus Properties](#Spring Prometheus Properties)
          • [Spring Prometheus Application](#Spring Prometheus Application)
          • [Spring Prometheus Configuration](#Spring Prometheus Configuration)
          • [Spring Prometheus Controller](#Spring Prometheus Controller)
          • [Configure Grafana Dash Board](#Configure Grafana Dash Board)
Install Prometheus Server
properties 复制代码
# download package
https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz
properties 复制代码
# edit prometheus config
prometheus-linux-amd64/prometheus.yml
yaml 复制代码
scrape_configs:
  - job_name: "prometheus"
    metrics_path: '/actuator/prometheus'
    scheme: 'http'
    static_configs:
      - targets: ["localhost:10003"]
properties 复制代码
# run server
sudo ./prometheus
properties 复制代码
# visit admin page
http://localhost:9090
Install Grafana Server
properties 复制代码
# download package
https://dl.grafana.com/enterprise/release/grafana-enterprise-11.5.0.linux-amd64.tar.gz
properties 复制代码
# run server
sudo bin/grafana server
properties 复制代码
# visit admin page
http://localhost:3000 admin/admin
Spring Prometheus Project
kotlin 复制代码
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
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
kotlin 复制代码
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
kotlin 复制代码
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
kotlin 复制代码
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
properties 复制代码
# add data source
Connections -> Data Sources -> Add Data Source -> Prometheus
settings.name=prometheus
connection.url=http://localhost:9090
properties 复制代码
# create dash board
Dashboards -> Create Dashboard -> Add Visualization -> Add Queries -> Run Queries
datasource=prometheus
metrics=count_api_calling_times_total
相关推荐
川石课堂软件测试1 天前
Oracle 数据库:视图与索引
数据库·网络协议·nginx·http·oracle·grafana·prometheus
一个松1 天前
【无标题】
spring boot
川石课堂软件测试1 天前
Oracle 数据库如何查询列
linux·数据库·sql·功能测试·oracle·grafana·prometheus
叫我阿柒啊1 天前
从Java全栈到前端框架:一次真实的面试对话
java·spring boot·微服务·前端框架·vue3·全栈开发
齐 飞1 天前
SpringBoot实现国际化(多语言)配置
java·spring boot·后端
if时光重来1 天前
springboot项目使用websocket功能,使用了nginx反向代理后连接失败问题解决
spring boot·websocket·nginx
FrankYoou1 天前
Spring Boot + Spring MVC 项目结构
spring boot·spring·springmvc
神云瑟瑟1 天前
Spring Boot大文件分块上传(代码篇)
java·spring boot·大文件上传·分块上传
川石课堂软件测试1 天前
Oracle 数据库使用事务确保数据的安全
数据库·python·功能测试·docker·oracle·单元测试·prometheus
小趴菜不能喝1 天前
Spring boot3.x整合mybatis-plus踩坑记录
java·spring boot·mybatis