当使用 Spring Native 编译 Dubbo,如何关闭 CGlib 避免 AOT 失败?

解读

在国内金融、政企云原生改造项目中,Spring Native + GraalVM 已成为上线前必须通过的“门禁”:镜像体积 < 80 MB、启动时间 < 50 ms、内存占用降低 30% 以上。Dubbo 默认使用 CGlib 生成代理类(如 GenericService、Wrapper、Adaptive 扩展),而 GraalVM 的 AOT 编译阶段无法识别运行时才生成的字节码,导致 ClassNotFoundException 或 IllegalArgumentException: Class must be non-final and have a non-private no-arg constructor 等致命错误,最终镜像构建失败。因此,“关闭 CGlib” 并不是简单去掉一个依赖,而是要让 Dubbo 在整个生命周期里彻底放弃运行时字节码增强,并提前把所需元数据告诉 Native Image。面试官通过此题,考察候选人对 Dubbo 内部扩展机制、Spring-AOT 插件、GraalVM 限制三者的综合掌控能力。

知识点

  1. Dubbo 代理生成策略:JDK 代理 vs CGlib 代理,ProxyFactory 的 SPI 扩展点
  2. Spring-AOT 与 GraalVM 的编译边界:运行时字节码生成、反射、资源、动态代理 四大禁区
  3. Dubbo 3.2 提供的 native-image.properties 与 ResourceConfig 入口类
  4. Spring Boot 3.x 的 AOT 生成器(spring-aot-maven-plugin)与 RuntimeHintsRegistrar 回调
  5. 国内常见“双注册中心”场景下,Dubbo 与 Nacos/Consul 同时开启时的 Native 适配坑点

答案

  1. 强制走 JDK 动态代理
    在 application.yml 中显式关闭 CGlib:

    dubbo:
      provider:
        proxy: jdk         # 默认 cglib,改为 jdk
      consumer:
        proxy: jdk
    

    同时覆盖 Spring Boot 的代理方式:

    spring.aop.proxy-target-class=false
    

    确保 @ConfigurationClassPostProcessor 不会回退到 Enhancer。

  2. 移除 CGlib 依赖
    在 pom.xml 中排除所有 CGlib 传递依赖:

    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    运行 mvn dependency:tree 确认 cglib-nodep 不再出现。

  3. 提供 Native Image 元数据
    新建 src/main/resources/META-INF/native-image/xxx/dubbo-native/reflect-config.json,把 Dubbo 的 Wrapper0/1/2…、AdaptiveExtension、ProtocolFilterWrapper 等潜在类提前列出;
    新建 proxy-config.json,只保留 java.lang.reflect.Proxy 类型,禁止出现 cglib.proxy.Enhancer。
    若使用 Dubbo 3.2+,可直接依赖官方已打包好的 dubbo-native 模块,它会自动带入上述配置。

  4. 利用 Spring-Boot 3 的 RuntimeHints
    编写 DubboRuntimeHints implements RuntimeHintsRegistrar:

    @Override
    public void registerHints(RuntimeHints hints, ClassLoader cl) {
        hints.reflection().registerType(ServiceBean.class, MemberCategory.INVOKE_DECLARED_METHODS);
        hints.resources().registerPattern("META-INF/dubbo/internal/org.apache.dubbo.rpc.ProxyFactory");
    }
    

    并在 @SpringBootApplication 同级目录创建 META-INF/spring/aot.factories 文件:

    org.springframework.aot.hint.RuntimeHintsRegistrar=\
    com.xxx.DubboRuntimeHints
    
  5. 构建命令
    使用 Paketo Buildpacks(国内阿里云镜像加速):

    mvn -Pnative spring-boot:build-image \
        -Dspring-boot.build-image.imageName=myhub/dubbo-native:1.0 \
        -DMETA-INF/native-image=default
    

    若出现 "Unsupported feature in configuration: proxy defined by org.springframework.cglib..." 错误,立即检查是否仍有 @ConfigurationClassEnhancer 或 @AspectJ 强制使用 CGlib,回退步骤 1。

拓展思考

  1. 如果业务代码里自己用 Enhancer.create() 做动态子类,必须改写成 JDK Proxy + 接口,或者提前在 native-image 编译期通过 @RegisterReflectionForBinding 生成字节码并固化。
  2. Dubbo 的 Triple 协议 基于 gRPC,底层依赖 Netty 的 epoll 与 tcnative,在 Native 环境下需要额外引入 netty-transport-native-epoll$linux-x86_64 的 JNI 配置,否则启动会报 UnsatisfiedLinkError。
  3. 国内信创 ARM 机器(鲲鹏 920)上,GraalVM 版本必须 ≥ 22.3 才能识别 aarch64 指令集,否则即使关闭 CGlib 也会因 SIGILL 崩溃。
  4. 当使用 Nacos 2.3 做注册中心时,其客户端内部也用 CGlib 增强 NamingClientProxy,需要 单独排除并强制使用 JDK 代理,否则 Dubbo 端关闭 CGlib 仍然会被 Nacos 重新引入。
  5. 未来 Dubbo 4.0 计划提供 compile-time bytecode generator(基于 Java Annotation Processor),在 maven compile 阶段就把 Wrapper、Adaptive 类生成好,彻底零 CGlib,届时 Native Image 将无需任何额外配置,可重点关注官方 roadmap 进展。