java - 使用 PreBuiltTransportClient 调用 elasticsearch
问题描述
我创建了一个新的正在运行的 docker 弹性实例:
docker run --name i3-ps-elastic -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:5.4.3
它对 http 调用响应良好:
$ curl localhost:9200
{
"name" : "_wqolsQ",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "m2Mh9oXaSbiPF2UQprrB-Q",
"version" : {
"number" : "5.4.3",
"build_hash" : "eed30a8",
"build_date" : "2017-06-22T00:34:03.743Z",
"build_snapshot" : false,
"lucene_version" : "6.5.1"
},
"tagline" : "You Know, for Search"
}
请注意 cluster_name 如何在客户端和服务器中设置为相同的值。
但随后我运行以下测试:
@Test
public void checkElasticConnection() throws UnknownHostException {
Settings.Builder settings = Settings.builder().put("cluster.name", "docker-cluster");
TransportClient client = new PreBuiltTransportClient(settings.build());
client.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));
assertThat(client).isNotNull();
ClusterHealthResponse clusterHealthResponse = client.admin().cluster().prepareHealth().get();
assertThat(clusterHealthResponse).isNotNull();
}
我得到:
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{hRbm7zJ1TR6p_KdmohkCFQ}{localhost}{127.0.0.1:9300}]
]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:378)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
at org.elasticsearch.client.support.AbstractClient$ClusterAdmin.execute(AbstractClient.java:706)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:53)
...
解决方案
对我来说,问题是服务器和客户端之间的版本不匹配。我的服务器是5.4.3
和客户端6.3.0
。我通过删除 docker 实例并运行匹配的版本来解决这个问题:
docker run --name i3-ps-elastic -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:6.3.0
推荐阅读
- neural-network - 强化学习。使用 PPO 绕过物体
- ios - 如何在 SwiftUI 中为叠加层设置动画
- ios - Node js socket io chat 发送消息通知
- android-studio - 如何在 Firebase 的嵌套节点中获取特定数据
- python - 从 gmail 帐户获取附件的 Python 脚本
- python - Nvim: $VIRTUAL_ENV 存在但似乎处于非活动状态。这可能会导致意想不到的结果
- javascript - 突出显示并删除单击 javascript 时突出显示 div
- yocto - 覆盖 bbappend 文件中的 defconfig
- python - 如何在python中从Json响应中创建单独的列表
- applet - Java 卡 SIM 工具包小程序