1、SpringBoot项目配置
(1)maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
(2)application.properties
spring.application.name=springboot_business server.port=6002 management.endpoints.web.exposure.include=* management.metrics.tags.application=${spring.application.name}
(3)这里以【订单成功数量】、【订单失败数量】、【订单成功金额】、【订单失败金额】为例进行统计
import com.alibaba.fastjson.JSON; import com.danny.monitor.prometheus.business.bean.Order; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.DistributionSummary; import io.micrometer.core.instrument.MeterRegistry; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.math.RandomUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.math.BigDecimal; import java.time.LocalDateTime; @Slf4j @Service public class OrderServiceImpl implements OrderService { @Autowired private MeterRegistry registry; private Counter orderSuccessCounter; // 累计成功订单数量 private Counter orderFailedCounter; // 累计失败订单数量 private DistributionSummary orderSuccessSummary; // 成功订单金额 private DistributionSummary orderFailedSummary; // 失败订单金额 @PostConstruct private void init() { orderSuccessCounter = registry.counter("create_order_total", "create_order_total", "success"); orderFailedCounter = registry.counter("create_order_total", "create_order_total", "failed"); orderSuccessSummary = registry.summary("order_amount", "order_amount", "success"); orderFailedSummary = registry.summary("order_amount", "order_amount", "failed"); } // 创建订单 @Override public Order createOrder(BigDecimal orderAmount) { log.info("createOrder orderAmount:{}", orderAmount); Order order = null; try { if (orderAmount.compareTo(BigDecimal.valueOf(10)) < 0) { throw new Exception("金额小于10时,模拟订单失败的情况"); } order = new Order(); order.setOrderAmount(orderAmount); order.setOrderNo(RandomStringUtils.randomAlphabetic(32)); order.setOrderTime(LocalDateTime.now()); orderSuccessCounter.increment(); // 订单成功数量埋点数据 orderSuccessSummary.record(orderAmount.doubleValue()); // 订单成功金额埋点数据 } catch (Exception e) { log.info("createOrder error", e); orderFailedCounter.increment(); // 订单失败数量埋点数据 orderFailedSummary.record(orderAmount.doubleValue()); // 订单失败金额埋点数据 order = null; } finally { log.info("queryOrder result:{}", JSON.toJSONString(order)); return order; } } }
2、prometheus添加配置
修改prometheus.yml,添加SpringBoot项目的地址
scrape_configs: - job_name: 'springboot_business' scrape_interval: 10s scrape_timeout: 10s metrics_path: '/actuator/prometheus' static_configs: - targets: ['10.246.140.63:6002']
重启Prometheus后,启动SpringBoot项目并访问几次下单接口(目的是为了造点埋点数据),在 http://localhost:6002/actuator/prometheus 页面可以看到在上面 OrderServiceImpl 中添加的统计指标。
在Prometheus UI界面通过PromeSQL查询统计指标的数据:
(1)查询【创建订单数量】统计数据
(2)查询【创建订单金额】统计数据
以上数据在重启SpringBoot项目后,就会丢失。可以把counter、summary中的值在缓存中存一份,在启动的时候初始化counter设置初始值。
3、Grafana配置
(1)订单数量统计
在Grafana中新建一个Dashboard(Create -> Dashboard),在新的Dashboard中新建一个Pannel(Add a new pannel),Pannel标题为【订单数量】,数据源选择【Prometheus】,Metircs选择【create_order_total】(在上面代码中定义的统计订单数量的Counter的名称),Legend写【{undefined{create_order_total}}】(在上面代码中定义的统计订单数量的Counter的tag的key)。
保存
(2)订单金额统计
按照同样的方式添加统计订单金额的pannel,需要注意的是,DistributionSummary定义的指标,在prometheus中会加个后缀,比如上面定义的 DistributionSummary 的 name 为 “order_amount”,在prometheus收集时会有“order_amount_sum”、“order_amount_count”、“order_amount_max”多个指标,具体可以在 http://localhost:6002/actuator/prometheus 中查看。
(3)订单增长率统计
在Dashboard中新建一个Pannel(Add a new pannel),Pannel标题为【订单量增长率】,数据源选择【Prometheus】,Metircs写【rate(create_order_total [1m])】(表示1分钟内的订单量增长率),Legend写【{undefined{create_order_total}}】(在上面代码中定义的统计订单数量的Counter的tag的key),在Standard options -> Unit 中选择纵坐标的显示方式为 Percent(0.0-1.0),也就是百分比显示。
(4)同理可以按照(3)的方式配置订单金额增长率的Pannel