Elasticsearch:None of the configured nodes are available

ゝ一世哀愁。 2022-02-19 11:11 380阅读 0赞

背景:

为了研究Elasticsearch的评分机制使用,使用docker快速搭建了elasticsearch;使用spring boot连接ES的时候报错:无法找到节点

环境:

【jdk】:1.8

【ES】:6.5.4 单节点 (ip:172.26.0.251)

【springboot】:2.1.2

代码片段:

es的elasticsearch.yml配置:

  1. cluster.name: "docker-cluster"
  2. network.host: 0.0.0.0
  3. node.name: cloud
  4. http.cors.enabled: true
  5. http.cors.allow-origin: "*"
  6. node.master: true
  7. node.data: true

工程的 application.yml

  1. spring.data.elasticsearch.cluster-name = docker-cluster
  2. spring.data.elasticsearch.cluster-nodes = 172.26.0.251:9300
  3. spring.data.elasticsearch.repositories.enabled = true
  4. spring.data.elasticsearch.client-transport-sniff = true #嗅探设置

pom.xml 文件中只引入相关包

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>transport</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.nlpcn</groupId>
  7. <artifactId>elasticsearch-sql</artifactId>
  8. </dependency>

es连接配置类

  1. @Configuration
  2. @EnableConfigurationProperties(ElasticsearchProperties.class)
  3. @ConditionalOnClass(TransportClient.class)
  4. public class ElasticsearchAutoConfiguration {
  5. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  6. private ElasticsearchProperties elasticsearchProperties;
  7. public ElasticsearchAutoConfiguration(ElasticsearchProperties elasticsearchProperties) {
  8. logger.debug("ElasticsearchAutoConfiguration elasticsearchProperties:{}", JSON
  9. .toJSONString(elasticsearchProperties));
  10. this.elasticsearchProperties = elasticsearchProperties;
  11. }
  12. @Bean(destroyMethod = "close")
  13. public TransportClient client() throws Exception {
  14. TransportClient client = new PreBuiltTransportClient(settings());
  15. String clusterNodes = elasticsearchProperties.getClusterNodes();
  16. Assert.hasText(clusterNodes, "[Assertion failed] clusterNodes settings missing.");
  17. for (String clusterNode : split(clusterNodes, ElasticsearchProperties.COMMA)) {
  18. String hostName = substringBeforeLast(clusterNode, ElasticsearchProperties.COLON);
  19. String port = substringAfterLast(clusterNode, ElasticsearchProperties.COLON);
  20. Assert.hasText(hostName, "[Assertion failed] missing host name in 'clusterNodes'");
  21. Assert.hasText(port, "[Assertion failed] missing port in 'clusterNodes'");
  22. logger.info("adding transport node : " + clusterNode);
  23. client.addTransportAddress(new TransportAddress(InetAddress.getByName
  24. (hostName), Integer.valueOf(port)));
  25. }
  26. client.connectedNodes();
  27. return client;
  28. }
  29. @Bean
  30. public ElasticClientKit elasticClientKit() throws Exception {
  31. ElasticClientKit elasticClientKit = new ElasticClientKit();
  32. elasticClientKit.setTransportClient(client());
  33. return elasticClientKit;
  34. }
  35. @Bean
  36. public ElasticAdminKit elasticAdminKit() throws Exception {
  37. ElasticAdminKit elasticAdminKit = new ElasticAdminKit();
  38. elasticAdminKit.setTransportClient(client());
  39. return elasticAdminKit;
  40. }
  41. private Settings settings() {
  42. return Settings.builder()
  43. .put("cluster.name", elasticsearchProperties.getClusterName())
  44. .put("client.transport.sniff", elasticsearchProperties.getClientTransportSniff())
  45. .put("client.transport.ignore_cluster_name", elasticsearchProperties
  46. .getClientIgnoreClusterName())
  47. .put("client.transport.ping_timeout", elasticsearchProperties
  48. .getClientPingTimeout())
  49. .put("client.transport.nodes_sampler_interval", elasticsearchProperties
  50. .getClientNodesSamplerInterval())
  51. //.put("xpack.security.user", "elastic:1qaz2wsx")
  52. .build();
  53. }
  54. }

控制台错误信息:

  1. 2019-04-16 13:46:18.257 [svr-demo] ERROR 4492 --- [nio-9999-exec-2] c.j.p.w.e.GlobalExceptionHandler : None of the configured nodes are available: [{#transport#-1}{_56HAY5MSUefZh10v5asSQ}{172.26.0.251}{172.26.0.251:9300}]
  2. org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{_56HAY5MSUefZh10v5asSQ}{172.26.0.251}{172.26.0.251:9300}]
  3. at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:349)
  4. at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:247)
  5. at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
  6. at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:381)
  7. at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:407)
  8. at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:396)
  9. at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
  10. at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:53)
  11. at com.jkzl.phr.elasticsearch.ElasticClientKit.count(ElasticClientKit.java:442)
  12. at com.jkzl.phr.elasticsearch.ElasticClientKit.list(ElasticClientKit.java:446)
  13. at com.jkzl.phr.elasticsearch.ElasticClientKit.list(ElasticClientKit.java:233)
  14. at com.jkzl.phr.elasticsearch.ElasticClientKit.list(ElasticClientKit.java:227)
  15. at com.jkzl.phr.demo.score.service.ElkScoreService.getMapList(ElkScoreService.java:17)
  16. at com.jkzl.phr.demo.score.controller.ElkScoreDemoController.find(ElkScoreDemoController.java:34)
  17. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  18. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  19. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  20. at java.lang.reflect.Method.invoke(Method.java:498)
  21. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
  22. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
  23. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
  24. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
  25. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
  26. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
  27. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
  28. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
  29. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
  30. at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
  31. at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
  32. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
  33. at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  34. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
  35. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  36. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  37. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  38. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  39. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
  40. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  41. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  42. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  43. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
  44. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  45. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  46. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  47. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
  48. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  49. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  50. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  51. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
  52. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  53. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
  54. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
  55. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
  56. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
  57. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
  58. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
  59. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
  60. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
  61. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
  62. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
  63. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
  64. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
  65. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
  66. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
  67. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  68. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  69. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  70. at java.lang.Thread.run(Thread.java:748)

分析:

可能原因:通过错误分析可能是集群名称,或者端口错误,或者无法连接到集群节点

  1. 检查工程中的集群名称和交互端口是否和es服务的配置文件中一致(本例中一致,所以排除)

  2. 查看es的启动日志,检查es服务器的监听IP和对外访问的IP地址

    [root@master-251 overlay2]# docker logs -f elasticsearch
    …省略…
    [2019-04-16T05:45:56,648][INFO ][o.e.t.TransportService ] [ehrCloud] publish_address {172.17.0.3:9300}, bound_addresses {0.0.0.0:9300}
    [2019-04-16T05:45:56,796][INFO ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [ehrCloud] publish_address {172.17.0.3:9200}, bound_addresses {0.0.0.0:9200}
    [2019-04-16T05:45:56,797][INFO ][o.e.n.Node ] [ehrCloud] started

因为boot工程中 使用client.transport.sniff为true,使客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中。这样做的好处是,一般你不用手动设置集群里所有集群的ip到连接客户端,它会自动帮你添加,并且自动发现新加入集群的机器

看上面的日志可以发现ES服务器监听到的IP是172.17.0.3,这是docker内部分配的IP;在自动发现时会使用docker内网IP进行通信,导致无法连接到ES服务器

处理:

方式一:修改boot工程配置,关闭嗅探

设置工程中的client.transport.sniff为false,关闭嗅探;或者直接使用addTransportAddress方法把集群中其它机器的ip地址加到客户端中

方式二:修改ES服务器配置,将publish_host改为服务器的ip而不是docker分配的内部IP

修改elasticsearch.yml配置

  1. cluster.name: "docker-cluster"
  2. network.host: 0.0.0.0
  3. #修改监听的IP为本机的IP地址
  4. network.publish_host: 172.26.0.251
  5. network.bind_host: 0.0.0.0
  6. node.name: cloud
  7. http.cors.enabled: true
  8. http.cors.allow-origin: "*"
  9. node.master: true
  10. node.data: true

重启docker

  1. [root@master-251 overlay2]# docker restart elasticsearch
  2. ...省略...
  3. [2019-04-16T06:30:00,908][INFO ][o.e.n.Node ] [ehrCloud] starting ...
  4. [2019-04-16T06:30:01,197][INFO ][o.e.t.TransportService ] [ehrCloud] publish_address {172.26.0.251:9300}, bound_addresses {0.0.0.0:9300}
  5. [2019-04-16T06:30:01,489][INFO ][o.e.x.s.t.n.SecurityNetty4HttpServerTransport] [ehrCloud] publish_address {172.26.0.251:9200}, bound_addresses {0.0.0.0:9200}
  6. [2019-04-16T06:30:01,490][INFO ][o.e.n.Node ] [ehrCloud] started
  7. [2019-04-16T06:30:01,694][INFO ][o.w.a.d.Monitor ] [ehrCloud] try load config from /usr/share/elasticsearch/config/analysis-ik/IKAnalyzer.cfg.xml
  8. [2019-04-16T06:30:01,696][INFO ][o.w.a.d.Monitor ] [ehrCloud] try load config from /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik/config/IKAnalyzer.cfg.xml
  9. [2019-04-16T06:30:01,992][INFO ][o.w.a.d.Monitor ] [ehrCloud] [Dict Loading] /usr/share/elasticsearch/plugins/elasticsearch-analysis-ik/config/customer/new_word.dic
  10. [2019-04-16T06:30:02,821][WARN ][o.e.x.s.a.s.m.NativeRoleMappingStore] [ehrCloud] Failed to clear cache for realms [[]]

可以发现publish_address已经变为服务器IP(注意elasticsearch.yml中如果配置network.publish_host:0.0.0.0的话 监听的ip还是docker分配的内部IP,不知道是什么原因)

谨记:

这个问题基本坑了我一天的时间,起初以为是boot的elasticsearch版本和ES服务版本冲突问题,忙活了半天,后面发现这个client-transport-sniff = true 不起眼的配置,查询了不少文章才发现该问题;本文章为了谨记自己,由于水平有限,文章讲的不清楚或者不对的地方欢迎指出。

参考连接:

https://blog.csdn.net/ljc2008110/article/details/48630609

https://discuss.elastic.co/t/no-node-available-elasticsearch-transport-client-cant-connect-to-docker-container/94883

发表评论

表情:
评论列表 (有 0 条评论,380人围观)

还没有评论,来说两句吧...

相关阅读