Azure Identity与ADLS SDK的Netty版本冲突问题及解决咨询
问题描述
我在项目中实现ADLS令牌凭证分发,引入了以下依赖:
// 1.13.1 implementation(libs.azure.identity) // 12.20.0 implementation(libs.azure.storage.file.datalake)
运行项目时抛出initializationError,根源为Netty版本冲突,错误信息如下:
{"az.sdk.message":"The following Netty versions were found on the classpath and have a mismatch with the versions used by azure-core-http-netty. If your application runs without issue this message can be ignored, otherwise please align the Netty versions used in your application.","azure-netty-version":"4.1.110.Final","azure-netty-native-version":"2.0.65.Final","classpath-netty-version-io.netty:netty-common":"4.1.112.Final","classpath-netty-version-io.netty:netty-handler":"4.1.112.Final","classpath-netty-version-io.netty:netty-handler-proxy":"4.1.110.Final","classpath-netty-version-io.netty:netty-buffer":"4.1.112.Final","classpath-netty-version-io.netty:netty-codec":"4.1.112.Final","classpath-netty-version-io.netty:netty-codec-http":"4.1.112.Final","classpath-netty-version-io.netty:netty-codec-http2":"4.1.112.Final","classpath-netty-version-io.netty:netty-transport-native-unix-common":"4.1.112.Final","classpath-netty-version-io.netty:netty-transport-native-epoll":"4.1.110.Final","classpath-netty-version-io.netty:netty-transport-native-kqueue":"4.1.110.Final","classpath-native-netty-version-io.netty:netty-tcnative-boringssl-static":"2.0.65.Final"}
此前尝试通过排除azure-core-http-netty解决冲突:
implementation(libs.azure.identity) { exclude(group = "com.azure", module = "azure-core-http-netty") } implementation(libs.azure.storage.file.datalake) { exclude(group = "com.azure", module = "azure-core-http-netty") }
但操作后未解决冲突,反而引发新问题。
现有疑问:
- 如何正确引入依赖并解决该冲突?
- 如何调试此类依赖冲突问题?
- Azure SDK必须使用Netty 4.1.110,项目其他模块必须使用4.1.112,能否让它们使用不同版本且不冲突?
备注:项目基于Java 8,无法升级Azure SDK版本。
解决方案
1. 强制统一Netty版本(优先推荐)
Netty 4.1.x系列小版本基本保持向后兼容,优先尝试统一版本:
- 若其他模块对4.1.112无硬性依赖,可强制整个项目使用Azure SDK要求的4.1.110.Final版本,Gradle配置如下:
configurations.all { resolutionStrategy.force 'io.netty:netty-common:4.1.110.Final' resolutionStrategy.force 'io.netty:netty-handler:4.1.110.Final' resolutionStrategy.force 'io.netty:netty-buffer:4.1.110.Final' resolutionStrategy.force 'io.netty:netty-codec:4.1.110.Final' resolutionStrategy.force 'io.netty:netty-codec-http:4.1.110.Final' resolutionStrategy.force 'io.netty:netty-codec-http2:4.1.110.Final' resolutionStrategy.force 'io.netty:netty-transport-native-unix-common:4.1.110.Final' }
- 若必须保留4.1.112,可尝试强制Azure SDK使用该版本(需测试兼容性),将上述配置中的版本替换为4.1.112.Final。
2. 替换Azure SDK的HTTP客户端
Azure SDK支持替换默认Netty客户端为OkHttp,彻底规避Netty版本冲突:
- 先排除Azure SDK中的Netty依赖:
implementation(libs.azure.identity) { exclude group: 'com.azure', module: 'azure-core-http-netty' } implementation(libs.azure.storage.file.datalake) { exclude group: 'com.azure', module: 'azure-core-http-netty' }
- 引入兼容的OkHttp客户端依赖:
implementation 'com.azure:azure-core-http-okhttp:1.11.11' // 选择与当前Azure SDK版本匹配的OkHttp客户端版本
注意:需确保azure-core-http-okhttp版本与azure-identity 1.13.1、azure-storage-file-datalake 12.20.0兼容。
调试依赖冲突的方法
- 生成依赖树:执行Gradle命令
./gradlew dependencies,搜索io.netty相关条目,定位引入4.1.112版本的具体模块。 - 定位冲突源:通过依赖树确认冲突版本的来源,判断该模块是否可调整版本,或是否必须保留该版本。
- 兼容性测试:对于Netty小版本差异,先尝试统一版本,运行核心功能验证是否正常;若出现问题,再切换到替换HTTP客户端的方案。
- 查阅官方文档:Azure SDK官方文档会明确核心依赖的版本兼容范围,可参考相关冲突解决指引。
不同Netty版本共存的可行性
Java类加载采用双亲委派模型,同一个类只能被加载一次,无法在同一JVM进程中同时加载两个不同版本的Netty核心类(如io.netty.buffer.ByteBuf)。因此无法实现Netty 4.1.110和4.1.112在同一进程中共存,必须通过统一版本或替换HTTP客户端的方式解决冲突。
内容的提问来源于stack exchange,提问作者Jun
相关产品推荐
相关产品推荐

