无法将java客户端连接到elasticsearch服务器

时间:2017-03-13 13:40:38

标签: java elasticsearch spring-data-elasticsearch

我正在尝试将我的java客户端连接到elasticsearch服务器。

Elasticsearch 2.4.0安装在远程服务器上。 要访问它,我必须使用端口10700.(当我运行" telnet xxxx 10700",它工作,所以端口是打开的)

我现在有2天的错误,我读到它可能是netty4依赖关系之间的冲突,但我无法修复它。 (我的netty依赖是传递的,我不能自己导入它们)

如何解决此问题?

java代码

public void connexionToEs() throws UnknownHostException {
    String clusterName = "xxxx";
    String serverAddress = "xxxx";

    try{
        Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", true)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(serverAddress), 10700));

        SearchResponse response = client.prepareSearch().execute().actionGet();
        String output = response.toString();
        System.out.println(output);
        client.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

依赖关系:

<!-- DEPENDENCIES FOR ELASTICSEARCH CLIENT -->
<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>5.2.0</version>
</dependency>

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>5.2.0</version>
</dependency>

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-web</artifactId>
  <version>2.6.2</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-to-slf4j</artifactId>
  <version>2.6.2</version>
</dependency>

错误:

Exception in thread "elasticsearch[_client_][management][T#1]" java.lang.NoSuchMethodError: io.netty.buffer.CompositeByteBuf.addComponents(ZLjava/lang/Iterable;)Lio/netty/buffer/CompositeByteBuf;
at org.elasticsearch.transport.netty4.Netty4Utils.toByteBuf(Netty4Utils.java:78)
at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:422)
at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:93)
at org.elasticsearch.transport.TcpTransport.internalSendMessage(TcpTransport.java:1058)
at org.elasticsearch.transport.TcpTransport.sendRequestToChannel(TcpTransport.java:1040)
at org.elasticsearch.transport.TcpTransport.executeHandshake(TcpTransport.java:1555)
at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:502)
at org.elasticsearch.transport.TcpTransport.connectToNode(TcpTransport.java:460)
at org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:318)
at org.elasticsearch.client.transport.TransportClientNodesService$SniffNodesSampler$1.run(TransportClientNodesService.java:488)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingRunnable.run(ThreadContext.java:527)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

netty4依赖关系:

io.netty:netty:3.10.6.Final
io.netty:netty-buffer:4.0.28.Final
io.netty:netty-codec:4.0.28.Final
io.netty:netty-codec-http:4.1.7.Final
io.netty:netty-common:4.0.28.Final
io.netty:netty-handler:4.0.28.Final
io.netty:netty-resolver:4.1.7.Final
io.netty:netty-transport:4.0.28.Final

5 个答案:

答案 0 :(得分:1)

使用正确的Elasticsearch依赖版本 - 与服务器上的相同。

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>2.4.0</version>
</dependency>

此外,2.x不需要elasticsearch传输依赖项。你可以删除

<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>5.2.0</version>
</dependency>

最后,2.x不使用PreBuiltTransportClient。请参阅文档here

答案 1 :(得分:1)

我解决了类似的问题,如下所示:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.0</version>
    <exclusions>
        <exclusion>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </exclusion>
    </exclusions>
</dependency>

答案 2 :(得分:0)

错误位于堆栈跟踪的顶部: 线程中的异常“elasticsearch [客户端] [管理] [T#1]” java.lang.NoSuchMethodError :io.netty.buffer.CompositeByteBuf.addComponents(ZLjava / lang /可迭代)LIO /网状/缓冲器/ CompositeByteBuf;

您的代码正在调用addComponents(...)类的io.netty.buffer.ComposityByteBuff方法,但在运行时此方法不可用。

很可能这是由于不同的编译和运行时环境。用于编译源代码的库版本以及在运行时使用的库版本可能不相同。

答案 3 :(得分:0)

请尝试使用与Elasticsearch服务器匹配的Java依赖关系

<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.4.0</version>
</dependency>

答案 4 :(得分:0)

排除这些netty-all依赖关系。这将解决您的问题。

compile.exclude group: "io.netty", module:"netty-all"
compile.exclude group: "org.jboss.netty", module:"netty"