(首先,我知道 Can not connect to elasticsearch container in docker 。我的问题仍然存在。)
我正在尝试 ElasticSearch。
我已经run the official Docker image from the command line as described in the official documentation ,将 cluster.name
指定为 elasticsearch
(文档声称这是默认值,但检查显示默认情况下它实际上是 docker-cluster
):
$ docker run -p 9200:9200 -p 9300:9300 -e "http.host=0.0.0.0" -e "transport.host=127.0.0.1" -e "xpack.security.enabled=false" -e "cluster.name=elasticsearch" docker.elastic.co/elasticsearch/elasticsearch:5.4.2
你会注意到我已经 disabled the X-Pack security, following official documentation .
您会注意到我已经公开了端口 9200 和端口 9300。
将浏览器指向 http://localhost:9200/_cat/health 的结果是:
1498166019 21:13:39 docker-cluster yellow 1 1 3 3 0 0 3 0 - 50.0%
…这并没有让我充满信心,但我猜,当你按照 official documentation 运行事情时,你会得到这样的结果。 .
无论如何,接下来,我使用 Java 构建了 Client
,如下所示:
this.client = new PreBuiltTransportClient(Settings.builder()
.put("cluster.name", "elasticsearch")
.put("client.transport.sniff", true)
.build())
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
您会注意到我已将 127.0.0.1
指定为主机名(与 transport.host
属性匹配),将 9300
指定为端口(与暴露的端口匹配)。
然后我运行:this.client.prepareGet("argle", "bargle", "1").get();
。我期待看到某种“嘿,虚拟的,argle
不存在”错误。
相反,这会导致可怕的结果:
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{q00tH2RKTlCkXut03lYHOg}{127.0.0.1}{127.0.0.1:9300}]]
我做错了什么?官方文档哪部分不正确?
最佳答案
官方文档要求您将transport.host
Docker环境变量设置为127.0.0.1
。需要将其设置为 0.0.0.0
。
因此,要连接到官方 ElasticSearch Docker 镜像进行测试,您需要像这样运行它:
$ docker run -p 9200:9200 -p 9300:9300 -e "http.host=0.0.0.0" -e "transport.host=0.0.0.0" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:5.4.2
集群名称将是(与文档告诉您的相反)docker-cluster
(而不是elasticsearch
)。因此,这意味着(与文档告诉您的相反)您需要像这样构建 Java 客户端:
this.client = new PreBuiltTransportClient(Settings.builder()
.put("cluster.name", "docker-cluster")
.build())
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
此外,您不能将 client.transport.sniff
设置为 true
。如果您设置了它使用此配置将其设置为 true
,您将得到原始异常。
关于java - 如何使用 TransportClient 连接到在官方 Docker 镜像中运行的 ElasticSearch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44709297/