使用PreBuiltTransportClient调用elasticsearch

时间:2018-11-20 15:59:29

标签: java 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)
...

1 个答案:

答案 0 :(得分:1)

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