ServletRegistrationBean

ServletRegistrationBean

我想监视一些应用程序,并且正在使用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)未定义

最佳答案

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>

10-04 17:22