springboot集成elasticsearch客户端问题记录 原

1背景说明

服务端ES版本为5.5.2,springboot版本为1.5.6。

工程中添加如下依赖

2问题记录

2.1 NetworkPlugin类找不到

报错java.lang.ClassNotFoundException: org.elasticsearch.plugins.NetworkPlugin

和java.lang.NoClassDefFoundError: org/elasticsearch/plugins/NetworkPlugin

问题分析:

在biz模块引入了5.5.2版本的es client jar,能找到对应的类

查看api模块的war中的es client jar版本确实不是5.5.2,而是springboot默认的2.4.5,此版本确实是没有NetworkPlugin这个类的。

这里涉及到maven的Dependency management 的两个作用,其中第二个方面就解释了为什么出现jar包版本的问题

1)It is a central place for defining versions of dependencies.

So when an artifact version is defined in the <dependencyManagement> section you can declare the dependency in your pom without defining the version, and the version defined in <dependencyManagement> will be used automatically.

Example 1

Let's say your parent pom defined the following

<dependencyManagement>

  <dependencies>

    <dependency>

      <groupId>com.mememe</groupId>

      <artifactId>mylib</artifactId>

      <version>1.1.12</version>

    </dependency>

  </dependencies>

</dependencyManagement>

Then if you define the following in your pom

<dependencies>

  <dependency>

    <groupId>com.mememe</groupId>

    <artifactId>mylib</artifactId>

  </dependency>

</dependencies>

Your project will automatically use version 1.1.12 as defined in the <dependencyManagement> section of your parent pom.

2)Overriding the versions of transitive dependencies.

If you have an artifact version defined in your <dependencyManagement> and one of your dependencies has a transitive dependency on the same artifact, the version of the artifact defined in your <dependencyManagement> section is used automatically.

Example 2

Let's say this artifact

<dependency>

  <groupId>com.youyou</groupId>

  <artifactId>yourlib</artifactId>

  <version>3.0.0</version>

</dependency>

Has this transitive dependency

<dependency>

  <groupId>com.morestuff</groupId>

  <artifactId>morelib</artifactId>

  <version>2.0.0</version>

</dependency>

Now let's say our parent pom defines this <dependencyManagement> section

<dependencyManagement>

  <dependencies>

    <dependency>

      <groupId>com.morestuff</groupId>

      <artifactId>morelib</artifactId>

      <version>2.5.2</version>

    </dependency>

  </dependencies>

</dependencyManagement>

Then if you define this dependency in your pom

<dependency>

  <groupId>com.youyou</groupId>

  <artifactId>yourlib</artifactId>

  <version>3.0.0</version>

</dependency>

Maven will override yourlib's dependency on morelib version 2.0.0 with morelib version 2.5.2.

参考:

https://stackoverflow.com/questions/49134849/maven-dependencytree-log-version1-compile-version-managed-from-version2

 

解决办法就是在api模块的pom文件中再一次添加依赖

2.2 netty版本冲突

报错

Exception in thread "elasticsearch[_client_][management][T#1]" java.lang.AbstractMethodError:

极光推送的客户端jar包也引入了netty

去掉即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章