【监控】Spring Boot+Prometheus+Grafana实现可视化监控
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MonitorConfig {
@Bean
public MeterRegistry prometheusMeterRegistry() {
return new PrometheusMeterRegistry(PrometheusMeterRegistry.config().commonTags("application", "myapp"));
}
@Bean
public JvmGcMetrics jvmGcMetrics() {
return new JvmGcMetrics();
}
@Bean
public JvmThreadMetrics jvmThreadMetrics() {
return new JvmThreadMetrics();
}
@Bean
public JvmMemoryMetrics jvmMemoryMetrics() {
return new JvmMemoryMetrics();
}
@Bean
public ProcessorMetrics processorMetrics() {
return new ProcessorMetrics();
}
}
这段代码定义了一个配置类MonitorConfig
,它创建了一个PrometheusMeterRegistry
,并注册了几个常用的JVM和处理器指标。这些指标可以通过Prometheus抓取后,通过Grafana进行可视化展示。这样,开发者可以实时监控应用程序的运行状态。
评论已关闭