gpt4 book ai didi

java - 如何使用elasticsearch java客户端连接到已启动并已启动的远程elasticsearch服务器

转载 作者:行者123 更新时间:2023-11-30 08:38:22 25 4
gpt4 key购买 nike

我正在使用此代码连接我的本地弹性服务器

        String url = "localhost:9200";
String encodedUrl = URLEncoder.encode(url, "UTF-8");
Node node = nodeBuilder().settings(
Settings.settingsBuilder()
.put("http.enabled", false)
.put("path.home", encodedUrl)
)
.client(true)
.data(false)
.node();
return node.client();

我正在使用这段代码连接远程弹性服务器

       // Transport client way


Settings settings = Settings.settingsBuilder()
.put("cluster.name", "Test") // remote elastic cluster name which is different in my case
.build();
Client client = TransportClient.builder().settings(settings).build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("**.**.***.**"), 9259)); // my remote elastic server IP and its port
return client;


// And my other way of connecting (Node Client way)is


String url = "**.**.***.**:9259";

String encodedUrl = URLEncoder.encode(url, "UTF-8");
Node node = nodeBuilder().settings(
Settings.settingsBuilder()
.put("http.enabled", false)
.put("path.home", encodedUrl)
.put("discovery.zen.ping.unicast.hosts", "52.74.***.**")
.put("cluster.name", "Test")
)
.client(true)
.data(false)
.node();
return node.client();

我可以连接我的本地弹性服务器,但我无法连接到远程弹性服务器,总是出现一些错误

NodeNotConnectedException:

发送请求传输异常

接收超时传输异常

节点不可用异常:

等...

谁能帮我弄清楚如何使用 java 连接到远程弹性服务器,因为我正在 dropwizard 框架中开发 Web 应用程序并且需要从这些弹性 api 服务器获取数据

在与远程 ES 服务器建立客户端连接时会发生这种情况

INFO  [2016-04-13 11:23:45,826] org.elasticsearch.node: [Thunderbolt] version[2.2.0], pid[4720], build[8ff36d1/2016-01-27T13:32:39Z]
INFO [2016-04-13 11:23:45,826] org.elasticsearch.node: [Thunderbolt] initializing ...
INFO [2016-04-13 11:23:45,826] org.elasticsearch.plugins: [Thunderbolt] modules [], plugins [], sites []
INFO [2016-04-13 11:23:46,039] org.elasticsearch.node: [Thunderbolt] initialized
INFO [2016-04-13 11:23:46,039] org.elasticsearch.node: [Thunderbolt] starting ...
INFO [2016-04-13 11:23:46,732] org.elasticsearch.transport: [Thunderbolt] publish_address {127.0.0.1:9302}, bound_addresses {127.0.0.1:9302}, {[::1]:9302}
INFO [2016-04-13 11:23:46,732] org.elasticsearch.discovery: [Thunderbolt] Test/eZo2PDQuTcWdd3zsLzVm-w
WARN [2016-04-13 11:24:16,748] org.elasticsearch.discovery: [Thunderbolt] waited for 30s and no initial state was set by the discovery
INFO [2016-04-13 11:24:16,748] org.elasticsearch.node: [Thunderbolt] started

最佳答案

从 ES 2.x 开始,ES 服务器将 only bind to localhost by default ,这意味着它无法从外部访问。

因此,在您的远程 ES 服务器上,确保您的 elasticsearch.yml 配置文件包含以下 network.host 设置以及可公开访问的 IP 地址:

network.host: a.b.c.d

其中 a.b.c.d 是您的远程服务器的 IP 地址。

修改后一定要重启ES。

关于java - 如何使用elasticsearch java客户端连接到已启动并已启动的远程elasticsearch服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36591116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com