【问题标题】:Can't get Prometheus to work with Spring Boot 2.0.3无法让 Prometheus 与 Spring Boot 2.0.3 一起使用
【发布时间】:2018-07-24 10:45:11
【问题描述】:

我正在使用具有以下依赖项的 Spring Boot 2.0.3.RELEASE:

spring-boot-starter-actuator:2.0.3.RELEASE
micrometer-core:1.0.6
micrometer-registry-prometheus:1.0.6

但是当我调用普罗米修斯时,我得到的只是

{
    "timestamp": 1532426317772,
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/actuator/prometheus"
}

*and/or from browser*
There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

我还使用 Spring Boot 2 尝试了 1.0.X 范围的 Prometheus 以前的版本,但没有运气。有人可以提出一些见解吗?非常感谢。

【问题讨论】:

  • 您在项目中使用的是@EnableWebFlux 还是@EnableWebMvc?尝试使用

标签: spring-boot prometheus


【解决方案1】:

我们最近遇到了同样的问题。在调试 Spring Boot 时,我发现我们没有注册可以处理“text/plain”媒体类型的 HttpMessageConverter,只有“application/json”。因为 Prometheus 端点返回“text/plain” MediaType,我们添加了一些配置作为临时解决方法。我们添加了一个链接到特定 MediaType 的 StringHttpMessageConverter。

@Configuration
public class ApplicationConfiguration implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_PLAIN));
        converters.add(converter);
    }

}

希望对你有帮助

【讨论】:

  • 这对我也有帮助。但不要忘记@EnableWebMvc 注释。
  • 我的 rest-api 应用程序遇到了同样的问题,@EnableWebMvc 解决了它。谢谢!
  • UPD:实际上,添加注释还会破坏我的旧应用程序中的其他功能(与 CROS 相关),因此我已将其删除并使用手动添加 StringHttpMessageConverter(如上所示)跨度>
【解决方案2】:

可能是 406 状态码,客户端也可以通过指定的接受头来解决。 像这样:curl -v -H "Accept: text/plain" url

【讨论】:

  • 好电话!我正在使用 Postman 来点击它,并且只有标头 Accept: application/json.在我将该标题更改为“application/json,text/plain”之前,它正在返回“找不到可接受的表示”(虽然不是 406)。谢谢!
【解决方案3】:

此错误的另一个原因是包含以下内容的无效 Spring XML 配置文件:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </list>
    </property>
</bean>

上面的 XML 覆盖了 RequestMappingHandlerAdapter 中的 messageConverters 列表,因此只有列出的转换器可用,在我的情况下,当请求 /actuator/prometheus 时,它删除了导致 406 错误响应的字符串转换器。

【讨论】:

    【解决方案4】:

    可能不是一个明确的答案,但希望这是一些帮助。 我正在将 prometheus 集成到我没有开发的服务中并且遇到了同样的问题。我把它缩小到这个代码块(删除使它工作)

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        converter.setGson(GSON);
        converters.add(converter);
    }
    

    因此,检查您的转换方法可能会有所帮助。我所做的回购是使用杰克逊而不是 gson 来完成这类事情,所以这就是我要采取的路线。不是一个理想的解决方案,但缺乏更好的......

    【讨论】:

      【解决方案5】:

      如果其他答案没有帮助,可以将配置添加到 ContentNegotiationConfigurer。您可以将 defaultContentType 设置为“MediaType.TEXT_PLAIN”。

      @Configuration
      public class MvcConfig extends WebMvcConfigurerAdapter {
         
          @Override
          public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
              configurer.defaultContentType(MediaType.TEXT_PLAIN);
              configurer.favorParameter(false);
              configurer.favorPathExtension(false);
              configurer.parameterName("mediaType");
              configurer.useJaf(false);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-05
        • 2020-03-09
        • 2018-10-23
        • 1970-01-01
        • 1970-01-01
        • 2011-10-16
        • 1970-01-01
        • 2014-10-21
        • 2015-07-06
        相关资源
        最近更新 更多