Java无法访问Windows 11上Docker中运行的Elasticsearch

9lowa7mx  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(199)

bounty将在6天后过期。回答此问题可获得+50的声望奖励。user3816378希望引起更多人关注此问题。

EDIT: it works if i disable transport client sniffing.
Settings settings = Settings.builder()
                .put("cluster.name", clusterName)
                .put("client.transport.sniff", false)
                .build();

Any ideas how to fix it?

首先,在Ubuntu 20.04中一切正常,但当我试图在Windows 10、11和MacOS X上运行同一个项目时,它失败了。
当spring服务启动时,它试图查找elasticsearch节点,但失败了,例外情况如下:

2022-12-27 16:20:40.459  INFO 23752 --- [           main] o.e.p.PluginsService                     : no modules loaded
2022-12-27 16:20:40.459  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2022-12-27 16:20:40.460  INFO 23752 --- [           main] o.e.p.PluginsService                     : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2022-12-27 16:21:02.679  INFO 23752 --- [           main] c.u.OnStartup                            : -----> APPLICATION INITIALIZATION <-----
2022-12-27 16:21:02.679  INFO 23752 --- [           main] c.u.OnStartup                            : -----> SETTING TIMEZONE
2022-12-27 16:21:02.679  INFO 23752 --- [           main] c.u.OnStartup                            : -----> CREATE INDEXES AND MAPPINGS NOW
2022-12-27 16:21:02.683 ERROR 23752 --- [           main] c.u.OnStartup                            : Elasticsearch unavailable, skipping creating of indexes

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{CA1SJmWMQfe1sRIDTY_Lpw}{localhost}{127.0.0.1:9300}]
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:371) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1247) ~[elasticsearch-6.2.4.jar:6.2.4]
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46) ~[elasticsearch-6.2.4.jar:6.2.4]
    at com.mymy.service.services.impl.IndexAndTypeManagementServiceImpl.indexExists(IndexAndTypeManagementServiceImpl.java:65) ~[classes/:?]
    at com.mymy.service.services.impl.IndexAndTypeManagementServiceImpl.createIndex(IndexAndTypeManagementServiceImpl.java:95) ~[classes/:?]
    at com.mymy.OnStartup.createIndexes(OnStartup.java:87) ~[classes/:?]
    at com.mymy.OnStartup.init(OnStartup.java:55) ~[classes/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_352]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_352]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_352]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_352]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) [spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) [spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) [spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at

Docker-compose文件如下所示:

version: '3.5'

services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.2.4
    container_name: elasticsearch
    ports: 
      - 9200:9200
      - 9300:9300
    environment:
      - discovery.type=single-node
      - cluster.name=mycluster
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - xpack.security.enabled=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - elastic_data:/usr/share/elasticsearch/data

  kibana:
    image: docker.elastic.co/kibana/kibana:6.2.4
    container_name: kibana
    environment:
      SERVER_NAME: localhost
      ELASTICSEARCH_URL: http://elasticsearch:9200/
    ports:
      - 5601:5601
    volumes: 
      - kibana_data:/var/log
    depends_on: 
      - elasticsearch
volumes:
  elastic_data:
  kibana_data:

Java版本是1.8。有什么建议吗?Elasticsearch可以通过浏览器中的Elasticsearch工具访问。

abithluo

abithluo1#

答案是将以下环境变量添加到elasticsearch服务的docker-composite.yaml中:

environment:
  - discovery.type=single-node
  - network.host=_local_,_site_
  - network.publish_host=_local_

或使用以下命令启动ElasticSearch:

command: elasticsearch -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_

https://github.com/olivere/elastic-with-docker/解决方案

相关问题