首次使用TransPort Client连接ElasticSearch集群报错

首次使用TransPort Client连接ElasticSearch集群报错

java.lang.NoSuchMethodError: io.netty.buffer.CompositeByteBuf.addComponents(ZLjava/lang/Iterable;)Lio/netty/buffer/CompositeByteBuf;

	at org.elasticsearch.transport.netty4.Netty4Utils.toByteBuf(Netty4Utils.java:117)
	at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:395)
	at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:94)
	at org.elasticsearch.transport.TcpTransport.internalSendMessage(TcpTransport.java:1122)
	at org.elasticsearch.transport.TcpTransport.sendRequestToChannel(TcpTransport.java:1104)
	at org.elasticsearch.transport.TcpTransport.executeHandshake(TcpTransport.java:1611)
	at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:555)
	at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:116)
	at org.elasticsearch.transport.TransportService.openConnection(TransportService.java:351)
	at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.doSample(TransportClientNodesService.java:407)
	at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:357)
	at org.elasticsearch.client.transport.TransportClientNodesService.addTransportAddresses(TransportClientNodesService.java:198)
	at org.elasticsearch.client.transport.TransportClient.addTransportAddress(TransportClient.java:319)
	at com.su.elasticsearch.TestClient.transportClient(TestClient.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

参考:https://blog.csdn.net/hayaqi0504/article/details/78781783

原因是hadoop使用的是netty3.x,5.5.0版本的es使用的是netty4,在项目目录下使用mvn dependency:tree可以查看

首次使用TransPort Client连接ElasticSearch集群报错_第1张图片

解决方案就是在项目里添加netty依赖

        
        
            io.netty
            netty-all
            4.1.11.Final
        

同时注意到还有log4j2的错误,不能显示日志
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.

此时已经添加过log4j的包

        
            org.apache.logging.log4j
            log4j-core
            2.8.2
        

解决方案:https://www.bbsmax.com/A/A2dmVmPxze/

在src目录下新建log4j2.xml



    
        
            
        
    
    
        
            
        
    

或者新建resources文件夹,并设为sources root目录后放进去

运行连接测试,成功

代码参考官网https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.5/transport-client.html

package com.su.elasticsearch;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * 获取TransportClient
 * 连接ES集群
 *
 * @Author su
 * @Date 2019/7/25 8:21
 */
public class TestClient {

	@Test
	public void transportClient() throws UnknownHostException {
		// Note that you have to set the cluster name if you use one different than "elasticsearch"
		Settings settings = Settings.builder().put("cluster.name", "escluster").build();
		// on startup
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("master"), 9300));

		List connectedNodes = client.connectedNodes();
		for (DiscoveryNode node : connectedNodes) {
			System.out.println("集群节点:" + node.getHostName());
		}
		// on shutdown
		client.close();

	}
}

运行结果:

loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
集群节点:192.168.93.10

 

你可能感兴趣的:(ElasticSearch)