Dubbo源码构建

980 阅读3分钟

前言

最近一直在倒腾Dubbo相关的事情,闲来无事准备研究一下Dubbo的源码。在网上找了几篇Dubbo源码构建的文章发现没有解决自己的问题,所以记录一下构建过程中自己踩的坑。

环境准备

  • MacOS (M1 Apple Silicon)

  • JetBrains IDEA

  • JDK: 1.8

  • maven: apache-maven-3.8.3

  • ZooKeeper: 3.5.0

    ————————————————————update————————————————————

有人在评论区说构建不了,我拉取了最新的 dubbo-2.7.15版本重新进行了构建,这里借助于官方提供的导入IDE命令,在terminal里面运行 mvn idea:idea 这个命令, 我自己测试了一下,构建速度比起mavenmvn clean install -DskipTests=true快了将近十几倍

image.png

mvn idea:idea 命令

image.png

mvn clean install -DskipTests=true 命令

image.png

在构建过程中又遇到了protobuf这个依赖的问题,如下:

image.png

[ERROR] Failed to execute goal org.xolstice.maven.plugins:protobuf-maven-plugin:0.5.1:compile (default) on project dubbo-serialization-protobuf: Missing:
[ERROR] ----------
[ERROR] 1) com.google.protobuf:protoc:exe:osx-aarch_64:3.7.1
[ERROR] 
[ERROR]   Try downloading the file manually from the project website.
[ERROR] 
[ERROR]   Then, install it using the command: 
[ERROR]       mvn install:install-file -DgroupId=com.google.protobuf -DartifactId=protoc -Dversion=3.7.1 -Dclassifier=osx-aarch_64 -Dpackaging=exe -Dfile=/path/to/file
[ERROR] 
[ERROR]   Alternatively, if you host your own repository you can deploy the file there: 
[ERROR]       mvn deploy:deploy-file -DgroupId=com.google.protobuf -DartifactId=protoc -Dversion=3.7.1 -Dclassifier=osx-aarch_64 -Dpackaging=exe -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
[ERROR] 
[ERROR]   Path to dependency: 
[ERROR]   	1) org.apache.dubbo:dubbo-serialization-protobuf:jar:2.7.15
[ERROR]   	2) com.google.protobuf:protoc:exe:osx-aarch_64:3.7.1
[ERROR] 
[ERROR] ----------

然后谷歌了一下,发现这个解决方案,Mac M1 maven proto buffer 编译出错

dubbo-serialization-protobuf模块的pom.xml中搜索protobuf-maven-plugin,找到这个plugin,然后进行修改,将 ${os.detected.classifier} 直接改为 osx-x86_64

image.png

然后重新在terminal里面运行mvn idea:idea这个命令即可

image.png

————————————————————分割线————————————————————

下载源码&编译

进入到GitHub中的Dubbo官网,点击tags标签,选择你想研究的版本进行切换

image-20220409153116910

然后将对应版本的Dubbo源码下载,不要试图使用IDEA直接clone,我挂着梯子速度还是不行。直接下载到本地,然后解压即可。我下载的是2.7.15版本。

image-20220409153442025

接着使用IDEA直接导入即可

image-20220409171234017

然后等待IDEA导入相关依赖,在这个过程中出现这个错误:Cannot resolve io.grpc:grpc-api:1.31.1

image-20220409153905736

然后我找了半天,终于在StackOverFlow找到解决办法,指路Cannot resolve io.grpc:grpc-api:1.31.1,这里面有两个办法,高赞的那个没能解决,我采用的是在依赖管理中覆盖grpc-core的办法,在dubbo-2.7.15的父pom.xml中引入如下依赖:

<dependencyManagement>
    
    ...
    
    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-core</artifactId>
            <version>1.13.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>6.6.0</version>
        </dependency>
    </dependencies>
    
    ...
</dependencyManagement>

然后reimport一下pom.xml文件即可

image-20220409154838720

接着打开IDEAmaven工具栏,找到dubbo-parent(root),先点击clean,完成后,点击上方的⚡️按钮,开启跳过test编译,然后点击install

image-20220409162140014

接着会遇到一个报错,Could not find artifact com.google.protobuf:protoc:exe:osx-aarch_64:3.7.1 in alimaven (maven.aliyun.com/nexus/conte…)

image-20220409165039650

谷歌一圈,发现好像是因为M1芯片的MBP不兼容,所以出现了报错,最后找到了这个解决办法,com.google.protobuf:protoc:exe:osx-aarch_64:3.7.1,需要在mavensetting.xml的文件中添加上针对M1芯片的profile,如下所示:

<settings>
    <activeProfiles>
        <activeProfile>
            apple-silicon
        </activeProfile>
    </activeProfiles>
    <profiles>
        <profile>
            <id>apple-silicon</id>
            <properties>
                <os.detected.classifier>osx-x86_64</os.detected.classifier>
            </properties>
        </profile>
    </profiles>
</settings>

然后重新执行build,最后编译成功。

image-20220409163349543

测试&日志

接着找到dubbo-demo中的dubbo-demo-annotation-provider,如下所示:

image-20220409160338075

为了方便了解Dubbo的启动流程,修改log4j.properties,将org.apache.dubbo包及其子包的日志等级设置为debug

image-20220409160137387

接着启动本地的zookeeper,如下所示:

image-20220409165936323

然后启动Application中的main方法

image-20220409165705725

至此Dubbo源码构建结束。