spark-network包在1.6时依赖guava 与 guava包冲突解决方案

一、背景

由于系统上线后,总是执行失败,而代码改动又不大。

二、异常内容

Invocation of init method failed; nested exception is com.google.common.util.concurrent.ExecutionError: java.lang.IllegalAccessError: com/google/common/base/Optional$1$1

三、异常分析

可以通过异常发现,该问题是guava包异常导致的,而仔细挖掘,会发现spark的network包下也存在com.google.common.base.Optional,而项目工程本身也使用了guava。导致JVM在加载时,使用了spark引用的包,导致的。


四、解决方案---对依赖包做shade处理

因为java是基于字节码的,因此可以动态修改字节码,即可以将依赖的jar包以及对应的class的import引用都动态修改掉。

比如以guava为例,guava包中的包名以com.google.common.*开头,我们将guava包及代码依赖中的包名全部改掉,如:xxx_guava.common.*,然后打包到一起,就可以解决包冲突的问题。这种处理的效果,看起来就像是我们不在依赖guava了,自然也就不会和Spark自带的guava包产生冲突了。

4.1、基于maven构建的项目

<plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-shade-plugin</artifactId>

                <version>3.1.0</version>

                <executions>

                    <execution>

                        <phase>package</phase>

                        <goals>

                            <goal>shade</goal>

                        </goals>

                        <configuration>

                            <relocations>

                                <relocation>

                                    <pattern>com.google.common</pattern>

                                    <shadedPattern>xx_guava.common</shadedPattern>

                                </relocation>

                            </relocations>

                            <filters>

                                <filter>

                                    <artifact>*:*</artifact>

                                    <excludes>

                                        <exclude>META-INF/maven/**</exclude>

                                    </excludes>

                                </filter>

                            </filters>

                            <finalName>${project.artifactId}-${project.version}-jar-with-dependencies</finalName>

                        </configuration>

                    </execution>

                </executions>

            </plugin>

4.2、基于sbt构建的项目

修改项目目录的project/plugins.sbt,添加assembly插件addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")

然后修改build.sbt在项目配置中添加以下设置:

assemblyShadeRules in assembly := Seq(

  // 处理guava版本和spark自带guava包版本冲突问题

  ShadeRule.rename("com.google.common.**" -> "xx_guava.common.@1").inAll

)

sbt的assembly插件会将项目中所有的依赖都打包到一起,通常情况下我们的集群中已经有Spark的部署包了,不需要把Spark的包也打进来。

我们在添加依赖的时候通过provided将其排除掉即可,如下:

libraryDependencies ++= Seq(

  "org.apache.spark" %% "spark-core" % "2.3.1" % "provided"

)

最后执行sbt assembly打包就可以了。

五.反编译查看结果

5.1.反编译看到依赖的guava.

已经不再是com.google.*,而是xx_guava.common.*.

5.2.自己的工程代码中

private static com.google.common.base.Optional test =null;

修改成

private static xx_guava.common.base.Optional test =null;

六、参考与注意事项

1.参考  https://zhuanlan.zhihu.com/p/44956574

2.注意:

因为spark部署环境中的代码没有办法改动,因此该方式有效。即将本来依赖的guava包,修改成依赖xx_guava包,如果spark的代码也可以改动,则无需这么麻烦。


3.打包工具只能有一个,因此要取消以下的打包插件,否则会报错。

Failed to create assembly: Error creating assembly archive jar-with-dependencies: zip file is empty -> [Help 1]

 <plugin>              
  <configuration>                
    <descriptorRefs>                
        <descriptorRef>jar-with-dependencies</descriptorRef>              
      </descriptorRefs>            
    </configuration>       
        <executions>            
                <execution>          
                       <id>make-assembly</id>           
                       <phase>package</phase>          
                        <goals>                          
                               <goal>single</goal>                 
                       </goals>              
               </execution>              
       </executions>          
   </plugin>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容