如何在 Spring Data ES 3.2.0.M1 中使用高级 Rest Client [英] How to use High Level Rest Client in Spring Data ES 3.2.0.M1

查看:35
本文介绍了如何在 Spring Data ES 3.2.0.M1 中使用高级 Rest Client的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Spring Data ES 3.2.0.M1 still uses old TransportClient instead of HighLevelRestClient

Spring Data ES 3.2.0.M1 支持 High Level Rest Client,见 添加对 Java 的支持高级 REST 客户端.我已将 Spring Data ES 3.2.0.M1 添加到 SB2 应用程序:

Spring Data ES 3.2.0.M1 supports High Level Rest Client, see Add support for Java High Level REST Client. I've added Spring Data ES 3.2.0.M1 to the SB2 app:

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

但是,仍然使用 TransportClient.有两个迹象表明:启动异常:

However, still TransportClient is used. There are two indication of that: exceptions on start-up:

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时的异常堆栈跟踪:

and exception stacktrace when calling ElasticsearchTemplate:

    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 切换到新的高级 Rest 客户端?文档对此只字未提.

Are there any config param to tell Spring Data ES to switch to new High Level Rest Client? The docs say nothing about it.

附言Spring Data ES 3.2.0.M1 有 6.4.3 ES 客户端版本:

P.S. Spring Data ES 3.2.0.M1 has 6.4.3 ES client version:

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"
}

推荐答案

如您所指的问题中所述,高级 REST 客户端可在 ElasticsearchRestTemplate(参见 PR #216)不在 ElasticsearchTemplate,出于向后兼容性的原因,他们将保留到 ES 7.

As mentioned in the issue you're referring to, the high level REST client is available in ElasticsearchRestTemplate (see PR #216) not in ElasticsearchTemplate, which they'll keep until ES 7 for backward compatibility reasons.

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

You can create one with the configuration below:

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

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

这篇关于如何在 Spring Data ES 3.2.0.M1 中使用高级 Rest Client的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆