如何配置spring-boot千分尺以推入elasticsearch?

Posted

技术标签:

【中文标题】如何配置spring-boot千分尺以推入elasticsearch?【英文标题】:How to configure spring-boot micrometer to push into elasticsearch? 【发布时间】:2020-01-07 05:29:26 【问题描述】:

我有一个 spring-boot 2 应用程序,它公开了它的执行器端点。这些值,我想导出到现有的 elasticsearch 实例。因此我使用了以下内容:

pom.xml

...

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-elastic</artifactId>
            <version>1.2.0</version>
        </dependency>

        ...

    </dependencies>

application.yml

management:
  metrics:
    export:
      elastic:
        enabled: true
        host: http://192.168.23.43:9200/
        auto-create-index: true
        index: metrics
        step: 1m

在启动应用程序时,POST 会抛出以下异常:

2019-09-04 11:20:42.498  WARN 2902 --- [       Thread-3] i.m.c.instrument.push.PushMeterRegistry  : Unexpected exception thrown while publishing metrics for ElasticMeterRegistry

java.lang.RuntimeException: java.lang.IllegalArgumentException: Unexpected response body: "error":"Incorrect HTTP method for uri [/] and method [POST], allowed: [DELETE, GET, HEAD]","status":405
    at io.micrometer.elastic.ElasticMeterRegistry.determineMajorVersionIfNeeded(ElasticMeterRegistry.java:252) ~[micrometer-registry-elastic-1.2.0.jar:1.2.0]
    at io.micrometer.elastic.ElasticMeterRegistry.publish(ElasticMeterRegistry.java:194) ~[micrometer-registry-elastic-1.2.0.jar:1.2.0]
    at io.micrometer.core.instrument.push.PushMeterRegistry.publishSafely(PushMeterRegistry.java:48) ~[micrometer-core-1.1.4.jar:1.1.4]
    at io.micrometer.core.instrument.push.PushMeterRegistry.close(PushMeterRegistry.java:83) ~[micrometer-core-1.1.4.jar:1.1.4]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.invokeCustomDestroyMethod(DisposableBeanAdapter.java:337) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:271) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:571) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:543) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1034) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:504) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1027) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1057) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1026) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:945) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
Caused by: java.lang.IllegalArgumentException: Unexpected response body: "error":"Incorrect HTTP method for uri [/] and method [POST], allowed: [DELETE, GET, HEAD]","status":405
    at io.micrometer.elastic.ElasticMeterRegistry.getMajorVersion(ElasticMeterRegistry.java:260) ~[micrometer-registry-elastic-1.2.0.jar:1.2.0]
    at io.micrometer.elastic.ElasticMeterRegistry.determineMajorVersionIfNeeded(ElasticMeterRegistry.java:250) ~[micrometer-registry-elastic-1.2.0.jar:1.2.0]
    ... 17 common frames omitted

我确保 elasticsearch 是可访问的,但它试图针对/ 发布的事实让我感到困惑。我错过了什么?

【问题讨论】:

【参考方案1】:

由于某种原因,micrometer-registry-elastic 的版本导致了问题。降级到版本 1.1.4 后,一切都按预期工作。

【讨论】:

我遇到了与版本 1.4.1 相同的问题。我知道 Maven 的情况如何,但尽量不要指定 micrometer-registry-elastic 的版本。相反,将其留给 Spring 的依赖管理(删除为我修复它的版本,Spring 声明 1.3.6

以上是关于如何配置spring-boot千分尺以推入elasticsearch?的主要内容,如果未能解决你的问题,请参考以下文章

Spring-boot千分尺定时器()它是如何工作的?

如何使用千分尺指定我想在spring-boot中使用的指标的白名单

如何使用千分尺指定我想要在spring-boot中使用的度量标准的白名单

在不使用 spring-boot 执行器的情况下将来自 spring 应用程序的指标公开给 prometheus

如何在 spring-boot 配置中加载 application.yaml 配置以进行硒测试

如何在 Angular、spring-boot、maven 项目中配置项目以自动重新加载浏览器