【发布时间】:2021-09-06 10:16:53
【问题描述】:
我正在使用 prometheus 库来获取我的 Spring Boot 应用程序 (REST API) 的指标。我正在使用库 io.prometheus.simpleclient:0.4.0 并将它包含在我的 Maven pom.xml 中。我正在使用Counter 和@Autowiring(我已经尝试过字段和构造函数注入)它到我自己的类之一,就像这样
MyCustomMetricsClass.java
@Component
public MyCustomMetricsClass {
@Autowire
private Counter counterBean;
public void myOwnMetricsMethod() {
counterBean.inc();
// do some stuff
}
那么,我正在 @Autowiring 这个MyCustomMetricsClass 到我的服务类 MyServiceClass.java 中,当我使用 Spring 在本地运行我的 API 时它似乎运行良好在端口 8080 (localhost:8080) 上启动嵌入式 tomcat。我可以点击端点,并且在执行器端点(localhost:8080/actuator/metrics)上正确报告了指标。例如
MyServiceClass.java
public MyServiceClass {
@Autowire
private MyCustomMetricsclass myMetrics;
public void genericServiceMethod() {
myMetrics.MyOwnMetricsMethod(); // NULL POINTER EXCEPTION ONLY DURING TEST SCOPE (GROOVY)
}
问题是,当我运行mvn install(触发我编写的本地 Groovy 单元测试)时,我不断收到 NULL POINTER EXCEPTION。使用调试器,我可以调试 Groovy 单元测试,并在我的 Service 类中看到 myMetrics 变量为 NULL。但是我不明白为什么它在运行时可以正常工作,而且我已经将MyCustomMetricsClass 注释为 @Component 注释,所以它应该是一个被 Spring Component scan 扫描的 bean。
这是一个多模块项目;结构如下
my-project (root, contains root pom.xml)
- my-api (module, contains RestController. has its own pom.xml)
- my-service (module. contains service classes, has its own pom.xml)
- my-model (module, contains all POJO/DTO model classes, has its own pom.xml)
我是否缺少对类路径的依赖?为什么它在运行时有效,但在测试期间无效? (我所有的依赖项都应该有默认范围)自动装配被破坏了吗?
【问题讨论】:
标签: spring-boot prometheus grafana promql