Prometheus
Prometheus是一个根据应用的metrics来进行监控的开源工具。Prometheus是一个开源的完整监控解决方案,其对传统监控系统的测试和告警模型进行了彻底的颠覆,形成了基于中央化的规则计算、统一分析和告警的新模型。
SpringBoot集成Prometheus
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>prometheus-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.10.0</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.10.0</version>
</dependency>
<!--Exposition servlet -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置启动类
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
最后启动服务,访问浏览器127.0.0.1:8080/metrics就可以看到一系列的metrics信息(json的数据格式)
新增配置类
@Configuration
public class PrometheusConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean(){
DefaultExports.initialize();
return new ServletRegistrationBean(new MetricsServlet(), "/metrics");
}
}
启动服务,访问浏览器127.0.0.1:8080/metrics。
自定义监控项
@RestController
public class SampleController {
private static Random random = new Random();
private static final Counter requestTotal = Counter.build()
.name("my_sample_counter")
.labelNames("status")
.help("A simple Counter to illustrate custom Counters in Spring Boot and Prometheus").register();
@RequestMapping("/endpoint")
public void endpoint() {
if (random.nextInt(2) > 0) {
requestTotal.labels("success: "+ random.nextInt(100)).inc();
} else {
requestTotal.labels("error: "+ random.nextInt(100)).inc();
}
}
}
配置文件
这个配置文件我就试过了几个,等我后面在试试在备注下。
# actuator暴露接口的前缀
management:
context-path: /admin
# actuator暴露接口使用的端口,为了和api接口使用的端口进行分离
port: 9194
security:
enabled: true
roles: SUPERUSER
spring.application.name=mydemo
server.port=8080
management.security.enabled=false
management.metrics.export.prometheus.enabled = true
management.metrics.export.prometheus.step = 1m
management.metrics.export.prometheus.descriptions = true
management.web.server.auto-time-requests = true
management.endpoints.web.exposure.include=prometheus
#开启prometheus的数据推送模式
management.metrics.export.prometheus.pushgateway.enabled=true
#prometheus服务端地址
#management.metrics.export.prometheus.pushgateway.base-url=localhost:9091
#推送数据的频率,默认1m(单位分钟)
management.metrics.export.prometheus.pushgateway.push-rate=1m
#在jvm关闭之前将数据推送出去
management.metrics.export.prometheus.pushgateway.shutdown-operation=push
management.endpoints.web.base-path=/actuator
#应用可视化监控
spring.metrics.servo.enabled=false