Elasticsearch客户端无法在Docker容器中连接Elasticsearch

时间:2020-03-22 17:07:42

标签: java spring-boot docker elasticsearch docker-compose

我正在尝试通过Java应用程序在容器内的容器中使用Elasticsearch,该应用程序也在容器内。没有docker容器,我的应用程序可以正确连接到本地elasticsearch。我的docker-compose文件:

version: "3.7"

volumes:
  postgis:

services:
  database:
    container_name: database
    build:
      postgis/
    ports:
      - 5432:5432
    volumes:
      - ./postgis:/var/lib/postgresql:rw
    restart: on-failure
    networks:
      - net

  application:
    depends_on:
      - database
      - es
    container_name: application
    build:
      application/
    ports:
      - $LORRYAPP_DEBUG_PORT:8080
    volumes:
      - ./application:/app:rw
    environment:
      LORRYAPP_OPTS: $LORRYAPP_OPTS
    restart: on-failure
    networks:
      - net

  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.1
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      - discovery.type=single-node
    networks:
      - net

networks:
  net:
    driver: bridge

在ctor中初始化es-client:

public ElasticSearchDao(ObjectMapper mapper) {
    this.esClient = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://localhost:9200")));
    this.mapper = mapper;
} 

Stacktrace:

java.net.ConnectException: Connection refused
        at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:788) ~[elasticsearch-rest-client-7.4.0.jar!/:7.4.0]
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:218) ~[elasticsearch-rest-client-7.4.0.jar!/:7.4.0]
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:205) ~[elasticsearch-rest-client-7.4.0.jar!/:7.4.0]
        at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1454) ~[elasticsearch-rest-high-level-client-7.4.0.jar!/:7.4.0]
        at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1424) ~[elasticsearch-rest-high-level-client-7.4.0.jar!/:7.4.0]
        at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1394) ~[elasticsearch-rest-high-level-client-7.4.0.jar!/:7.4.0]
        at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:836) ~[elasticsearch-rest-high-level-client-7.4.0.jar!/:7.4.0]

ES可通过浏览器获得。 http://0.0.0.0:9200/http://127.0.0.1:9200/http://localhost:9200/做出回应:

{
  "name" : "254bdb7bcc2a",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "vm537RNGSiG3dW8ag2MDTw",
  "version" : {
    "number" : "7.6.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
    "build_date" : "2020-02-29T00:15:25.529771Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

1 个答案:

答案 0 :(得分:0)

我认为这可能与我很久以前问过的问题有关。

Accessing Elasticsearch Docker instance using NEST

它针对C#,这确实是容器的问题。在我的情况下,发生的事情是客户端将连接到docker,获得仅用于容器间通信的内部IP,然后尝试使用该IP保持连接打开-显然不起作用。