apache / apache/dubbo

给dubbo的外部依赖包做类隔离,以降低接入和升级成本

Open
#7,865 2 comments 0 reactions 0 assignees View on GitHub
type/proposal
Dominant language
Java
Stars
41.6k
Forks
26.4k
Avg merge
15h 13m
Merged PRs (30d)
4

Description

- [x] I have searched the [issues](https://github.com/apache/dubbo/issues) of this repository and believe that this is not a duplicate.
- [x] I have checked the [FAQ](https://github.com/apache/dubbo/blob/master/FAQ.md) of this repository and believe that this is not a duplicate.

### Environment

* Dubbo version: all
* Operating System version: all
* Java version: all

### Steps to reproduce this issue

dubbo依赖的jar很多,在实际开发中,经常出现Dubbo跟其它框架、业务代码共jar包,导致冲突,例如:

1. 项目接入或者升级dubbo时,经常发生jar依赖冲突,如 Dubbo + RocketMQ共存时容易遇到netty版本冲突;
2. 在升级dubbo sdk版本时,会导致依赖的jar被连带升级,很多jar包并非100%向前兼容,从而导致业务逻辑出错;
3. 有些jar虽然maven坐标不同,但代码却有重合之处,例如:``io.netty`` 和 ``org.jboss.netty`` 二者就有重合的类。

dubbo用户反馈的冲突案例:

* [解决项目导入dubbo依赖项目报红叉问题](https://www.cnblogs.com/alphajuns/p/10221437.html)
* [跟springboot2.x dubbo集成 jar 包冲突](https://github.com/yinjihuan/elastic-job-spring-boot-starter/issues/31)
* [记一次使用dubbo过程中版本冲突导致的坑](https://www.cnblogs.com/wenq001/p/11580668.html)

### Expected Result

1. 通过 ``maven-shade-plugin``,对外部依赖做类隔离,可以参考 ``flink-shaded`` 这个库

相关文档:

* [maven-shade-plugin](https://maven.apache.org/plugins/maven-shade-plugin/ )
* [flink-shaded](https://github.com/apache/flink-shaded)

``maven-shade-plugin``的工作原理如下:

1). 类隔离前(不同框架引入了同1个jar的不同版本分支,导致类冲突):

![image](https://user-images.githubusercontent.com/22830937/119628191-729c6000-be3f-11eb-962a-39dc9ae56f20.png)

2). 类隔离后(通过shade插件加package前缀,对外部依赖做类隔离,来消除冲突):

![image](https://user-images.githubusercontent.com/22830937/119628380-a37c9500-be3f-11eb-837b-776d02d8a712.png)

### Actual Result

1. Dubbo的外部依赖过多,以下是1个最简单的Dubbo用法下的依赖树:

```
[INFO] +- org.apache.dubbo:dubbo:jar:2.7.7:compile
[INFO] | +- org.springframework:spring-context:jar:4.3.16.RELEASE:compile
[INFO] | | +- org.springframework:spring-aop:jar:4.3.16.RELEASE:compile
[INFO] | | +- org.springframework:spring-beans:jar:4.3.16.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.3.16.RELEASE:compile
[INFO] | +- com.alibaba.spring:spring-context-support:jar:1.0.6:compile
[INFO] | +- org.javassist:javassist:jar:3.20.0-GA:compile
[INFO] | +- io.netty:netty-all:jar:4.1.25.Final:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.20:compile
[INFO] +- org.apache.dubbo:dubbo-dependencies-zookeeper:pom:2.7.7:compile
[INFO] | +- org.apache.curator:curator-recipes:jar:4.0.1:compile
[INFO] | | \- org.apache.curator:curator-framework:jar:4.0.1:compile
[INFO] | | \- org.apache.curator:curator-client:jar:4.0.1:compile
[INFO] | | \- com.google.guava:guava:jar:20.0:compile
[INFO] | \- org.apache.zookeeper:zookeeper:jar:3.4.13:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.25:compile
[INFO] | +- log4j:log4j:jar:1.2.17:compile
[INFO] | +- jline:jline:jar:0.9.94:compile
[INFO] | \- org.apache.yetus:audience-annotations:jar:0.5.0:compile
```

2. 不是所有的冲突都能通过exclude排除来解决,例如:某框架只能用低版netty,dubbo只能用高版netty 就解决不了;
3. 即便能够通过exclude来解决,也会增加程序员的负担,从而间接提高了dubbo的使用门槛、降低了开发效率。

### 预期收益

通过 将依赖类隔离,来减少dubbo的外部依赖数量,甚至做到0依赖,降低dubbo的接入和升级成本;

### 类隔离实施步骤

1. 建立1个 ``dubbo-shaded`` 代码库,可参考 ``flink-shaded`` ;
2. 定义dubbo类隔离前缀,例如:``org.apache.dubbo.shaded``,并在社区约定业务代码不要用里面的类;
3. 把dubbo常用的外部依赖逐步加入到 ``dubbo-shaded`` 代码库,并deploy到maven中央仓库;
4. 修改dubbo的``maven-shade-plugin``配置,把常用的外部依赖给shade掉;
5. 逐步shade掉外部依赖,最终做到0依赖。

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.