首页 > 解决方案 > 带有千分尺 Elasticsearch 注册表的 Spring Boot 仅索引空文档

问题描述

我有一个使用千分尺 Elasticsearch 注册表(使用 Elasticsearch 7.2.0)的简单 spring boot 2.1.7.RELEASE项目。该项目github 上可用。它只有两个类,看起来像这样

pom.xml具有以下依赖项:

<dependencies>
  <dependency>
    <artifactId>spring-boot-starter-web</artifactId>
    <groupId>org.springframework.boot</groupId>
  </dependency>

  <dependency>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <groupId>org.springframework.boot</groupId>
  </dependency>

  <dependency>
    <artifactId>micrometer-registry-elastic</artifactId>
    <groupId>io.micrometer</groupId>
  </dependency>
</dependencies>

和两个类: MicrometerElasticApplication

@SpringBootApplication
public class MicrometerElasticApplication {
  public static void main(final String[] args) {
    SpringApplication.run(MicrometerElasticApplication.class, args);
  }
}

测试控制器

@RestController
public class TestController {
  @ResponseStatus(HttpStatus.OK)
  @GetMapping("/test")
  public void sendMessage() {
    System.out.println("Received a test message");
  }
}

启动应用程序后,我可以在日志中看到

i.m.elastic.ElasticMeterRegistry         : publishing metrics to elastic every 1m

这意味着发送了指标,但是当我检查 Elasticsearch 中的索引时,我只能看到这样的文档

{
    "_index": "metrics-2019-08",
    "_type": "_doc",
    "_id": "nWuMdWwBxBoi4XILEHVK",
    "_score": 1.0
}

所以没有字段,只有文档元数据。即使在达到端点服务器时间之后,索引/test也没有任何变化。metrics

通过阅读此处的官方文档并检查此处的常见属性,我的理解Spring默认情况下将为 JVM、CPU ......甚至测量所有 MVC 请求的时间。现在,我没有得到任何这些,只是空文件。将该属性设置management.metrics.web.server.auto-time-requeststrue不会更改任何内容。

有人看到我在这里缺少什么吗?

更新

我在方法上设置了一个断点,ElasticMeterRegistry.publish发送到 Elaticsearch /_bulkAPI 的请求对我来说看起来不错

POST http://localhost:9200/metrics-2019-08/_bulk

{ "index" : {} }
{"@timestamp":"2019-08-09T10:49:18.826Z","name":"jvm_memory_max","type":"gauge","area":"heap","id":"PS Survivor Space","value":1.5204352E7}
{ "index" : {} }
{"@timestamp":"2019-08-09T10:49:18.826Z","name":"jvm_threads_states","type":"gauge","state":"terminated","value":0.0}
...

当我使用 Postman 发送此请求时,所有文档都保存为空文档,尽管 Elasticsearch 没有报告任何错误,但"errors": false在响应中

{
    "took": 8,
    "errors": false,
    "items": [
        ...
    ]
}

标签: spring-bootelasticsearchmicrometerspring-micrometer

解决方案


metrics-2019-08由其创建的索引ElasticMeterRegistry在其映射中设置_source为 false

GET http://localhost:9200/metrics-2019-08/_mapping

回应是

"metrics-2019-08": {
    "mappings": {
        "_source": {
            "enabled": false
        }
        ...
    }
}

来自此处的 Elasticserch 文档

_source 字段本身没有被索引(因此不可搜索),但它被存储以便在执行获取请求时可以返回它,例如 get 或 search。

因此,如果_source为 false,则每个获取文档的请求都返回空源。禁用此功能的原因是文档仅用于聚合(avg,min,max,sum ...),_source不需要这些,因此_source不存储磁盘空间以节省磁盘空间。

要禁用此行为,请设置management.metrics.export.elastic.auto-create-indexfalse。如果您已经使用true运行它,则需要使用删除现有模板

DELETE http://localhost:9200/_template/metrics_template

之后,为指标索引创建模板,如下所示:

POST http://localhost:9200/_template/metrics_template
{
  "index_patterns": [
    "metrics*"
  ],
  "mappings": {
    "_source": {
      "enabled": true
    },
    "properties": {
      "name": {
        "type": "keyword"
      },
      "count": {
        "type": "double"
      },
      "value": {
        "type": "double"
      },
      "sum": {
        "type": "double"
      },
      "mean": {
        "type": "double"
      },
      "duration": {
        "type": "double"
      },
      "max": {
        "type": "double"
      },
      "total": {
        "type": "double"
      },
      "unknown": {
        "type": "double"
      },
      "active": {
        "type": "double"
      }
    }
  }
} 

此模板与 micrometer 使用的模板相同,_source但设置为true。在此之后,文档将出现在 fetch 请求中,例如 get 或 search。


推荐阅读