Springboot连接elasticsearch maven项目指定自动导入的依赖项的版本号

有时候为了保持服务器端和客户端版本号的一致,避免版本冲突问题,在pom.xml中需要指定某些依赖项的版本。

服务器端 的环境:elasticsearch:5.4.1,Lucene:6.5.1

而当使用了如下属性:

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

 在引入下面这个依赖时,

        
            org.elasticsearch.client
            transport
            5.4.1
        

会自动导入大量相关的依赖项,主要是涉及Lucene和elasticsearch的,注意观察版本,如图所示:

Springboot连接elasticsearch maven项目指定自动导入的依赖项的版本号_第1张图片

导致无法连接上es,在调试中遇到的异常列表如下:

1. org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{H-qMbJOUG2f-yCcCw}{*.*.*.104}{*.*.*.104:9200}, {#transport#-2}{_XOQ8xY8QT-aamw}{*.*.*.105}{*.*.*.105:9200}, {#transport#-3}{QDUMocgQ8N2mF89w}{*.*.*.106}{*.*.*.106:9200}]
	at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) ~[elasticsearch-5.6.11.jar:5.6.11]
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) ~[elasticsearch-5.6.11.jar:5.6.11]

	原因:引入的es版本跟服务器es版本不一致;java连接es端口为9300
	
2. failed to get local cluster state for {#transport#-3}{agOeB-c3TSs3GI7pw}{*.*.*.106}{*.*.*.106:9300}, disconnecting...
	org.elasticsearch.transport.RemoteTransportException: [name-106][*.*.*.106:9300][cluster:monitor/state]
	Caused by: java.io.EOFException: tried to read: 60 bytes but only 9 remaining
	
3. ception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transportClient' defined in class path resource [com/example/demo/config/EsConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'transportClient' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/lucene/util/SetOnce
	原因:Lucene版本冲突,注意依赖的版本是否有多个;缺少Lucene-core包

4. java.lang.AbstractMethodError: org.elasticsearch.transport.TcpTransport.connectToChannels(Lorg/elasticsearch/cluster/node/DiscoveryNode;Lorg/elasticsearch/transport/ConnectionProfile;)Lorg/elasticsearch/transport/TcpTransport$NodeChannels;
	原因: elasticsearch版本冲突

所以在查看了很多博客之后,发现,可以通过给pom.xml的指定这些自动引入的依赖项的版本:

    
        UTF-8
        UTF-8
        
        1.8
        6.5.1
        5.4.1
    

测试,连接成功。

补充:

1. spring-boot-starter-parent 自动引入的依赖项默认的版本名称以及版本号可以到目录

.m2\repository\org\springframework\boot\spring-boot-dependencies\2.0.1.RELEASE\spring-boot-dependencies-2.0.1.RELEASE.pom 中查看。

2. 每次更新了maven依赖最好reimport,避免项目中依赖更新的不及时而耗费不必要的时间去调试异常。

 

你可能感兴趣的:(Springboot连接elasticsearch maven项目指定自动导入的依赖项的版本号)