【问题标题】:Prometheus ServletRegistrationBean does not work in some WorkspacePrometheus ServletRegistrationBean 在某些 Workspace 中不起作用
【发布时间】:2018-03-28 07:26:24
【问题描述】:

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

【问题讨论】:

    标签: java spring-boot prometheus


    【解决方案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>
    

    【讨论】:

    • 现在没有错误但我仍然无法获取指标,当我访问 localhost:8787/prometheus 时我找不到
    • 那么,maven 依赖解决了你的问题吗(即 构造函数 ServletRegistrationBean(MetricsServlet, String) 未定义)?
    • 是的,但我仍然无法获得指标。
    • 我明白了。如果是这样,我认为应该解决一个新问题。我也会尽力帮助你解决这个问题(如果可以的话)
    • 好的,谢谢:)
    猜你喜欢
    • 2015-09-22
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多