我们有一种情况,我们需要一个应用程序能够连接到两个版本的kafka(0.7.2和0.10.0+)并充当路由器。我试图省略在这里使用两个运行时因为我们需要快速愚蠢,所以想要在运行时之间发送数据时防止额外的序列化/反序列化。
为此,我尝试将旧的kafka驱动程序从包kafka重新打包到old.kafka,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kafka-router</artifactId>
<groupId>org.deer</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>old-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<kafka.version>0.7.2</kafka.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.9.2</artifactId>
<version>${kafka.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<includes>**/*.class,**/*.xml</includes>
</artifactItem>
</artifactItems>
<includes>**/*.java</includes>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>kafka.</pattern>
<shadedPattern>old.kafka.</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我正在使用依赖插件将kafka类解包到target / classes和shade插件来重新打包它们。原因是最终的jar应该就像是一个kafka驱动程序jar(它没有其他传递依赖,因此它不会导致使用kafka而不是old.kafka的一些不匹配。但这不是真正的重点在这里,只是试图阻止题外话题。
这里的主要问题是,当我查看已安装到.m2的jar时,它看起来是正确的(具有old.kafka包):
但是当我尝试使用这个jar作为依赖时,就像这样......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>kafka-router</artifactId>
<groupId>org.deer</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>router-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.deer</groupId>
<artifactId>old-kafka</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
...并在类似的类中引用它...
package org.deer.test;
import old.kafka.producer.ProducerData;
public class TwoKafkaDriversExample {
public static void main(String[] args) {
new ProducerData();
}
}
......它自己进口不起作用。我怀疑阴影罐子里缺少与maven相关的东西,但没有注意到任何东西。另一种可能的方式是,shade插件或asm不喜欢scala类生成的字节码。
好的,所以我已经能够解决这个问题了。导入错误是intelij的问题,由于某种原因它没有看到重新打包的类。但是maven确实如此,使用正确的构造函数并添加了scala-lang依赖(它抱怨缺少Seq类)我能够构建它。
完整示例上传于github - https://github.com/Marssmart/kafka-router