一 、问题背景
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版本对应
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.2.0</version>
</dependency>
参考: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);
}