如何用spring-boot显示Dropwizard Metrics Servlet?

时间:2015-07-28 13:01:36

标签: java spring spring-boot metrics dropwizard

我使用 properties{ minIdle = 5 maxIdle = 25 maxWait = 10000 timeBetweenEvictionRunsMillis = 5000 minEvictableIdleTimeMillis = 60000 validationQuery = "SELECT 1" validationQueryTimeout = 3 testOnBorrow = true testWhileIdle = true testOnReturn = false defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED } 获取spring-boot-starter-actuator端点。

现在我还想使用localhost/metricsdropwizard.metrics依赖项。在他们的网页(https://dropwizard.github.io/metrics/3.1.0/getting-started/)上声明,metrics-servlets带有某种指标管理菜单,会创建健康,线程转储和ping。

但我没有看到那个servlet。我是否可能必须在spring-boot中明确注册?

2 个答案:

答案 0 :(得分:5)

我必须显式地实例化servlet并提供servlet映射路径,如下所示:

@Bean
public ServletRegistrationBean servletRegistrationBean(){
    return new ServletRegistrationBean(new AdminServlet(),"/metrics/admin/*");
}

答案 1 :(得分:1)

如果有人正在使用5.0.0版,则必须执行以下步骤才能使其正常工作:

@Inject
private ServletContext servletContext;

@Inject
private MetricRegistry metricRegistry;

@Inject
private HealthCheckRegistry healthCheckRegistry;

@Bean
public ServletRegistrationBean<Servlet> servletRegistrationBean(){
    servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY,
        metricRegistry);
    servletContext.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY,
        healthCheckRegistry);

    return new ServletRegistrationBean<>(new AdminServlet(), "/metrics/*");
}

来源:https://stackoverflow.com/a/41382649/1047418