You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.1.9.RELEASE中找不到匹配的CachePublicMetrics类求助

Fixing Missing Classes When Upgrading to Spring Boot 2.1.9.RELEASE

Hey there, let's tackle this issue you're facing when moving from Spring Boot 1.5.x to 2.1.x. The CachePublicMetrics and Metric classes you're using were part of the older Actuator metrics system, which got a complete overhaul in Spring Boot 2.x (switched to using Micrometer as the underlying metrics library). Here's how to update your code:

Key Changes to Understand

  • org.springframework.boot.actuate.endpoint.CachePublicMetrics: This class was removed in Spring Boot 2.x. Cache metrics are now automatically registered with the Micrometer MeterRegistry when you use Spring Cache (e.g., Redis, Caffeine).
  • org.springframework.boot.actuate.metrics.Metric: This class was replaced by Micrometer's io.micrometer.core.instrument.Metric (though you'll work more with Meter and Measurement in practice).

Updated Code Example

First, update your imports to use Micrometer classes:

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metric;
import io.micrometer.core.instrument.search.Search;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import com.google.common.collect.Maps; // Assuming you're using Guava's Maps utility

Then modify your controller to inject MeterRegistry instead of CachePublicMetrics, and adjust the logic to fetch metrics using Micrometer's API:

public class CachingController extends CloudRestTemplate {

    @Autowired
    private MeterRegistry meterRegistry; // Replace CachePublicMetrics with this

    public @ResponseBody Map<String, Object> getData(
            @Pattern(regexp=Constants.STRING_VALID_PATTERN, message=Constants.STRING_INVALID_MSG) 
            @PathVariable(required = true) final String name) throws Exception {
        
        boolean success = false;
        Map<String, Object> m = Maps.newHashMap();
        Collection<Metric<?>> resp = new ArrayList<>();

        // Search for metrics related to the target cache name
        // Option 1: Filter by name containing the cache name
        Search.in(meterRegistry).nameContains(name).meters().forEach(meter -> {
            meter.measure().forEach(measurement -> {
                String metricFullName = String.format("%s.%s", meter.getId().getName(), measurement.getStatistic().getName());
                resp.add(Metric.of(metricFullName, measurement.getValue()));
                logger.debug("{}: {}", metricFullName, measurement.getValue());
            });
        });

        // Option 2 (more precise): Filter by the "cache" tag (recommended)
        // Cache metrics have a tag named "cache" with the cache's name
        // Search.in(meterRegistry).tag("cache", name).meters().forEach(meter -> {
        //     meter.measure().forEach(measurement -> {
        //         String metricFullName = String.format("%s.%s", meter.getId().getName(), measurement.getStatistic().getName());
        //         resp.add(Metric.of(metricFullName, measurement.getValue()));
        //         logger.debug("{}: {}", metricFullName, measurement.getValue());
        //     });
        // });

        success = true;
        m.put("success", success);
        m.put("metrics", resp);
        return m;
    }
}

Additional Notes

  • Ensure Dependencies: Make sure your pom.xml (or build.gradle) includes the spring-boot-starter-actuator dependency (it should bring in Micrometer automatically for Spring Boot 2.x).
  • Cache Metrics Auto-Configuration: Spring Boot 2.x automatically enables cache metrics if you have a cache provider configured (like spring-boot-starter-cache). You don't need to manually register beans like CachePublicMetrics anymore.
  • Metric Naming: Micrometer uses a consistent naming convention for cache metrics (e.g., cache.gets, cache.puts, cache.hits, cache.misses) with tags for cache (cache name) and result (success/failure for operations).

内容的提问来源于stack exchange,提问作者Rajesh Rajoji

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:14:09