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

prometheus监控SpringBoot应用

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

prometheus监控SpringBoot应用

1.依赖

			org.springframework.boot
			spring-boot-starter-actuator
		
		
			io.micrometer
			micrometer-registry-prometheus
			1.7.3
		
2.配置
server:
  port: 10200
  servlet:
    context-path: /
spring:
  application:
    name: lys-openstack
  devtools:
    remote:
      restart:
        enabled: true
  thymeleaf:
    cache: false
    prefix: classpath:/templates
    encoding: UTF-8
    suffix: .html
    mode: LEGACYHTML5

management:
  endpoints:
   web:
     exposure:
       include: "*"
  metrics:
    tags:
      application: lys-openstack
  endpoint:
    metrics:
      enabled: true
    prometheus:
      enabled: true
    info:
      enabled: true
3.修改启动类
package com.lys;

import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class AppApplication {

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

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

4.验证

http://localhost:10200/actuator/prometheus

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=` to any timeseries scraped from this config.
  - job_name: "prometheus"

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

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

    #通过配置file 获取target,这里以lys-web项目进行举例
  - job_name: 'lys-other'
    #metrics_path: /metrics
    file_sd_configs:
      - files:
         - host.json

  - job_name: 'container'
    static_configs:
      - targets: ['10.6.8.184:8080']  # 本地 cadvisor 访问地址

  - job_name: 'rds'
    metrics_path: /actuator/prometheus
    static_configs:
      - targets: ['10.6.9.35:10200']  # 本地 springboot 访问地址


参考

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.endpoints.enabling

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

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

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