簡體   English   中英

Spring 啟動無法連接到 elasticsearch docker

[英]Spring boot unable to connect to elasticsearch docker

我的 Elasticsearch docker-compose 文件就是這樣

version: '3'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6
    container_name: elasticsearch
    environment:
      - cluster.name=cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ./config/elasticsearch/elasticsearch.yml:/usr/share/elasticsearch/elasticsearch.yml
      - esdata6:/usr/share/elasticsearch/data    
    ports:
      - "9200:9200"
  kibana:
    image: docker.elastic.co/kibana/kibana:6.8.6
    container_name: kibana
    volumes:
      - ./config/kibana/kibana.yml:/usr/share/kibana/kibana.yml
    restart: on-failure
    ports:
      - "5601:5601"

volumes:
  esdata6:

我有一個 spring 啟動應用程序,application.properties 是

spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=localhost:9200
spring.data.elasticsearch.cluster-name=cluster

但是,我收到了錯誤

2020-04-18 13:49:45.775  INFO 7576 --- [           main] o.s.d.e.c.TransportClientFactoryBean     : Adding transport node : 127.0.0.1:9200
2020-04-18 13:50:16.961 ERROR 7576 --- [           main] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{Lzoi5ddvSJizvu4g3Y3E6Q}{localhost}{127.0.0.1:9200}]

問題: 1) 我如何通過 spring 外部引導連接到 elasticsearch? 2)如果我把彈性和springboot應用程序一起docker-compose,它們將在同一個網絡上。 為了連接到彈性搜索,我應該對 spring.data.elasticsearch.cluster-nodes 進行哪些更改?

我在 spring 啟動應用程序中使用 spring-boot-starter-data-elasticsearch。

在您的 application.properties 文件中 spring.data.elasticsearch.cluster-nodes 應該是elasticsearch:9200而不是localhost:9200。

這幾乎與我幾天前遇到的問題相似,並且在這個SO 問題中也提到過,但是讓我在這里解釋一下,因為我解決了您的用例中有一些類似的警告。

使用 docker 檢查容器 ID 檢查了我的 elasticsearch docker 容器使用的網絡,其中一部分如下所示:

"Networks": {
            "docker-files_default": {->this is network name used by elasticsearch docker.
                "IPAMConfig": null,
                "Links": null,
                "Aliases": [
                    "elasticsearch",
                    "de78c684ae60"
                ],

現在為我的應用程序定義了我的 docker-compose,如下所示:

version: '3'
services:
  web:
    build: .
    ports:
      - "8080:8080"
    volumes:
      - .:/code
    networks: --> see it uses the network section and below which defines the same network used by elastic docker 
      - docker-files_default

networks:  --> note this section as well, you need to add this as well.
  docker-files_default:
    external: true

After this, as both docker containers are using the same network, you can use the name of your elasticsearch docker container name, which is elasticsearch as shown in your elastic docker-compose.yml. 所以下面的 spring 配置彈性將是:

spring.data.elasticsearch.cluster-nodes=elasticsearch:9200

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM