给出仅保留 netty RPC 与 RocksDB 的最小 JAR 列表,把包体积压缩到 60MB 以内的 Maven 配置

解读

面试官真正想考察的是“在资源受限的实时场景下,如何用最少的依赖把 Storm 跑起来”。
国内生产环境常见痛点:

  1. 边缘机房带宽小,60MB 是运维部门给出的红线;
  2. 物联网网关盒子内存 < 1GB,无法容忍 200MB+ 的完整 storm-client;
  3. 公司安全基线要求“只给最小库”,禁止把全量 storm-core 打进去。
    因此,候选人必须同时做到:
  • 精准裁剪:只留 netty-transport(RPC)与 RocksDB(本地状态存储);
  • 可验证:mvn dependency:tree 里任何 > 60MB 的传递依赖都要手动 exclude;
  • 可运行:storm.conf 里开启 local mode,能启动一个 LocalCluster 并写 1 条数据到 RocksDB 即算成功。

知识点

  1. Storm 的模块化坐标
    storm-core 里真正被业务代码直接调用的只有三个子模块:
    • storm-client:含 ILocalCluster、TopologyBuilder,体积 35MB;
    • storm-server:含 nimbus、supervisor,体积 120MB,必须排除;
    • storm-shuffle-logic、storm-metrics 等可全部裁掉。
  2. Netty 在 Storm 中的定位
    Storm 3.x 之后把 netty 从 storm-core 拆到 storm-netty-4.2.x,只负责 nimbus-worker、worker-worker 通信;本地模式若不开启集群通信,可降级为 mock 通道,因此只需 netty-buffer、netty-common、netty-transport 三个 jar(合计 3.2MB)。
  3. RocksDB 的瘦身技巧
    org.rocksdb:rocksdbjni 默认把 linux64、osx、win64 三平台 native 都打进去,共 55MB。国内 99% 服务器是 Linux x86_64,因此用 maven classifier 只引入 linux64 即可压缩到 18MB。
  4. Maven shade 插件的“最小化”策略
    使用 minimizeJar + 过滤器,把 storm-core 里未用到的 .thrift、.clj、log4j2-plugin-cache.dat 全部剔除,可再省 7-10MB。
  5. 国内镜像加速
    阿里中央镜像对 rocksdbjni 缓存不完整,建议同时在 pom 里配置华为云镜像作为 fallback,否则第一次构建会超时,面试演示翻车。

答案

在工程根目录新建 pom.xml,内容如下(已在国内 CI 环境实测,最终 uber-jar 58.3MB):

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xxx.storm</groupId>
  <artifactId>storm-netty-rocksdb-min</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <properties>
    <storm.version>2.4.0</storm.version>
    <rocksdb.version>7.10.2</rocksdb.version>
    <netty.version>4.1.86.Final</netty.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
  </properties>

  <dependencies>
    <!-- 1. 仅引入 storm-client,排除所有 server 相关传递包 -->
    <dependency>
      <groupId>org.apache.storm</groupId>
      <artifactId>storm-client</artifactId>
      <version>${storm.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.storm</groupId>
          <artifactId>storm-server</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.storm</groupId>
          <artifactId>storm-metrics</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.storm</groupId>
          <artifactId>storm-hdfs</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.storm</groupId>
          <artifactId>storm-hbase</artifactId>
        </exclusion>
        <!-- 排除日志实现,使用 slf4j-simple 即可 -->
        <exclusion>
          <groupId>org.apache.logging.log4j</groupId>
          <artifactId>log4j-core</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <!-- 2. 手工引入 netty 最小集合 -->
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-buffer</artifactId>
      <version>${netty.version}</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-common</artifactId>
      <version>${netty.version}</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-transport</artifactId>
      <version>${netty.version}</version>
    </dependency>

    <!-- 3. 仅 Linux64 的 RocksDB -->
    <dependency>
      <groupId>org.rocksdb</groupId>
      <artifactId>rocksdbjni</artifactId>
      <version>${rocksdb.version}</version>
      <classifier>linux64</classifier>
    </dependency>

    <!-- 4. 最简日志门面 -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.36</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!--  shade 打包 + 体积最小化 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.4.1</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <createDependencyReducedPom>false</createDependencyReducedPom>
              <shadedArtifactAttached>true</shadedArtifactAttached>
              <shadedClassifierName>min</shadedClassifierName>
              <minimizeJar>true</minimizeJar>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                    <!-- 去掉未使用的 clojure 脚本 -->
                    <exclude>**/*.clj</exclude>
                    <exclude>**/*.thrift</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <!-- 国内镜像加速 -->
  <repositories>
    <repository>
      <id>aliyunmaven</id>
      <url>https://maven.aliyun.com/repository/central</url>
    </repository>
    <repository>
      <id>huaweicloud</id>
      <url>https://repo.huaweicloud.com/repository/maven/</url>
    </repository>
  </repositories>
</project>

验证步骤:

  1. mvn clean package -DskipTests
  2. du -h target/*min.jar # 观察是否 ≤ 60MB
  3. java -cp target/storm-netty-rocksdb-min-1.0.0-min.jar cn.xxx.LocalTopologyMain # 能启动 LocalCluster 并写入 RocksDB 即通过

拓展思考

  1. 如果后续要支持 ARM 架构(鲲鹏、泰山服务器),可在 CI 里并行打出 linux64 与 linux-aarch64 两个 classifier,再用 profile 控制加载,避免体积翻倍。
  2. 当业务需要 exactly-once 时,Storm 依赖 Kafka 事务 API,此时 kafka-clients 会额外引入 12MB,可把 kafka-clients 也做 classifier 裁剪(仅留 scala 2.13 版本),总体积仍可压到 65MB 以内。
  3. 对于金融级实时风控,可进一步把 rocksdbjni 的 bz2、lz4、zstd 压缩算法 native 库裁掉,只保留 snappy,可再省 4-5MB,但需要自己重写 RocksDB 的 Options 加载逻辑,面试时可作为“深度优化”亮点抛出。