Prometheus监控SpringBoot
参考:
完整demoPrometheus + Grafana 监控 SpringBoot,
直接使用: 使用Prometheus监控SpringBoot应用,
Spring Boot Actuator 实现应用监控,
SpringBoot 实现自定义指标监控,
SpringBoot2.0 Actuator 监控参数说明
基础监控
SpringBoot项目
-
添加依赖,使用Prometheus监控SpringBoot应用只需要在pom文件添加如下两个依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
-
修改application.properties或者application.yml文件,对外暴露监控端点
spring.application.name = blog management.endpoints.web.exposure.include = prometheus management.metrics.tags.application = ${spring.application.name} # 不允许shutdown management.endpoint.shutdown.enabled=false
-
启动应用
nohup java -jar -Xms512m -Xmx512m demo-1.0-SNAPSHOT.jar --server.port=9090 > /tmp/demo.log 2>&1 &
访问http://localhost:9090/actuator/查看开放端点
prometheus
- job_name: 'spring' # 可随意指定
scrape_interval: 15s # 多久采集一次数据
scrape_timeout: 10s # 采集时的超时时间
metrics_path: '/actuator/prometheus' # 采集的路径
static_configs: # 采集服务的地址,设置成Springboot应用所在服务器的具体地址
- targets: ['locathost:9090']
# 添加完后,需要重启prometheus, 然后登陆web: http://x.x.x.x:9090/targets
grafana
-
添加模板: JVM (Micrometer), 搜索
-
导入模板
自定义业务指标
添加springBoot指标
- 添加业务监控指标
在 spring-web-prometheus-demo 项目的基础上,我们添加一个 PrometheusCustomMonitor 类。在这里面我们定义了三个业务指标:
- order_request_count:下单总次数
- order_amount_sum:下单总金额
@Component
public class PrometheusCustomMonitor {
/**
* 订单发起次数
*/
private Counter orderCount;
/**
* 金额统计
*/
private DistributionSummary amountSum;
private final MeterRegistry registry;
@Autowired
public PrometheusCustomMonitor(MeterRegistry registry) {
this.registry = registry;
}
@PostConstruct
private void init() {
orderCount = registry.counter("order_request_count", "order", "test-svc");
amountSum = registry.summary("order_amount_sum", "orderAmount", "test-svc");
}
public Counter getOrderCount() {
return orderCount;
}
public DistributionSummary getAmountSum() {
return amountSum;
}
}
-
模拟订单数据
这里我们新增一个 TestController 类,去模拟现实的订单数据。 后续应用启动后,我们可以通过 localhost:8080/order 去模拟用户下单操作。 package com.chenshuyi.springwebprometheusdemo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.Random; @RestController public class TestController { @Resource private PrometheusCustomMonitor monitor; @RequestMapping("/order") public String order() throws Exception { // 统计下单次数 monitor.getOrderCount().increment(); Random random = new Random(); int amount = random.nextInt(100); // 统计金额 monitor.getAmountSum().record(amount); return "下单成功, 金额: " + amount; } }
-
启动项目测试
现在我们启动应用,访问 localhost:8080/order 可以成功模拟下单,每次都会有一个随机的订单金额产生。 页面会返回这种效果 下单成功, 金额: 90
-
查看指标及源码对应关系
此时我们访问 localhost:8080/actuator/prometheus 就可以看到对应的指标已经存在。
grafana配置
订单个数与总额
还是在原有的面板上进行添加操作,接着在「图表设置区」的「Display」中的 Value 设置为「Last」,表示其值是取最后一个数值(因为这个数值是已经统计好了的)。Fields 设置为「Numeric Fields」,表示其是一个数值字段。
配置订单总额图表与订单个数是一样的配置
订单个数与总额增长率
Metrics: rate(order_amount_sum_count [1m])
legend: {{instance}}
arex--> left Y --> unit: Percent(0.0-1.0)
最后查看自定义指标
多访问几次: 图表就会产生变化了
http://192.168.0.163:9090/order
预警
# 根据需求自定义