首页 > 解决方案 > 使用 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)
...

标签: javaelasticsearch

解决方案


对我来说,问题是服务器和客户端之间的版本不匹配。我的服务器是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

推荐阅读