簡體   English   中英

Spring Boot 存儲庫指標

[英]spring boot repository metrics

我正在嘗試在 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

謝謝!

為了訪問 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. 您應該以最混亂的方式查看您的班級打印的所有指標

如果您使用 Maven 或 Gradle 進行依賴管理,那么您必須確保它們存在於其中之一(來自Spring Clouds 的主頁):

馬文

<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>

搖籃

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'
}

如果省略parent Maven中或dependencyManagement在gradle產出,並嘗試只需導入最新版本的每個的依賴關系的,你會得到運行時錯誤,你所描述。

類似的問題也解決了這個位置在GitHub上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM