配置文件中的 ElasticSearch Java API TransportClient 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20611739/
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
ElasticSearch Java API TransportClient address in configuration file
提问by nefo_x
Given the application using ElasticSearch's JavaAPI TransportClient, i want to use native elasticsearch.yml
(resolved by InternalSettingsPreparer
) config file in class path to connect to cluster via transport.
鉴于使用 ElasticSearch 的 JavaAPI TransportClient 的应用程序,我想在类路径中使用本机elasticsearch.yml
(由 解析InternalSettingsPreparer
)配置文件通过传输连接到集群。
For now i have the following contents:
现在我有以下内容:
cluster:
name: elasticsearch # would be ${es.cluster.name}
network:
host: localhost
transport_address: localhost:9301 # would be ${es.network.trasport}
and the initialisation of client:
和客户端的初始化:
TransportClient client = new TransportClient();
Which gives me exception:
这给了我例外:
13/12/16 14:04:40 INFO elasticsearch.plugins: [Hanna Levy] loaded [], sites []
org.elasticsearch.client.transport.NoNodeAvailableException: No node available
But when I add the following line
但是当我添加以下行时
client.addTransportAddress(new InetSocketTransportAddress("localhost", 9301));
things begin to work as expected.
事情开始按预期工作。
So i wonder, whether is there a way to configure transport addresses from standard configuration file format and not re-invent the wheel by having a config file for config file?
所以我想知道,是否有一种方法可以从标准配置文件格式配置传输地址,而不是通过配置文件的配置文件来重新发明轮子?
采纳答案by Paige Cook
Use transport.tcp.port Should look like the following:
使用 transport.tcp.port 应该如下所示:
cluster:
name: elasticsearch # would be ${es.cluster.name}
network:
host: localhost
transport:
tcp:
port: 9301 # would be ${es.network.transport.tcp.port}
Also, I typically use the following format for my elasticsearch.yml file
另外,我通常为我的 elasticsearch.yml 文件使用以下格式
cluster.name=elasticsearch
network.host=localhost
network.transport.tcp.port=9301
回答by Njal Karevoll
The short answer is, no, Elasticsearch does not have a way to configure a default set of network addresses to join when using the transport client.
简短的回答是,不,Elasticsearch 没有办法在使用传输客户端时配置要加入的默认网络地址集。
However, it's rather simple to add support for this while still piggybacking on the default configuration loading etc by adding it to anywhere you'd like in the configuration. For example, in elasticsearch.yml
:
但是,添加对此的支持相当简单,同时仍然捎带默认配置加载等,将其添加到您想要的配置中的任何位置。例如,在elasticsearch.yml
:
transport.client.initial_nodes: ["localhost:9301"]
can be loaded by the following code which includes some extra parsing for the optional port number:
可以通过以下代码加载,其中包括对可选端口号的一些额外解析:
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
public class Test {
public static void main(String[] args) {
TransportClient client = new TransportClient();
for(String host: client.settings().getAsArray("transport.client.initial_nodes")) {
int port = 9300;
// or parse it from the host string...
String[] splitHost = host.split(":", 2);
if(splitHost.length == 2) {
host = splitHost[0];
port = Integer.parseInt(splitHost[1]);
}
client.addTransportAddress(new InetSocketTransportAddress(host, port));
}
// ...
}
}
回答by Ichwardort
I always called
我总是打电话
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "mycluster")
.put("client.transport.sniff", true).build();
// now try to connect with the TransportClient
Client client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(
"localhost", httpPort));
which resulted in: org.elasticsearch.client.transport.NoNodeAvailableException: No node available
这导致:org.elasticsearch.client.transport.NoNodeAvailableException:无可用节点
In the logs I found: bound_address {inet[/0:0:0:0:0:0:0:0:9210]}, publish_address {inet[/172.00.71.128:9200]}
在我发现的日志中: bound_address {inet[/0:0:0:0:0:0:0:0:9210]}, publish_address {inet[/172.00.71.128:9200]}
But I got a [transport.netty] [Sabra] exception caught on transport layer [[id: 0x0fd1380f, /127.0.0.1:53090 => /127.0.0.1:9300]], closing connection java.io.IOException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen
但是我在传输层 [[id: 0x0fd1380f, /127.0.0.1:53090 => /127.0.0.1:9300]] 上捕获到 [transport.netty] [Sabra] 异常,关闭连接 java.io.IOException: Eine vorhandene远程主机连接
After searching for a fix to this quite a long time myself I finally found this working for me:
在我自己搜索了很长时间的解决方案之后,我终于发现这对我有用:
When you call http://localhost:9200/_cluster/state
I noticed that only my local IP address was bound, not the localhost loopback.
当你打电话时,http://localhost:9200/_cluster/state
我注意到只有我的本地 IP 地址被绑定,而不是本地主机环回。
Changing my TransportAddress configuration to
将我的 TransportAddress 配置更改为
InetAddress IP = InetAddress.getLocalHost();
client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(IP.getHostAddress(), 9300));
finally solved my issue. Hope this helps
终于解决了我的问题。希望这可以帮助