摘自我球的docs文档,我没时间在CSDN上再写一份,见
pom添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.0.26</version>
</dependency>
application.yml配置监控指标:
## prometheus监控配置
management:
security:
enabled: false
prometheus.yml配置:
global:
scrape_interval: 10s
scrape_timeout: 10s
evaluation_interval: 10m
scrape_configs:
- job_name: redis-manager
scrape_interval: 5s
scrape_timeout: 5s
metrics_path: /prometheus
scheme: http
# basic_auth:
# # 对应application.yml security配置
# username: admin
# password: 123456
static_configs:
- targets:
- 127.0.0.1:18182
-------------样例 效果--------------
grafana的dashboard json demo:
simpleclient_spring_boot-1568689162837.json
Prometheus监控效果:
inteceptor.java统计QPS
public class RequestCounterInterceptor extends HandlerInterceptorAdapter {
// @formatter:off
// Note (1)
private static final Counter requestTotal = Counter.build()
.name("http_requests_total")
.labelNames("method", "handler", "status")
.help("Http Request Total").register();
// @formatter:on
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e)
throws Exception {
// Update counters
String handlerLabel = handler.toString();
// get short form of handler method name
if (handler instanceof HandlerMethod) {
Method method = ((HandlerMethod) handler).getMethod();
handlerLabel = method.getDeclaringClass().getSimpleName() + "." + method.getName();
}
// Note (2)
requestTotal.labels(request.getMethod(), handlerLabel, Integer.toString(response.getStatus())).inc();
}
}
知道三个维度就行。