问题的提出
最近在学习ElasticSearch之时,基于Java客户端的代码来测试基本的操作,结果在写测试程序的时候,碰到如下问题:
Mar 13, 2018 7:17:29 PM org.elasticsearch.plugins.PluginsService <init>
信息: [Nimrod] modules [], plugins [], sites []
Mar 13, 2018 7:17:31 PM org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler doSample
警告: [Nimrod] node {#transport#-1}{127.0.0.1}{127.0.0.1:9300} not part of the cluster Cluster [elasticsearch], ignoring...
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:286)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:351)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:85)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:59)
at node.IndexTest.indexCreate(IndexTest.java:56)
at node.IndexTest.main(IndexTest.java:35)
在正式进入问题分析之前,先来看看代码吧。
public class IndexTest {
public static void main(String[] args) throws UnknownHostException {
Client client = TransportClient.builder()
.build().addTransportAddress(
new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300));
indexGet(client);
}
public static void indexGet(Client client ) {
SearchResponse res = null;
res = client.prepareSearch("searchkk")
.setTypes("articles").get();
System.out.println(res);
// on shutdown
client.close();
}
}
在上述代码中, searchkk和articles都是提前已经设置好的索引数据项,indexGet()方法是为了进行index的查询。
环境介绍
目前最新的ES版本为6.2,笔者目前使用的版本是2.4. JDK 8. OS: macos
es的版本分为两条主线: 2.x和5.x的两个版本。
elasticsearch的安装
安装方式可以直接从官方网站上下载对应的安装包进行安装,也可以进行命令来进行,在macos上是基于brew来安装的:
brew search elasticsearch
命令输出为:
==> Searching local taps...
elasticsearch elasticsearch@2.4 elasticsearch@5.6
==> Searching taps on GitHub...
==> Searching blacklisted, migrated and deleted formulae...
TonydeMacBook-Pro:~ tonychen$ brew search elasticsearch@2.4
==> Searching local taps...
elasticsearch@2.4
==> Searching taps on GitHub...
==> Searching blacklisted, migrated and deleted formulae...
brew install elasticsearch@2.4
安装过程如下:
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core, caskroom/cask).
==> Updated Formulae
bit cloog e2fsprogs file-roller getdns gsoap imagemagick@6 numpy pidgin pumba vim
buildifier cockroach evince fn gjs gtk+3 libmatio ocrmypdf pipenv qpdf vte3
chisel cromwell faas-cli folly glade gucharmap libsass ophcrack plantuml skaffold yelp-tools
cimg dmd fdk-aac geckodriver gnu-time imagemagick libsoup paket pre-commit termius
==> Downloading https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.6/elasticsearch-2.4.6.tar.gz
######################################################################## 100.0%
==> Caveats
Data: /usr/local/var/elasticsearch/elasticsearch_tonychen/
Logs: /usr/local/var/log/elasticsearch/elasticsearch_tonychen.log
Plugins: /usr/local/opt/elasticsearch@2.4/libexec/plugins/
Config: /usr/local/etc/elasticsearch/
plugin script: /usr/local/opt/elasticsearch@2.4/libexec/bin/plugin
This formula is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/elasticsearch@2.4/bin:$PATH"' >> ~/.bash_profile
To have launchd start elasticsearch@2.4 now and restart at login:
brew services start elasticsearch@2.4
Or, if you don't want/need a background service you can just run:
elasticsearch
==> Summary
�� /usr/local/Cellar/elasticsearch@2.4/2.4.6: 59 files, 29.1MB, built in 3 minutes 39 seconds
问题分析
在命令行下,启动elasticsearch.
首先写入环境变量:
echo ‘export PATH=”/usr/local/opt/elasticsearch@2.4/bin:$PATH”’ >> ~/.bash_profile
执行profile的变化:
source ~/.bash_profile
启动elasticsearch
elasticsearch
描述完了这么多步骤信息,下面该如何来解决问题呢?
于是上网去搜索来一下,有各种不同的说法,经过尝试和验证之后,最终的方式是cluster_name没有配置。
cluster_name的设置
哪里去找cluster_name呢? 从elasticsearch的启动过程信息来看:
[2018-03-13 18:52:27,685][INFO ][node ] [Gorgeous George] version[2.4.6], pid[3401], build[5376dca/2017-07-18T12:17:44Z]
[2018-03-13 18:52:27,686][INFO ][node ] [Gorgeous George] initializing ...
[2018-03-13 18:52:28,404][INFO ][plugins ] [Gorgeous George] modules [reindex, lang-expression, lang-groovy], plugins [], sites []
[2018-03-13 18:52:28,478][INFO ][env ] [Gorgeous George] using [1] data paths, mounts [[/ (/dev/disk1s1)]], net usable_space [18.3gb], net total_space [112.8gb], spins? [unknown], types [apfs]
[2018-03-13 18:52:28,479][INFO ][env ] [Gorgeous George] heap size [990.7mb], compressed ordinary object pointers [true]
[2018-03-13 18:52:28,480][WARN ][env ] [Gorgeous George] max file descriptors [10240] for elasticsearch process likely too low, consider increasing to at least [65536]
[2018-03-13 18:52:31,107][INFO ][node ] [Gorgeous George] initialized
[2018-03-13 18:52:31,107][INFO ][node ] [Gorgeous George] starting ...
[2018-03-13 18:52:31,242][INFO ][transport ] [Gorgeous George] publish_address {127.0.0.1:9300}, bound_addresses {[fe80::1]:9300}, {[::1]:9300}, {127.0.0.1:9300}
[2018-03-13 18:52:31,249][INFO ][discovery ] **[Gorgeous George] elasticsearch_tonychen/QuiZlm2nRDmaZVdlhJfZcw**
[2018-03-13 18:52:34,312][INFO ][cluster.service ] [Gorgeous George] new_master {Gorgeous George}{QuiZlm2nRDmaZVdlhJfZcw}{127.0.0.1}{127.0.0.1:9300}, reason: zen-disco-join(elected_as_master, [0] joins received)
[2018-03-13 18:52:34,329][INFO ][http ] [Gorgeous George] publish_address {127.0.0.1:9200}, bound_addresses {[fe80::1]:9200}, {[::1]:9200}, {127.0.0.1:9200}
[2018-03-13 18:52:34,329][INFO ][node ] [Gorgeous George] started
[2018-03-13 18:52:34,385][INFO ][gateway ] [Gorgeous George] recovered [1] indices into cluster_state
[2018-03-13 18:52:34,767][INFO ][cluster.routing.allocation] [Gorgeous George] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[searchkk][0], [searchkk][2], [searchkk][0]] ...]).
[2018-03-13 19:09:22,146][INFO ][cluster.metadata ] [Gorgeous George] [api_test] creating index, cause [auto(index api)], templates [], shards [5]/[1], mappings []
[2018-03-13 19:09:22,293][INFO ][cluster.routing.allocation] [Gorgeous George] Cluster health status changed from [RED] to [YELLOW] (reason: [shards started [[api_test][4]] ...]).
[2018-03-13 19:09:22,313][INFO ][cluster.metadata ] [Gorgeous George] [api_test] create_mapping [type_test]
其中提到来cluster_name,
[2018-03-13 18:52:31,249][INFO ][discovery]
[Gorgeous George] elasticsearch_tonychen/QuiZlm2nRDmaZVdlhJfZcw
另外 一条路径是基于elasticsearch.yml,在其中设置有cluster_name.
问题解决
在client初始化中,设置cluster_name,然后就可以了,正确的代码如下:
+ Settings settings = Settings.builder().put("cluster.name", "elasticsearch_tonychen").build();
Client client = TransportClient.builder()
.settings(settings)
.build().addTransportAddress(
new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300));
修改了上述代码之后,整个程序就可以正常运行了。
总结
注意版本的不同,同样的问题会有不同的解决方案的。这里是elasticsearch 2.4。