Apache Storm - 如何在生产集群中排除 Maven 中的风暴 jar

Apache Storm - How to exclude storm jar in Maven in production cluster

根据 documentation,当我们将 Storm 拓扑部署到生产集群时,我们必须从 Maven 中排除 Storm 的 jar,因为它已经在类路径中,如果我们不这样做这样,就会出现multiple default.yaml in classpath这样的错误。

那么我们该怎么做呢?文档提供了一些细节,但不够清楚。当我们配置 maven 并构建 jar 时,仍然包含 org.apache.storm jar,我们在 jar 中有一个 default.yaml,实际上,如果我们在构建中打开调试输出,我们将看到像这样的警告:

[WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.

将依赖范围 org.apache.storm 更改为 provided,但它不起作用。

<dependency>
    <groupId>org.apache.storm</groupId>
    <artifactId>storm-core</artifactId>
    <type>jar</type>
    <version>1.1.1</version>
    <scope>provided</scope>
</dependency>

documentation of Storm tells us to exclude the Storm dependency and provides a link to a page of Maven,其中解释了如何操作,但缺少完整的示例。事实证明,我们必须创建另一个 XML 文件作为程序集配置的描述符,而不是直接将配置放在 pom.xml 中,即使 Eclipse 没有抱怨将两个文件在一起。

并且,在第二个 xml 文件中,范围应该是 compile 而不是 runtime

方法如下:

我们有 pom.xmlassembly 插件,指向另一个描述符 xml 文件:

<plugin>                                                                                
    <artifactId>maven-assembly-plugin</artifactId>                                      
    <executions>                                                                        
        <execution>                                                                     
            <id>make-assembly</id> <!-- this is used for inheritance merges -->         
            <phase>package</phase> <!-- bind to the packaging phase -->                 
            <goals>                                                                     
                <goal>single</goal>                                                     
            </goals>                                                                    
        </execution>                                                                    
    </executions>                                                                       
    <configuration>                                                                     
        <descriptors>                                                                   
            <descriptor>/src/main/resources/exclude-storm.xml</descriptor>              
        </descriptors>                                                                  
        <archive>                                                                       
            <manifest>                                                                  
                <mainClass>path.to.main.class</mainClass> 
            </manifest>                                                                 
        </archive>                                                                      
        <descriptorRefs>                                                                
            <descriptorRef>jar-with-dependencies</descriptorRef>                        
        </descriptorRefs>                                                               
    </configuration>                                                                    
</plugin>     

注意部分:(应该是完整路径)

<descriptors>                                                                   
    <descriptor>/src/main/resources/exclude-storm.xml</descriptor>              
</descriptors>

并且在这条路径中:

<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>exclude-storm</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>true</unpack>
            <scope>compile</scope>   <!-- note here!!!! -->
            <excludes>
                <exclude>org.apache.storm:storm-core:jar:1.1.1</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.build.outputDirectory}</directory>
        </fileSet>
    </fileSets>
</assembly>

这里我们添加Maven doc页面的设置。

最重要的是:排除格式:应该是:(ref)

groupId:artifactId:type[:classifier]:version

还有范围! runtime 是默认值,我将其更改为 compile 并且有效。

最后编译的时候使用:

clean assembly:assembly

并打开调试输出以在控制台中查看完整输出。如果你:

  • 构建成功
  • 搜索输出但未找到类似以下内容的内容:[WARNING]The following patterns were never triggered in this artifact inclusion filter: o 'org.apache.storm:storm-core:jar:1.1.1'.
  • 罐子里没有 default.yaml

那你就知道你成功了。

感谢另一个问答的启发:How to exclude dependencies from maven assembly plugin : jar-with-dependencies?

很高兴您知道如何使用程序集插件来完成此操作。一个更简单的解决方案是使用 shade 插件来创建你的 fat jar。在 Storm examples 目录(也包含在 Storm 二进制分发版中)中有如何配置 pom 的完整示例

见实例https://github.com/apache/storm/blob/80cc88112bf4fab34571ebff03782b759d112288/examples/storm-kafka-client-examples/pom.xml#L94

当您使用 shade 插件时,在创建 fat jar 时将考虑为 storm-core 提供的范围。