如何解决Java Spring Boot项目中的gRPC类路径冲突问题
Spring Boot项目gRPC类路径冲突问题求助
启动报错日志
java.lang.NoSuchMethodError: io.grpc.NameResolverProvider.getScheme()Ljava/lang/String; at io.grpc.NameResolverRegistry.refreshProviders(NameResolverRegistry.java:96) ~[grpc-api-1.42.2.jar:3.4.7] at io.grpc.NameResolverRegistry.getDefaultRegistry(NameResolverRegistry.java:131) ~[grpc-api-1.42.2.jar:3.4.7] at net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration.grpcNameResolverRegistration(GrpcClientAutoConfiguration.java:119) ~[grpc-client-spring-boot-autoconfigure-2.13.1.RELEASE.jar:2.13.1.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_321] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_321] 2022-11-10 17:22:36.290 ERROR 21408 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Action: Correct the classpath of your application so that it contains compatible versions of the classes io.grpc.NameResolverRegistry and io.grpc.NameResolverProvider Process finished with exit code 0
冲突详情
- 调用位置:
io.grpc.NameResolverRegistry.refreshProviders(NameResolverRegistry.java:96) - 缺失方法:
io.grpc.NameResolverProvider.getScheme()Ljava/lang/String; - 调用方类来源:
jar:file:/C:/Users/Bashlaw/.gradle/caches/modules-2/files-2.1/io.grpc/grpc-api/1.42.2/dd67a2446043d3903f7e0532d17b8bd2f1bfe67a/grpc-api-1.42.2.jar!/io/grpc/NameResolverRegistry.class - 被调用方类的多个来源:
jar:file:/C:/Users/Bashlaw/.gradle/caches/modules-2/files-2.1/com.github.AfricasTalkingLtd.africastalking-java/core/3.4.8/dba39ba2047b38c53c9e645d535a1db6c1164219/core-3.4.8.jar!/io/grpc/NameResolverProvider.class jar:file:/C:/Users/Bashlaw/.gradle/caches/modules-2/files-2.1/io.grpc/grpc-api/1.42.2/dd67a2446043d3903f7e0532d17b8bd2f1bfe67a/grpc-api-1.42.2.jar!/io/grpc/NameResolverProvider.class - 实际加载的被调用方类层次:
io.grpc.NameResolverProvider: file:/C:/Users/Bashlaw/.gradle/caches/modules-2/files-2.1/com.github.AfricasTalkingLtd.africastalking-java/core/3.4.8/dba39ba2047b38c53c9e645d535a1db6c1164219/core-3.4.8.jar io.grpc.NameResolver.Factory: file:/C:/Users/Bashlaw/.gradle/caches/modules-2/files-2.1/com.github.AfricasTalkingLtd.africastalking-java/core/3.4.8/dba39ba2047b38c53c9e645d535a1db6c1164219/core-3.4.8.jar
已尝试在Gradle中排除相关路径,但冲突仍存在,恳请提供解决方案。
解决方案
问题根源
项目存在gRPC版本不兼容的类路径冲突:项目引入的grpc-api:1.42.2中的NameResolverRegistry需要调用getScheme()方法,但类加载器优先加载了africastalking-java/core:3.4.8中内置的旧版本NameResolverProvider,该版本未实现此方法,导致NoSuchMethodError。
具体解决步骤
- 强制统一所有gRPC依赖版本
在build.gradle中添加版本强制规则,确保所有依赖使用与项目一致的gRPC版本:
configurations.all { resolutionStrategy { force 'io.grpc:grpc-api:1.42.2' force 'io.grpc:grpc-core:1.42.2' force 'io.grpc:grpc-stub:1.42.2' // 根据项目依赖情况补充其他gRPC子模块 } }
- 彻底排除第三方依赖中的gRPC类
针对africastalking-java/core依赖,排除其内置的所有gRPC相关模块,避免旧版本类被加载:
implementation('com.github.AfricasTalkingLtd.africastalking-java:core:3.4.8') { exclude group: 'io.grpc', module: 'grpc-api' exclude group: 'io.grpc', module: 'grpc-core' exclude group: 'io.grpc', module: 'grpc-stub' exclude group: 'io.grpc', module: 'grpc-protobuf' // 若存在则排除 }
- 验证依赖树确认版本统一
执行Gradle命令查看依赖树,检查所有gRPC相关依赖的版本是否一致:
# Linux/macOS ./gradlew dependencies --configuration compileClasspath | grep -A 10 -B 5 "io.grpc" # Windows gradlew.bat dependencies --configuration compileClasspath | findstr /i "io.grpc"
- 清理缓存并重建项目
清理Gradle缓存和旧构建文件,确保没有残留的旧版本类:
./gradlew clean build --refresh-dependencies
额外排查点
如果以上步骤无效,检查项目中是否有其他依赖(如grpc-netty、grpc-protobuf等)引入了不同版本的gRPC,确保所有gRPC相关依赖版本完全匹配。
内容的提问来源于stack exchange,提问作者Bash Law
相关产品推荐
相关产品推荐

