• 问题现象:

java.lang.NoSuchMethodError: okio.ByteString.startsWith(Lokio/ByteString;)Z
	at okio.Options.of(Options.java:64)
	at okhttp3.internal.Util.<clinit>(Util.java:73)
	at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
	at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.java:449)
	at org.influxdb.InfluxDBFactory.connect(InfluxDBFactory.java:48)
	at org.apache.flume.sink.DefaultSinkProcessor.start(DefaultSinkProcessor.java:45)
	at org.apache.flume.SinkRunner.start(SinkRunner.java:79)
	at org.apache.flume.lifecycle.LifecycleSupervisor$MonitorRunnable.run(LifecycleSupervisor.java:249)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
2020-08-31 12:15:00,660 INFO org.eclipse.jetty.util.log: Logging initialized @3211ms
  • 问题原因:

NoSuchMethodError很明显是包冲突导致的问题。

经过排查,发现大数据集群中依赖了okhttp和okio的包,而集群中也有okhttp和okio的包。

使用maven剔除okhttp和okio的包,再次启动还是不行,集群里对应的包版本太低。

将集群中的okhttp和okio的包删除,使用我们自己打的包。启动后发现可以,但是这肯定会导致集群用这两个包时报错。也不可行。

集群版本: 

ls /usr/odp/current/hadoop-hdfs-client/lib/ok*

代码中使用版本:

<dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.14.9</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okio</groupId>
            <artifactId>okio</artifactId>
            <version>1.17.6</version>
        </dependency>
  •  使用maven-shade-plugin解决问题

maven-shade-plugin

maven-shade-plugin中提供了一个Relocating(迁移)的功能,通过将原来包下的类迁移到我们指定的包名下。

类似实现了linux的mv命令,mv 源包名 目的包名。

配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <relocations>
                    <relocation>
                    	<!-- 源包名 -->
                        <pattern>okio</pattern>
                        <!-- 目的包名 -->
                        <shadedPattern>shaded.okio</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>okhttp3</pattern>
                        <shadedPattern>shaded.okhttp3</shadedPattern>
                    </relocation>
                </relocations>

            </configuration>
        </execution>
    </executions>
</plugin>

添加完后,执行打包,就会完成迁移。

如下图所示,就将okio和okhttp3包移到了我们制定的shaded目录下。

在这里插入图片描述

打包,扔到集群中,是可行的。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐