Java ElasticSearch 没有配置的节点可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25912572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ElasticSearch None of the configured nodes are available
提问by Chris
Just downloaded and installed elasticsearch 1.3.2 in past hour
刚刚下载并安装了elasticsearch 1.3.2 在过去的一个小时里
Opened IP tables to port 9200 and 9300:9400
打开 IP 表到端口 9200 和 9300:9400
Set my computer name and ip in /etc/hosts
在 /etc/hosts 中设置我的计算机名称和 IP
Head Module and Paramedic Installed and running smoothly
头部模块和护理人员安装并运行顺利
curl on localhost works flawlessy
本地主机上的 curl 完美无缺
copied all jars from download into eclipse so same version client
将下载中的所有 jar 复制到 eclipse 中,因此相同版本的客户端
--Java--
--Java--
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;
public class Test{
public static void main(String[] args) {
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
TransportClient transportClient = new TransportClient(settings);
Client client = transportClient.addTransportAddress(new InetSocketTransportAddress("143.79.236.xxx",9300));//just masking ip with xxx for SO Question
try{
SearchResponse response = client.prepareSearch().setQuery(QueryBuilders.matchQuery("url", "twitter")).setSize(5).execute().actionGet();//bunch of urls indexed
String output = response.toString();
System.out.println(output);
}catch(Exception e){
e.printStackTrace();
}
client.close();
}
}
--Output--
- 输出 -
log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:298)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:214)
at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:105)
at org.elasticsearch.client.support.AbstractClient.search(AbstractClient.java:330)
at org.elasticsearch.client.transport.TransportClient.search(TransportClient.java:421)
at org.elasticsearch.action.search.SearchRequestBuilder.doExecute(SearchRequestBuilder.java:1097)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
at Test.main(Test.java:20)
Update: Now I am REALLY confused. I just pressed run in eclipse 3 times. 2 times received the error above. 1 time the search worked!?? Brand new Centos 6.5 vps, brand new jdk installed. Then installed elasticsearch, have done nothing else to box.
更新:现在我真的很困惑。我刚刚在 Eclipse 中按了 3 次运行。2 次收到上述错误。1 次搜索成功了!??全新的 Centos 6.5 vps,安装全新的 jdk。然后装了elasticsearch,没做别的就装箱。
Update: After running ./bin/elasticsearch console
更新:运行 ./bin/elasticsearch 控制台后
[2014-09-18 08:56:13,694][INFO ][node ] [Acrobat] version[1.3.2], pid[2978], build[dee175d/2014-08-13T14:29:30Z]
[2014-09-18 08:56:13,695][INFO ][node ] [Acrobat] initializing ...
[2014-09-18 08:56:13,703][INFO ][plugins ] [Acrobat] loaded [], sites [head, paramedic]
[2014-09-18 08:56:15,941][WARN ][common.network ] failed to resolve local host, fallback to loopback
java.net.UnknownHostException: elasticsearchtest: elasticsearchtest: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
at org.elasticsearch.common.network.NetworkUtils.<clinit>(NetworkUtils.java:54)
at org.elasticsearch.transport.netty.NettyTransport.<init>(NettyTransport.java:204)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.elasticsearch.common.inject.DefaultConstructionProxyFactory.newInstance(DefaultConstructionProxyFactory.java:54)
at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:86)
at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.call(ProviderToInternalFactoryAdapter.java:45)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
at org.elasticsearch.common.inject.Scopes.get(Scopes.java:57)
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
at org.elasticsearch.common.inject.FactoryProxy.get(FactoryProxy.java:52)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.call(ProviderToInternalFactoryAdapter.java:45)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
at org.elasticsearch.common.inject.Scopes.get(Scopes.java:57)
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
at org.elasticsearch.common.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.call(ProviderToInternalFactoryAdapter.java:45)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
at org.elasticsearch.common.inject.Scopes.get(Scopes.java:57)
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
at org.elasticsearch.common.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
at org.elasticsearch.common.inject.FactoryProxy.get(FactoryProxy.java:52)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.call(ProviderToInternalFactoryAdapter.java:45)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
at org.elasticsearch.common.inject.Scopes.get(Scopes.java:57)
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
at org.elasticsearch.common.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.call(ProviderToInternalFactoryAdapter.java:45)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
at org.elasticsearch.common.inject.Scopes.get(Scopes.java:57)
at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
at org.elasticsearch.common.inject.InjectorBuilder.call(InjectorBuilder.java:200)
at org.elasticsearch.common.inject.InjectorBuilder.call(InjectorBuilder.java:193)
at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:830)
at org.elasticsearch.common.inject.InjectorBuilder.loadEagerSingletons(InjectorBuilder.java:193)
at org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:175)
at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:110)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70)
at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:59)
at org.elasticsearch.node.internal.InternalNode.<init>(InternalNode.java:192)
at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:159)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:70)
at org.elasticsearch.bootstrap.Bootstrap.main(Bootstrap.java:203)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:32)
Caused by: java.net.UnknownHostException: elasticsearchtest: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:901)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
... 62 more
[2014-09-18 08:56:16,937][INFO ][node ] [Acrobat] initialized
[2014-09-18 08:56:16,937][INFO ][node ] [Acrobat] starting ...
[2014-09-18 08:56:17,110][INFO ][transport ] [Acrobat] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/143.79.236.31:9300]}
[2014-09-18 08:56:17,126][INFO ][discovery ] [Acrobat] elastictest/QvSNFajjQ9SFjU7WOdjaLw
[2014-09-18 08:56:20,145][INFO ][cluster.service ] [Acrobat] new_master [Acrobat][QvSNFajjQ9SFjU7WOdjaLw][localhost][inet[/143.79.236.31:9300]], reason: zen-disco-join (elected_as_master)
[2014-09-18 08:56:20,212][INFO ][http ] [Acrobat] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/143.79.236.31:9200]}
[2014-09-18 08:56:20,214][INFO ][node ] [Acrobat] started
--cluster config in elasticsearch.yml--
--elasticsearch.yml 中的集群配置--
################################### Cluster ###################################
# Cluster name identifies your cluster for auto-discovery. If you're running
# multiple clusters on the same network, make sure you're using unique names.
#
cluster.name: elastictest
采纳答案by Aritra
Elasticsearch settings are in $ES_HOME/config/elasticsearch.yml
. There, if the cluster.name
setting is commented out, it means ES would take just about any cluster name. So, in your code, the cluster.name
as "elastictest"
might be the problem. Try this:
Elasticsearch 设置在$ES_HOME/config/elasticsearch.yml
. 在那里,如果该cluster.name
设置被注释掉,则意味着 ES 将采用几乎任何集群名称。因此,在您的代码中,cluster.name
as"elastictest"
可能是问题所在。尝试这个:
Client client = new TransportClient()
.addTransportAddress(new InetSocketTransportAddress(
"143.79.236.xxx",
9300));
回答by fmyblack
You should check the node's port, you could do it using head. These ports are not same. Example,
您应该检查节点的端口,您可以使用 head 来完成。这些端口不一样。例子,
The web URL you can open is localhost:9200
,
but the node's port is 9300
, so none of the configured nodes are available if you use the 9200
as the port.
您可以打开的 Web URL 是localhost:9200
,但节点的端口是9300
,因此如果您将9200
用作端口,则所有配置的节点都不可用。
回答by Philip M.
For completion's sake, here's the snippet that creates the transport client using proper static method provided by InetSocketTransportAddress
:
为了完成起见,这里是使用以下提供的适当静态方法创建传输客户端的片段InetSocketTransportAddress
:
Client esClient = TransportClient.builder()
.settings(settings)
.build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("143.79.236.xxx"), 9300));
回答by backingwu
possible problem:
可能的问题:
- wrong port, if you use a Java or Scala client, correct port is
9300
, not9200
- wrong cluster name, make sure the cluster name you set in your code is the same as the cluster.name you set in
$ES_HOME/config/elasticsearch.yml
- the sniff option, set
client.transport.sniff
to betrue
but can't connect to all nodes of ES cluster will cause this problem too. ES doc hereexplained why.
- 错误的端口,如果您使用 Java 或 Scala 客户端,则正确的端口是
9300
,而不是9200
- 错误的集群名称,请确保您在代码中设置的集群名称与您设置的 cluster.name 相同
$ES_HOME/config/elasticsearch.yml
- sniff选项,设置
client.transport.sniff
为true
但不能连接到ES集群的所有节点也会导致这个问题。ES doc here解释了原因。
回答by Dimitar II
If the above advices do not work for you, change the log levelof your logging framework configuration (log4j, logback...) to INFO. Then re-check the output.
如果上述建议对您不起作用,请将日志框架配置(log4j、logback...)的日志级别更改为INFO。然后重新检查输出。
The logger may be hiding messages like:
记录器可能隐藏如下消息:
INFO org.elasticsearch.client.transport.TransportClientNodesService - failed to get node info for...
Caused by: ElasticsearchSecurityException: missing authentication token for action...
(in the example above, there was X-Pack plugin in ElasticSearch which requires authentication)
(在上面的示例中,ElasticSearch 中有 X-Pack 插件需要身份验证)
回答by Anantha
Faced similar issue, and here is the solution
遇到类似问题,这里是解决方案
Example :
例子 :
In elasticsearch.yml add the below properties
cluster.name: production node.name: node1 network.bind_host: 10.0.1.22 network.host: 0.0.0.0 transport.tcp.port: 9300
Add the following in Java Elastic API for Bulk Push (just a code snippet). For IP Address add public IP address of elastic search machine
Client client; BulkRequestBuilder requestBuilder; try { client = TransportClient.builder().settings(Settings.builder().put("cluster.name", "production").put("node.name","node1")).build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(""), 9300)); requestBuilder = (client).prepareBulk(); } catch (Exception e) { }
Open the Firewall ports for 9200,9300
在 elasticsearch.yml 中添加以下属性
cluster.name: production node.name: node1 network.bind_host: 10.0.1.22 network.host: 0.0.0.0 transport.tcp.port: 9300
在 Java Elastic API for Bulk Push 中添加以下内容(只是一个代码片段)。对于IP地址添加弹性搜索机的公共IP地址
Client client; BulkRequestBuilder requestBuilder; try { client = TransportClient.builder().settings(Settings.builder().put("cluster.name", "production").put("node.name","node1")).build().addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(""), 9300)); requestBuilder = (client).prepareBulk(); } catch (Exception e) { }
打开 9200,9300 的防火墙端口
回答by BenCes
This one did work for me in ES 1.7.5:
这个在 ES 1.7.5 中对我有用:
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
public static void main(String[] args) throws IOException {
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff",true)
.put("cluster.name","elasticcluster").build();
Client client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("[ipaddress]",9300));
XContentBuilder builder = null;
try {
builder = jsonBuilder().startObject().field("user", "testdata").field("postdata",new Date()).field("message","testmessage")
.endObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(builder.string());
IndexResponse response = client.prepareIndex("twitter","tweet","1").setSource(builder).execute().actionGet();
client.close();
}
}
回答by Rajeev Rathor
This means we are not able to instantiate ES transportClient and throw this exception. There are couple of possibilities that cause this issue.
这意味着我们无法实例化 ES transportClient 并抛出此异常。有几种可能会导致此问题。
- Cluster name is incorrect. So open
ES_HOME_DIR/config/elasticserach.yml
file and check the cluster name value OR use this command:curl -XGET 'http://localhost:9200/_nodes'
- Verify port 9200 is http port but elasticsearch service is using tcp port 9300 [by default]. So verify that the port is not blocked.
Authentication issue: set the header in transportClient's context for authentication:
client.threadPool().getThreadContext() .putHeader("Authorization", "Basic " + encodeBase64String(basicHeader.getBytes()));
- 集群名称不正确。所以打开
ES_HOME_DIR/config/elasticserach.yml
文件并检查集群名称值或使用以下命令:curl -XGET 'http://localhost:9200/_nodes'
- 验证端口 9200 是 http 端口,但 elasticsearch 服务使用 tcp 端口 9300 [默认]。因此,请确认该端口未被阻塞。
身份验证问题:在 transportClient 的上下文中设置标题以进行身份验证:
client.threadPool().getThreadContext() .putHeader("Authorization", "Basic " + encodeBase64String(basicHeader.getBytes()));
If you are still facing this issue then add the following property:
如果您仍然面临此问题,请添加以下属性:
put("client.transport.ignore_cluster_name", true)
The below basic code is working fine for me:
下面的基本代码对我来说很好用:
Settings settings = Settings.builder()
.put("cluster.name", "my-application").put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
回答by Romain DEQUIDT
Check your elasticsearch.yml, "transport.host" property must be "0.0.0.0" not "127.0.0.1" or "localhost"
检查您的elasticsearch.yml,“transport.host”属性必须是“0.0.0.0”而不是“127.0.0.1”或“localhost”
回答by nesohc
For other users getting this problem.
对于遇到此问题的其他用户。
You may get this error if you are running a newer ElasticSearch (5.5 or later) while running Spring Boot <2 version.
如果您在运行 Spring Boot <2 版本时运行较新的 ElasticSearch(5.5 或更高版本),您可能会收到此错误。
Recommendation is to use the REST Client since the Java Client will be deprecated.
建议使用 REST 客户端,因为 Java 客户端将被弃用。
Other workaround would be to upgrade to Spring Boot 2, since that should be compatible.
其他解决方法是升级到 Spring Boot 2,因为它应该是兼容的。
See https://discuss.elastic.co/t/spring-data-elasticsearch-cant-connect-with-elasticsearch-5-5-0/94235for more information.
有关更多信息,请参阅https://discuss.elastic.co/t/spring-data-elasticsearch-cant-connect-with-elasticsearch-5-5-0/94235。