You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中GRPC Channel引发AbstractManagedChannelImplBuilder类找不到异常

问题

在Spring Boot应用中配置GRPC客户端的ManagedChannelBuilder时,调用ManagedChannelBuilder.forAddress(...)方法触发java.lang.NoClassDefFoundError,提示找不到io.grpc.internal.AbstractManagedChannelImplBuilder类。该异常由grpc-api库的ManagedChannelRegistry类尝试加载io.grpc.netty.NettyChannelProvider类时引发。

GRPC客户端代码

public class GrpcClient {
    public void request() throws InterruptedException {
        ManagedChannel channel = ManagedChannelBuilder
                .forAddress("myGrpcServerAddress",
                        12345)
                .useTransportSecurity()
                .keepAliveTime(1, TimeUnit.MINUTES)
                .keepAliveTimeout(10, TimeUnit.SECONDS).build();

        MyServiceGrpc.MyServiceBlockingStub stub 
          = MyServiceGrpc.newBlockingStub(channel);

        MyResponse myResponse = stub.hello(MyRequest.newBuilder()
            .setFirstName("test")
            .setLastName("test")
            .build());

        System.out.println("Response received from server: " + myResponse);

        channel.shutdown();
    }
}

引发异常的代码行

ManagedChannelBuilder.forAddress("myGrpcServerAddress",12345)

库中触发异常的代码片段

static List<Class<?>> getHardCodedClasses() {
    List<Class<?>> list = new ArrayList();
    ...
    try {
        list.add(Class.forName("io.grpc.netty.NettyChannelProvider"));
    } catch (ClassNotFoundException var3) {
        logger.log(Level.FINE, "Unable to find NettyChannelProvider", var3);
    }

    ...
}

当前使用的依赖

"io.grpc:grpc-stub:1.61.1"
"io.grpc:grpc-protobuf:1.61.1"
"com.google.protobuf:protobuf-java-util:3.7.1"
"io.grpc:grpc-netty:1.61.1"
"io.grpc:grpc-netty-shaded:1.61.1"

已尝试更换不同GRPC版本,但问题仍未解决,求可能的原因及解决方案。


可能的原因及解决方案

1. 互斥依赖重复引入

你同时添加了grpc-netty和grpc-netty-shaded,这两个包功能重叠且互斥:

  • grpc-netty依赖原生Netty库,易与项目中其他Netty依赖产生冲突
  • grpc-netty-shaded内置了重命名后的Netty,能避免版本冲突

解决:移除其中一个依赖,推荐保留grpc-netty-shaded。

2. 缺失grpc-api核心依赖

AbstractManagedChannelImplBuilder类属于grpc-api包,你的依赖列表未显式声明该包。虽然部分GRPC组件会间接依赖它,但Spring Boot的依赖管理可能导致版本不一致或依赖缺失。

解决:显式添加同版本的grpc-api依赖:

"io.grpc:grpc-api:1.61.1"

3. Protobuf版本不兼容

当前使用的protobuf-java-util:3.7.1与GRPC 1.61.1要求的Protobuf版本不匹配(GRPC 1.61.x通常需要Protobuf 3.25及以上版本),版本差异可能引发类加载异常。

解决:升级Protobuf相关依赖至兼容版本,例如:

"com.google.protobuf:protobuf-java:3.25.1"
"com.google.protobuf:protobuf-java-util:3.25.1"

4. Spring Boot依赖管理覆盖GRPC版本

Spring Boot的spring-boot-dependencies会强制指定部分依赖的版本,可能导致你的GRPC版本被覆盖,出现类不兼容问题。

解决:通过BOM锁定所有GRPC组件的版本,确保一致性:

  • Maven:
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-bom</artifactId>
            <version>1.61.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • Gradle:
implementation platform('io.grpc:grpc-bom:1.61.1')

5. 类加载器冲突

Spring Boot打包成fat jar时,类加载路径可能出现异常,导致GRPC内部类无法被正确加载;另外Spring DevTools的类加载器也可能引发冲突。

解决:

  • 检查打包配置,确保所有GRPC相关依赖都被正确打包进fat jar
  • 若使用Spring DevTools,可临时关闭它,在application.properties中添加:
spring.devtools.restart.enabled=false

内容的提问来源于stack exchange,提问作者amx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:25:41