gpt4 book ai didi

spring-boot - Spring Boot 指标存储库

转载 作者:行者123 更新时间:2023-12-02 00:50:32 24 4
gpt4 key购买 nike

我正在尝试使用

org.springframework.boot.actuate.metrics.repository.MetricRepository

吐出指标

private final MetricRepository repository;

@Autowired
public MetricExporterService(MetricRepository repository) {
this.repository = repository;
}

但是因为我使用的是 Java 1.8,所以我得到了

Parameter 0 of constructor in com.example.actuator.MetricExporterService required a bean of type 'org.springframework.boot.actuate.metrics.repository.MetricRepository' that could not be found.
- Bean method 'actuatorMetricRepository' not loaded because @ConditionalOnJava (older than 1.8) found 1.8

有没有办法覆盖这种行为?

最佳答案

对于 java 8,您应该使用 CounterServiceGaugeServiceBufferMetricReader:

@Autowired
private BufferMetricReader metricReader;

@Autowired
private CounterService counterService;

@Autowired
private GaugeService gaugeService;

...

counterService.increment("metric1");
gaugeService.submit("metric2", 10d);
metricReader.findAll();

Spring Documentation :

The default implementation of GaugeService and CounterService provided by Spring Boot depends on the version of Java that you are using. With Java 8 (or better) the implementation switches to a high-performance version optimized for fast writes, backed by atomic in-memory buffers, rather than by the immutable but relatively expensive Metric type (counters are approximately 5 times faster and gauges approximately twice as fast as the repository-based implementations).

...

[Note] The old MetricRepository and its InMemoryMetricRepository implementation are not used by default if you are on Java 8 or if you are using Dropwizard metrics.

...

To record your own metrics inject a CounterService and/or GaugeService into your bean. The CounterService exposes increment, decrement and reset methods; the GaugeService provides a submit method.

关于spring-boot - Spring Boot 指标存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40314140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com