繁体   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