【问题标题】:spring boot repository metricsSpring Boot 存储库指标
【发布时间】:2015-05-01 11:19:52
【问题描述】:

我正在尝试在 SpringBoot 中创建一个对象 repositoryMetrics,后来我通过这个存储库获得了信息。但我创建了一个存储库,它始终为空。

如何生成存储库并保存指标? 我有这个代码:

public static void stateSist() throws Exception {

    InMemoryMetricRepository metricRepository = null;
    metricRepository.findAll();
    System.out.println(metricRepository.count());

}

编辑 1:

我在我的版本中做了这个改变,我得到了这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actuatorMetricsPrinter' defined in file [ActuatorMetricsPrinter.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Collection]: : No qualifying bean of type [org.springframework.boot.actuate.endpoint.PublicMetrics] found for dependency [collection of org.springframework.boot.actuate.endpoint.PublicMetrics]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.actuate.endpoint.PublicMetrics] found for dependency [collection of org.springframework.boot.actuate.endpoint.PublicMetrics]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1115)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at hello.Application.main(Application.java:14)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.actuate.endpoint.PublicMetrics] found for dependency [collection of org.springframework.boot.actuate.endpoint.PublicMetrics]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1118)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:919)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:811)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:739)
... 18 common frames omitted

谢谢!

【问题讨论】:

  • Herzo,您粘贴的代码完全没有任何作用。您需要向我们提供有关您的环境的更多信息 - 您是否使用 Spring Data - 如果是,则使用哪个模块(JPA、Mongo 等)。请粘贴您对 Spring Boot 应用程序的配置。没有它,我们无法为您提供帮助。
  • @R4J 我使用弹簧启动执行器,我想知道如何填充这个存储库。我没有在网上看到这个例子。
  • 如果您使用的是 Actuator,则该存储库已经创建。您需要做的就是将其注入您的组件并操作内容。见 Github 上的代码:github.com/spring-projects/spring-boot/blob/master/…
  • @R4J 但我不明白存储库何时填满?执行应用时自动吗?
  • Actuator 从您启动应用程序的那一刻起就开始工作并记录许多不同的统计信息 (docs.spring.io/spring-boot/docs/current/reference/html/…)。

标签: spring spring-boot metrics


【解决方案1】:

为了访问 Spring Boot 执行器框架打印的所有公共指标,您需要将“集合”注入到您的组件中,然后访问它以读取所有指标。这是一个例子:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.actuate.endpoint.PublicMetrics;
    import org.springframework.boot.actuate.metrics.Metric;
    import org.springframework.stereotype.Component;

    import java.util.Collection;

    @Component
    public class ActuatorMetricsPrinter {
        private static final String TEMPLATE = "Metric: %s [%s]";

        private final Collection<PublicMetrics> publicMetrics;

        @Autowired
        public ActuatorMetricsPrinter(Collection<PublicMetrics> publicMetrics) {
            this.publicMetrics = publicMetrics;
        }

        public String printAllMetrics() {

            StringBuilder sb = new StringBuilder();

            for (PublicMetrics pm : publicMetrics) {
                sb.append("Public Metric: " + pm.getClass().getName());
                sb.append("\n\n");

                for (Metric<?> m : pm.metrics()) {
                    sb.append(String.format(TEMPLATE, m.getName(), m.getValue().toString()));
                    sb.append("\n");
                }
            }

            return sb.toString();
        }
    }

如果你想重现这个简单的场景,那么:

  1. 转到 http://start.spring.io/ 并创建新项目,并检查 Actuator 和 Web,
  2. 使用您选择的构建工具(Gradle、Maven)创建一个演示项目,
  3. 下载并在您的 IDE 中打开,
  4. 创建一个新组件,就像我的示例中的一样,
  5. 创建一个新的 RestController,如下所示:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DummyController {

    private final ActuatorMetricsPrinter metricsPrinter;

    @Autowired
    public DummyController(ActuatorMetricsPrinter metricsPrinter) {
        this.metricsPrinter = metricsPrinter;
    }

    @RequestMapping(value = "/customMetrics", method = RequestMethod.GET)
    public String printMetrics() {
        return metricsPrinter.printAllMetrics();
    }
}
  1. 然后启动应用程序并在浏览器中输入:http://localhost:8080/customMetrics
  2. 你应该看到你的班级以最混乱的方式打印的所有指标

【讨论】:

  • 您确定执行器加载正确吗?你能看到他们@localhost:8080/metrics 吗?
  • 问题是编译时给我这个错误
  • 是运行时异常,不是编译错误。你完全按照我写的那样做吗?
  • 是的,不同的是你告诉我生成的spring版本和我拥有的版本。
【解决方案2】:

如果您使用 Maven 或 Gradle 进行依赖管理,那么您必须确保它们存在于其中(来自Spring Clouds's Home Page):

Maven

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Angel.SR4</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

Gradle

buildscript {
  dependencies {
    classpath "io.spring.gradle:dependency-management-plugin:0.4.0.RELEASE"
  }
}

apply plugin: "io.spring.dependency-management"

dependencyManagement {
  imports {
    mavenBom 'org.springframework.cloud:spring-cloud-starter-parent:Angel.SR4'
  }
}

dependencies {
    compile 'org.springframework.cloud:spring-cloud-starter-config'
    compile 'org.springframework.cloud:spring-cloud-starter-eureka'
}

如果您在 Maven 中省略 parent 或在 gradle 中省略 dependencyManagement,并尝试仅导入每个依赖项的最新版本,您将收到您所描述的运行时错误。

类似的问题解决了 GitHub 上的 here

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2018-08-06
    • 2021-04-16
    • 2016-12-13
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多