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 MicrometerMeterRegistrywhen you use Spring Cache (e.g., Redis, Caffeine).org.springframework.boot.actuate.metrics.Metric: This class was replaced by Micrometer'sio.micrometer.core.instrument.Metric(though you'll work more withMeterandMeasurementin 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(orbuild.gradle) includes thespring-boot-starter-actuatordependency (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 likeCachePublicMetricsanymore. - Metric Naming: Micrometer uses a consistent naming convention for cache metrics (e.g.,
cache.gets,cache.puts,cache.hits,cache.misses) with tags forcache(cache name) andresult(success/failure for operations).
内容的提问来源于stack exchange,提问作者Rajesh Rajoji
相关产品推荐
相关产品推荐

