【问题标题】:How can I connect to ElasticSearch running in the official Docker image using TransportClient?如何使用 TransportClient 连接到在官方 Docker 映像中运行的 ElasticSearch?
【发布时间】:2017-06-22 20:59:10
【问题描述】:

(首先,我知道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}]]

我做错了什么?官方文档哪部分不正确?

【问题讨论】:

    标签: java elasticsearch


    【解决方案1】:

    官方文档要你将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,则会得到原始异常。

    【讨论】:

      【解决方案2】:

      就我而言,“xpack.security.enabled=false”是解决方案。

      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:5.4.1
        ports:
          - 9200:9200
          - 9300:9300
        container_name: elasticsearch
        ulimits:
          memlock:
            soft: -1
            hard: -1
        mem_limit: 1g
        environment:
          - cluster.name=docker-cluster
          - node.name=one
          - bootstrap.memory_lock=false
          - xpack.security.enabled=false
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - network.publish_host=192.168.99.100
          - transport.publish_port=9300
        volumes:
          - /usr/share/elasticsearch/data
      
      elasticsearch2:
        image: docker.elastic.co/elasticsearch/elasticsearch:5.4.1
        ports:
          - 9201:9200
          - 9301:9300
        container_name: elasticsearch2
        ulimits:
          memlock:
            soft: -1
            hard: -1
        mem_limit: 1g
        environment:
          - cluster.name=docker-cluster
          - node.name=two
          - bootstrap.memory_lock=false
          - xpack.security.enabled=false
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - network.publish_host=192.168.99.100
          - transport.publish_port=9301
          - "discovery.zen.ping.unicast.hosts=192.168.99.100"
          - "discovery.zen.minimum_master_nodes=2"
        volumes:
          - /usr/share/elasticsearch/data
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-15
        • 2017-10-15
        • 2016-07-15
        • 1970-01-01
        • 1970-01-01
        • 2019-03-22
        • 2015-05-04
        • 2022-10-14
        相关资源
        最近更新 更多