elasticsearch 5.5使用TransportClient初始化抛异常
es版本说明
- elasticsearch 5.5.3版本
maven依赖包
- elasticsearch java client包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>5.6.0</version>
</dependency>
- netty依赖包
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.13.Final</version>
</dependency>
初始化client异常信息
elasticsearch 集群安装x-pack插件,如果直接使用TransportClient无法连接到es集群,需要使用XPackTransportClient进行连接,需要添加x-pack-transport包,添加完后初始化client时抛出以下异常信息:
Caused by: java.lang.NoSuchMethodError: io.netty.util.internal.ObjectUtil.checkPositive(ILjava/lang/String;)I
at io.netty.util.NettyRuntime$AvailableProcessorsHolder.setAvailableProcessors(NettyRuntime.java:44) ~[netty-common-4.1.13.Final.jar:4.0.33.Final]
at io.netty.util.NettyRuntime.setAvailableProcessors(NettyRuntime.java:87) ~[netty-common-4.1.13.Final.jar:4.0.33.Final]
解决方法,只需要在初始化方法前添加
// 避免netty冲突
System.setProperty("es.set.netty.runtime.available.processors", "false");
解决netty冲突后初始化client时还会抛出异常,异常信息如下:
Caused by: 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) ~[transport-netty4-client-5.6.0.jar:5.6.0]
at org.elasticsearch.transport.netty4.Netty4Transport.sendMessage(Netty4Transport.java:395) ~[transport-netty4-client-5.6.0.jar:5.6.0]
netty版本冲突,低版本的netty启动时会抛异常,需要添加高版本的netty依赖,请看maven依赖包 项,将两项添加到你的pom.xml既可,如果还有问题请留言