问题
I am using following classes in one of controllers of (spring-boot.1.5.12 release)
I am unable to find matching classes in spring 2.1.9 release.
The following is the code snippet
import org.springframework.boot.actuate.endpoint.CachePublicMetrics;
import org.springframework.boot.actuate.metrics.Metric;
public class CachingController extends CloudRestTemplate {
@Autowired
private CachePublicMetrics metrics;
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<>();
Collection<Metric<?>> mets = metrics.metrics();
for (Iterator<Metric<?>> iterator = mets.iterator(); iterator.hasNext();) {
Metric<?> met = iterator.next();
String metName = met.getName();
logger.debug(metName+":"+met.getValue());
if(StringUtils.isNotEmpty(metName)
&& metName.indexOf(name) != -1 ){
resp.add(met);
}
}
}
回答1:
I think you should take a deeper look into Spring boot actuator, once you expose your all endpoints you might find what you are looking for. Spring Boot provides bunch of pre-defined endpoints, below is a list of Spring boot actuator endpoints (source)
- /auditevents: Exposes audit events information for the current application.
- /beans: Returns list of all spring beans in the application.
- /caches: Gives information about the available caches.
- /health: Provides applications health information.
- /conditions: Provides list of conditions those were evaluated during auto configurations.
- /configprops: Returns list of application level properties.
- /info: Provides information about current application. This info can be configured in a properties file.
- /loggers: Displays logging configurations. Moreover, this endpoint can be used to modify the configurations.
- /headdump: Produces a head dump file and returns it.
- /metrics: Returns various metrics about the application. Includes memory, heap and threads info. However, this endpoint doesn’t return any metrics. While, it only returns list of available metrics, the
metrics names can be used in a separate request to fetch the respective details. For instance, /actuator/metrics/jvm.memory.max like this.- /scheduledtasks: Returns a list of Scheduled tasks in the application.
- /httptrace: Returns last 100 http interactions in the form of request and response. Including, the actuator endpoints.
- /mappings: List of all Http Request Mappings. Also includes the actuator endpoints.
Edit:
As discussed in comments, you need to access /actuator/metrics/jvm.memory.max , you can invoke same using RestTemplate if you want to access using Java, you need to explore Actuator APIs, I wrote a quick program, you can refer the same
@Autowired
private MetricsEndpoint metricsEndpoint;
public MetricResponse printJavaMaxMemMetrics() {
ListNamesResponse listNames = metricsEndpoint.listNames();
listNames.getNames().stream().forEach(name -> System.out.println(name));
MetricResponse metric = metricsEndpoint.metric("jvm.memory.max", new ArrayList<>());
System.out.println("metric (jvm.memory.max) ->" + metric);
List<AvailableTag> availableTags = metric.getAvailableTags();
availableTags.forEach(tag -> System.out.println(tag.getTag() + " : " + tag.getValues()));
List<Sample> measurements = metric.getMeasurements();
measurements.forEach(sample -> System.out.println(sample.getStatistic() + " : " + sample.getValue()));
return metric;
}
来源:https://stackoverflow.com/questions/58305840/looking-for-matched-cachepublicmetrics-in-spring-boot-2-1-9-release