您可以将指标用于Spring。这是一个github链接,它说明了如何将其与Spring MVC集成。metrics-spring模块将Dropwizard
Metrics库与Spring
集成在一起,并提供XML和Java配置。
马文
当前版本是3.1.2,与Metrics 3.1.2兼容
<dependency> <groupId>com.ryantenney.metrics</groupId> <artifactId>metrics-spring</artifactId> <version>3.1.2</version></dependency>
基本用法
从版本3开始,可以根据您的个人喜好使用XML或Java配置metrics-spring。
XML配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:metrics="http://www.ryantenney.com/schema/metrics" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.ryantenney.com/schema/metricshttp://www.ryantenney.com/schema/metrics/metrics.xsd"> <!-- Creates a MetricRegistry bean --> <metrics:metric-registry id="metricRegistry" /> <!-- Creates a HealthCheckRegistry bean (Optional) --> <metrics:health-check-registry id="health" /> <!-- Registers BeanPostProcessors with Spring which proxy beans and capture metrics --> <!-- Include this once per context (once in the parent context and in any subcontexts) --> <metrics:annotation-driven metric-registry="metricRegistry" /> <!-- Example reporter definiton. Supported reporters include jmx, slf4j, graphite, and others. --> <!-- Reporters should be defined only once, preferably in the parent context --> <metrics:reporter type="console" metric-registry="metricRegistry" period="1m" /> <!-- Register metric beans (Optional) --> <!-- The metrics in this example require metrics-jvm --> <metrics:register metric-registry="metricRegistry"> <bean metrics:name="jvm.gc" /> <bean metrics:name="jvm.memory" /> <bean metrics:name="jvm.thread-states" /> <bean metrics:name="jvm.fd.usage" /> </metrics:register> <!-- Beans and other Spring config --></beans>
Java配置:
import java.util.concurrent.TimeUnit;import org.springframework.context.annotation.Configuration;import com.codahale.metrics.ConsoleReporter;import com.codahale.metrics.MetricRegistry;import com.codahale.metrics.SharedMetricRegistries;import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;@Configuration@EnableMetricspublic class SpringConfiguringClass extends MetricsConfigurerAdapter { @Override public void configureReporters(MetricRegistry metricRegistry) { // registerReporter allows the MetricsConfigurerAdapter to // shut down the reporter when the Spring context is closed registerReporter(ConsoleReporter .forRegistry(metricRegistry) .build()) .start(1, TimeUnit.MINUTES); }}阅读更多有关Metrics Spring



