elasticsearch-java中使用ES的API启用报错

【问题描述】:

1.说明:ElasticSearch版本为6.2,kibana版本为6.2

2.java项目中pom引用版本如下:

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

测试类中面方法如下

public static void main(String[] args) throws Exception {
    // 先构建client
    Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
    TransportClient client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));
    createEmployee(client);
    client.close();
}

启动程序后报错信息如下:

no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
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]
failed to get node info for {#transport#-1}{dRl3qDDhTaylpnHvI8Ukdg}{localhost}{127.0.0.1:9300}, disconnecting...
org.elasticsearch.transport.NodeDisconnectedException: [][127.0.0.1:9300][cluster:monitor/nodes/liveness] disconnected
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{dRl3qDDhTaylpnHvI8Ukdg}{localhost}{127.0.0.1:9300}]]
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:344)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:242)
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:356)
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:403)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
    at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:62)
    at com.roocoo.es.score.first.EmployeeCRUDApp.createEmployee(EmployeeCRUDApp.java:37)
    at com.roocoo.es.score.first.EmployeeCRUDApp.main(EmployeeCRUDApp.java:19)


Process finished with exit code 1


【定为分析】:

1.思路一:根据报错信息

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{dRl3qDDhTaylpnHvI8Ukdg}{localhost}{127.0.0.1:9300}]]”

百度查询,大部分为修改ElasticSearch/config/elasticsearch.yml文件,考虑可能为集群名配置、地址、端口配置问题

# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.

尝试如下:

1.1 network.host解注后,报错:java.net.BindException: Cannot assign requested address: bind,IP地址变化导致此问题。

1.2 修改java代码中的localhost为实际IP地址,或者127.0.0.1均不可解决。

InetAddress.getByName("localhost")

2.思路二:从elasticsearch运行日志入手,查看日志,关键如红色标注

[2018-05-17T13:52:49,611][WARN ][o.e.t.n.Netty4Transport  ] [yWbGC_X] exception
caught on transport layer [NettyTcpChannel{localAddress=/127.0.0.1:9300, remoteA
ddress=/127.0.0.1:55958}], closing connection
java.lang.IllegalStateException: Received message from unsupported version: [5.2
.2] minimal compatible version is: [5.6.0]
        at org.elasticsearch.transport.TcpTransport.ensureVersionCompatibility(T
cpTransport.java:1430) ~[elasticsearch-6.2.4.jar:6.2.4]
        at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport
.java:1377) ~[elasticsearch-6.2.4.jar:6.2.4]
        at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channe
lRead(Netty4MessageChannelHandler.java:64) ~[transport-netty4-6.2.4.jar:6.2.4]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
ctChannelHandlerContext.java:340) [netty-transport-4.1.16.Final.jar:4.1.16.Final
]
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMes
sageDecoder.java:310) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMes
sageDecoder.java:297) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageD
ecoder.java:413) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessage
Decoder.java:265) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
ctChannelHandlerContext.java:340) [netty-transport-4.1.16.Final.jar:4.1.16.Final
]
        at io.netty.handler.logging.LoggingHandler.channelRead(LoggingHandler.ja
va:241) [netty-handler-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(Abstra
ctChannelHandlerContext.java:340) [netty-transport-4.1.16.Final.jar:4.1.16.Final
]
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(Defau
ltChannelPipeline.java:1359) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(Abst
ractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Fin
al]
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChanne
lPipeline.java:935) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(Abstra
ctNioByteChannel.java:134) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.jav
a:645) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLo
op.java:545) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.ja
va:499) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459) [netty-t
ransport-4.1.16.Final.jar:4.1.16.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThread
EventExecutor.java:858) [netty-common-4.1.16.Final.jar:4.1.16.Final]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]
[2018-05-17T13:55:32,015][INFO ][o.e.c.m.MetaDataCreateIndexService] [yWbGC_X] [
company] creating index, cause [auto(bulk api)], templates [], shards [5]/[1], m
appings []

尝试修改pom中jar包版本

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

修改后API存在差异,需要修改main方法如下:

public static void main(String[] args) throws Exception {
	// 先构建client
	Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
    TransportClient client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
    createEmployee(client);
    client.close();
}

重新运行程序,创建成功

no modules loaded
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.Netty4Plugin]
CREATED

Process finished with exit code 0


【问题根因】:

pom文件中引用jar包版本过低;

【解决方案】:

修改pom中jar包版本

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

修改后API存在差异,需要修改main方法如下:

public static void main(String[] args) throws Exception {
	// 先构建client
	Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
    TransportClient client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"),9300));
    createEmployee(client);
    client.close();
}

重新运行程序

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页