Spring Boot 指标存储库

Springboot MetricRepository

我正在尝试使用

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.