如何在Spring Data ES 3.2.0.M1中使用High Level Rest Client

时间:2019-02-18 20:21:18

标签: elasticsearch spring-data-elasticsearch

Spring Data ES 3.2.0.M1仍使用旧的TransportClient而不是HighLevelRestClient

Spring Data ES 3.2.0.M1支持高级Rest Client,请参阅Add support for Java High Level REST Client。我已将Spring Data ES 3.2.0.M1添加到SB2应用程序中:

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.2.0.M1</version>
        </dependency>

但是,仍然使用TransportClient。有两种指示:启动例外:

o.e.transport.netty4.Netty4Transport     : exception caught on transport layer [NettyTcpChannel{localAddress=/127.0.0.1:61171, remoteAddress=localhost/127.0.0.1:8085}], closing connection

io.netty.handler.codec.DecoderException: java.io.StreamCorruptedException: invalid internal transport message format, got (48,54,54,50)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:472) ~[netty-codec-4.1.33.Final.jar:4.1.33.Final]

和调用ElasticsearchTemplate时的异常stacktrace:

    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:349)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:247)
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:382)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:395)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:384)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
    at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.getSearchResponse(ElasticsearchTemplate.java:947)

是否有任何配置参数告诉Spring Data ES切换到新的High Level Rest Client?文档对此一言不发。

P.S。 Spring Data ES 3.2.0.M1具有 6.4.3 ES客户端版本:

Caused by: java.io.StreamCorruptedException: invalid internal transport message format, got (48,54,54,50)
    at org.elasticsearch.transport.TcpTransport.validateMessageHeader(TcpTransport.java:1327) ~[elasticsearch-6.4.3.jar:6.4.3]
    at org.elasticsearch.transport.netty4.Netty4SizeHeaderFrameDecoder.decode(Netty4SizeHeaderFrameDecoder.java:36) ~[transport-netty4-client-6.4.3.jar:6.4.3]
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:502) ~[netty-codec-4.1.33.Final.jar:4.1.33.Final]
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:441) ~[netty-codec-4.1.33.Final.jar:4.1.33.Final]
    ... 20 common frames omitted

后端运行 6.4.2 版本:

bash-4.4$ curl http://127.0.0.1:8085
{
  "name" : "NA17WWR",
  "cluster_name" : "494164851665",
  "cluster_uuid" : "7t3LoK7PRp-ur6FyxSmHwQ",
  "version" : {
    "number" : "6.4.2",
    "build_flavor" : "oss",
    "build_type" : "zip",
    "build_hash" : "04711c2",
    "build_date" : "2018-10-16T09:16:35.059415Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

1 个答案:

答案 0 :(得分:1)

正如您所提到的问题中所述,高级REST客户端在ElasticsearchRestTemplate(请参阅PR #216)中可用,而在ElasticsearchTemplate中不可用,它们将保留到ES 7是出于向后兼容的原因。

您可以使用以下配置创建一个:

<bean name="elasticsearchTemplate"
      class="org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate">
    <constructor-arg name="client" ref="restClient"/>
</bean>

<elasticsearch:rest-client id="restClient"/>