解决springboot集成es 报错NoNodeAvailableException None of the configured nodes are available

一 、问题背景
1、elasticsearch启动正常,localhost:9200能正常访问

2、spring-boot-starter-data-elasticsearch连接es,启动报错NoNodeAvailableException None of the configured nodes are available

3、软件环境

soft version function
springboot2.x 2.1.6.RELEASE springboot框架
spring-data-elasticsearch3.x 3.1.9.RELEASE spring-data
jdk1.8+ 1.8.2 java版本
elasticsearch7.x 7.2.0 最新es
二、 问题分析
1、开发版本不对应,汗颜作为程序员需要注意版本!!!

参考:https://github.com/spring-projects/spring-data-elasticsearch

可以知道如果要使用spring-data-elasticsearch 目前只能使用6.7.2以下版本

Versions
The following table shows the Elasticsearch versions that are used by Spring Data Elasticsearch:

Spring Data Elasticsearch Elasticsearch
3.2.x

6.7.2

3.1.x

6.2.2

3.0.x

5.5.0

2.1.x

2.4.0

2.0.x

2.2.0

1.3.x

1.5.2

2、检查配置

7.x的elasticsearch.yml

#集群名称
cluster.name: zyj-es
#本节点名称
node.name: node-1
#是否master节点
node.master: true
#是否存储数据
node.data: true
#跨域设置
http.cors.enabled: true
http.cors.allow-origin: “*”
#http端口
http.port: 9200
#java端口
transport.tcp.port: 9300
#可以访问es集群的ip 0.0.0.0表示不绑定
network.bind_host: 0.0.0.0
#es集群相互通信的ip 0.0.0.0默认本地网络搜索
network.publish_host: 0.0.0.0

#7.x配置
discovery.seed_hosts: [“127.0.0.1:9200”]
cluster.initial_master_nodes: [“node-1”]
6.x的elasticsearch.yml

#集群名称
cluster.name: zyj-es
#本节点名称
node.name: node-1
#是否master节点
node.master: true
#是否存储数据
node.data: true
#跨域设置
http.cors.enabled: true
http.cors.allow-origin: “*”
#http端口
http.port: 9200
#java端口
transport.tcp.port: 9300
#可以访问es集群的ip 0.0.0.0表示不绑定
network.bind_host: 0.0.0.0
#es集群相互通信的ip 0.0.0.0默认本地网络搜索
network.publish_host: 0.0.0.0

#6.x配置
discovery.zen.minimum_master_nodes: 1
xpack.license.self_generated.type: basic
springboot的application.yml配置

spring:
data:
elasticsearch:
cluster-name: zyj-es
cluster-nodes: 127.0.0.1:9300
cluster-name必须对应
network.publist_host非本机需要注意0.0.0.0 或者 指定主机ip、如果使用xpack或者head插件需要设置跨域
java通信端口9300不是9200
三、 解决方法
1、仍然使用spring-data-elasticsearch 则根据版本对应关系降低es到6.2.2

http://localhost:9200
{
“name” : “node-1”,
“cluster_name” : “docker-cluster”,
“cluster_uuid” : “NPYDCiHVT3eX2I6w8QgpBA”,
“version” : {
“number” : “6.2.2”,
“build_hash” : “10b1edd”,
“build_date” : “2018-02-16T19:01:30.685723Z”,
“build_snapshot” : false,
“lucene_version” : “7.2.1”,
“minimum_wire_compatibility_version” : “5.6.0”,
“minimum_index_compatibility_version” : “5.0.0”
},
“tagline” : “You Know, for Search”
}
. ____ _ __ _ _
/\ / __ _ () __ __ _ \ \ \
( ( )_
_ | '_ | '| | ’ / ` | \ \ \
\/ )| |)| | | | | || (| | ) ) ) )
’ |
| .__|| ||| |__, | / / / /
=|_|======|/=////
:: Spring Boot :: (v2.1.6.RELEASE)

2019-07-07 16:24:38.994 INFO 21500 — [ main] com.zyj.service.TestEsServiceTest : Starting TestEsServiceTest on DESKTOP-5VUK815 with PID 21500 (started by mechrevo in E:\SpringWorkSpace\springboot-elasticsearch)
2019-07-07 16:24:38.995 INFO 21500 — [ main] com.zyj.service.TestEsServiceTest : No active profile set, falling back to default profiles: default
2019-07-07 16:24:40.648 INFO 21500 — [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-07-07 16:24:40.765 INFO 21500 — [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 110ms. Found 1 repository interfaces.
2019-07-07 16:24:42.796 INFO 21500 — [ main] o.elasticsearch.plugins.PluginsService : no modules loaded
2019-07-07 16:24:42.798 INFO 21500 — [ main] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
2019-07-07 16:24:42.798 INFO 21500 — [ main] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2019-07-07 16:24:42.798 INFO 21500 — [ main] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2019-07-07 16:24:42.798 INFO 21500 — [ main] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
2019-07-07 16:24:42.798 INFO 21500 — [ main] o.elasticsearch.plugins.PluginsService : loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019-07-07 16:24:48.210 INFO 21500 — [ main] o.s.d.e.c.TransportClientFactoryBean : Adding transport node : 127.0.0.1:9300
2019-07-07 16:24:54.325 ERROR 21500 — [ main] .d.e.r.s.AbstractElasticsearchRepository : failed to load elasticsearch nodes : org.elasticsearch.index.mapper.MapperParsingException: No type specified for field [message]
2019-07-07 16:24:56.519 INFO 21500 — [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor’
2019-07-07 16:24:57.711 INFO 21500 — [ main] com.zyj.service.TestEsServiceTest : Started TestEsServiceTest in 19.47 seconds (JVM running for 22.305)
2019-07-07 16:24:58.743 INFO 21500 — [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService ‘applicationTaskExecutor’

Process finished with exit code 0
2、使用elasticsearch.client进行操作,与elasticsearch版本对应

org.elasticsearch.client transport 7.2.0 参考:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html

@Test
public void esinit() throws UnknownHostException {
// 指定集群名
Settings settings = Settings.builder()
.put(“cluster.name”, “zyj-es”)
.build();

    TransportClient client = new PreBuiltTransportClient(settings);

    /*
     多个就添加多个
     */
    TransportAddress node = new TransportAddress(
            InetAddress.getByName("localhost"),
            9300
    );
    client.addTransportAddress(node);
    
}

————————————————
版权声明:本文为CSDN博主「Mars'Ares」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_22211217/article/details/94998037

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值