Prometheus ServletRegistrationBean在某些Workspace中不起作用

时间:2018-03-28 07:26:24

标签: java spring-boot prometheus

我想监控一些应用程序,我正在使用Prometheus。它在某些应用程序中运行良好,但当我尝试在另一个应用程序中实现它时,我遇到了一些问题。 它说:

构造函数ServletRegistrationBean(MetricsServlet,String)未定义

导致此问题的原因以及如何解决此问题。

这是我的班级。

import java.util.Collection;

import org.springframework.boot.actuate.endpoint.PublicMetrics;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import io.prometheus.client.spring.boot.SpringBootMetricsCollector;

@Configuration
public class MonitoringConfig {

    @Bean
    SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) {

        SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector(publicMetrics);
        springBootMetricsCollector.register();

        return springBootMetricsCollector;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        DefaultExports.initialize();
        return new ServletRegistrationBean(new MetricsServlet(), "/prometheus");
    }

}

依赖条件:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.moelholm/prometheus-spring-boot-starter -->
    <dependency>
        <groupId>com.moelholm</groupId>
        <artifactId>prometheus-spring-boot-starter</artifactId>
        <version>1.0.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient</artifactId>
        <version>0.0.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_hotspot -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_hotspot</artifactId>
        <version>0.0.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_spring_boot</artifactId>
        <version>0.0.25</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_servlet -->
    <dependency>
        <groupId>io.prometheus</groupId>
        <artifactId>simpleclient_servlet</artifactId>
        <version>0.0.25</version>
    </dependency>

在某些应用程序中它可以正常工作,但在其中一个堆叠着:

构造函数ServletRegistrationBean(MetricsServlet,String)未定义

1 个答案:

答案 0 :(得分:1)

MetricsServlet 应该实现 javax.servlet.Servlet 。确保项目/类路径中有该类(即 javax.servlet.Servlet )。 包含此类的库的maven依赖项是:

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>