Docker中运行的Elasticsearch 6.4,go无法

2020-03-01  本文已影响0人  gurlan

启动go应用,报错:"no active connection found: no Elasticsearch node available"

docker-compose文件

    image: elasticsearch:6.4.2
    network_mode: "bridge"
    expose:
      - "9200"
      - "9300"
    volumes:
      - ./es-data:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"

go代码

esClient, esClientErr :=             
elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"))
if esClientErr != nil {
    return nil, fmt.Errorf("Failed to connect to ES: %v", esClientErr)
}

异常

Failed to connect to ES: no active connection found: no Elasticsearch node available

解决方法,elasticsearch.yml增加这行,重启容器

network.publish_host: "_local_"

问题解决

访问:http://127.0.0.1:9200/_nodes/http?pretty


{
  "_nodes" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "cluster_name" : "es-server",
  "nodes" : {
    "PxU7ErYgR8K8FBS2rQ8qZw" : {
      "name" : "node-1",
      "transport_address" : "127.0.0.1:9300",
      "host" : "127.0.0.1",
      "ip" : "127.0.0.1",
      "version" : "6.4.3",
      "build_flavor" : "default",
      "build_type" : "tar",
      "build_hash" : "fe40335",
      "roles" : [
        "master",
        "data",
        "ingest"
      ],
      "attributes" : {
        "ml.machine_memory" : "2086522880",
        "xpack.installed" : "true",
        "ml.max_open_jobs" : "20",
        "ml.enabled" : "true"
      },
      "http" : {
        "bound_address" : [
          "0.0.0.0:9200"
        ],
        "publish_address" : "127.0.0.1:9200", #这里原来是容器的地址,现在已经修改
        "max_content_length_in_bytes" : 104857600
      }
    }
  }
}
上一篇 下一篇

猜你喜欢

热点阅读