Skip to main content

· 3 min read
Shixu

背景​

go 遵循 errors are values 的理念,内置 error 提供的信息比较有限,如:

_, err := os.Open("a.txt") // open a.txt: no such file or directory

然而 error 出现在哪行?调用堆栈是怎样的?不得而知。在实际使用时,这些信息的缺失不利用 Troubleshooting。这也是本文要探究的 go 错误处理的最佳实践

解决方案​

pkg/errors 的作者 Dave Cheney 经过大量的思考,赋予了 error context 的能力,很好的解决了上述提到的问题

pkg/errors 分析​

pkg/errors 主要有两类 func

一类是 Wrap,赋予 error context 的能力

// Wrap annotates cause with a message.
func Wrap(cause error, message string) error

另一类是 Cause,获取 error chain 源头处 error

// Cause unwraps an annotated error.
func Cause(err error) error

pkg/errors 有三个实现 error 接口的 struct,分别是 fundamental/withStack/withMessage

image

fundamental 通过 errors.New(msg) 创建,用于初始化 error, 作为 error chain 的源头

withMessage 通过 errors.WithMessage(err, msg) 创建,用于 wrapper 已有 error 并添加 msg

withStack 通过 errors.WithStack(err) 创建,用于 wrapper 已有 error 并添加 trace,若还需额外 msg 可使用 errors.Wrap(err, msg)

fmt.Printf("%+v", err) 用来打印 stack trace

示例代码​

// 使用 errors.Wrap 或 error.WithStack wrapper 原生的 error
func readFile(path string) ([]byte, error) {
f, err := os.Open(path)
defer f.Close()
if err != nil {
return []byte{}, errors.Wrap(err, "open failed")
}
bytes, err := ioutil.ReadAll(f)
return bytes, errors.WithStack(err)
}
// 调用
func CopyFile(old string, new string) error {
log.Print("start to copy file")
bytes, err := readFile(old)
if err != nil {
// 接受到下层传递的 context err,有以下两种方式:
// 1.透传
//return err
// 2. 添加本层信息
return errors.WithMessage(err, "copy failed")
// Warning: 切勿使用 errors.Wrap, trace 信息会重复
// return errors.Wrap(err, "copy failed")
}
return writeFile(new, bytes)
}

使用小结​

  • 总是使用 pkg/errors wrapper go 原生 err,包括第三方库返回的 err(除非能确定其已经使用 pkg/errors wrapper 了带有 context 的 error)
  • 最下层函数返回带 context 的 error,通过 errors.New(msg) 创建或者errors.WithStack(err)/Wrap(err, msg) wrapper,在最上层函数统一处理(如日志打印 fmt.Printf("%+v", err))
  • 能处理则尽早处理,不能处理则透传或使用 errors.WithMessage(err, msg) 在抛到上层函数

参考​

· 9 min read
Shixu

介绍​

Mvnd 是一个致力于提升 maven 构建速度的项目。它不是一个全新的产物,而是内置 maven,并且整合 Gradle 和 Takari 中的优势技术。那么它借鉴了什么?对比 maven 优势是否明显?适合使用在哪些场景?本文带你一探究竟。

基本使用​

安装​

Mvnd 0.7.1 运行依赖 jdk 11, 如不满足有以下提示:

org/mvndaemon/mvnd/client/DefaultClient has been compiled by a more recent version of the Java Runtime (class file version 55.0)

推荐使用 jabba或 asdf 管理 java 多版本。

观察​

执行 mvnd 命令即可观察到启动了 daemon 进程,同时该命令也充当 client 与 daemon service 交互。

执行 mvnd --status 查看 daemon 状态,如下展示的是一个空闲状态的 daemon:

liangliangdai@liangliangdai-VirtualBox:~/jacoco$ /home/liangliangdai/Downloads/mvnd-0.7.1-linux-amd64/bin/mvnd --status
ID PID Address Status RSS Last activity Java home
88a359ee 69068 inet:/127.0.0.1:36633 Idle 132m 2021-12-30T12:20:43.405 /home/liangliangdai/.jabba/jdk/openjdk@1.11.0

执行 mvnd --stop 停止 daemon。

实例测试​

事先说明​

  • 均基于 4 core linux 虚拟机
  • 均设置 Jvm args -Xms128M Xmx2048M
  • Mvnd 默认开启并行构建,线程数量为 Math.max(Runtime.getRuntime().availableProcessors() - 1, 1),为了保持一致,并行参数统一设置 -T 4,-T 4 表示开启并行构建的线程数为 4 个

Jacoco | 20 + modules​

mvn clean verify 构建成功
[INFO] Reactor Summary for root 0.8.6:
[INFO]
[INFO] JaCoCo ............................................. SUCCESS [ 2.454 s]
[INFO] JaCoCo :: Core ..................................... SUCCESS [ 10.961 s]
[INFO] JaCoCo :: Report ................................... SUCCESS [ 5.012 s]
[INFO] JaCoCo :: Agent RT ................................. SUCCESS [ 2.425 s]
[INFO] JaCoCo :: Agent .................................... SUCCESS [ 3.108 s]
[INFO] JaCoCo :: Ant ...................................... SUCCESS [ 3.423 s]
[INFO] JaCoCo :: Command Line Interface ................... SUCCESS [ 3.536 s]
[INFO] JaCoCo :: Examples ................................. SUCCESS [ 3.183 s]
[INFO] JaCoCo :: Maven Plugin ............................. SUCCESS [ 6.747 s]
[INFO] JaCoCo :: Tests .................................... SUCCESS [ 0.192 s]
[INFO] JaCoCo :: Test :: Core ............................. SUCCESS [ 6.984 s]
[INFO] JaCoCo :: Test :: Core :: Validation ............... SUCCESS [ 0.049 s]
[INFO] JaCoCo :: Test :: Core :: Validation Java 5 ........ SUCCESS [ 3.031 s]
[INFO] JaCoCo :: Test :: Core :: Validation Kotlin ........ SUCCESS [ 11.635 s]
[INFO] JaCoCo :: Test :: Core :: Validation Java 7 ........ SUCCESS [ 2.744 s]
[INFO] JaCoCo :: Test :: Core :: Validation Java 8 ........ SUCCESS [ 2.680 s]
[INFO] JaCoCo :: Test :: Core :: Validation Groovy ........ SUCCESS [ 4.787 s]
[INFO] JaCoCo :: Test :: Core :: Validation Scala ......... SUCCESS [ 10.767 s]
[INFO] JaCoCo :: Test :: Report ........................... SUCCESS [ 5.021 s]
[INFO] JaCoCo :: Test :: Agent RT ......................... SUCCESS [ 3.188 s]
[INFO] JaCoCo :: Test :: Agent ............................ SUCCESS [ 2.288 s]
[INFO] JaCoCo :: Test :: Ant .............................. SUCCESS [ 8.807 s]
[INFO] JaCoCo :: Test :: Command Line Interface ........... SUCCESS [ 4.286 s]
[INFO] JaCoCo :: Test :: Examples ......................... SUCCESS [ 17.863 s]
[INFO] JaCoCo :: Test :: Maven Plugin ..................... SUCCESS [01:59 min]
[INFO] JaCoCo :: Documentation ............................ SUCCESS [ 11.561 s]
[INFO] JaCoCo :: Distribution ............................. SUCCESS [ 1.810 s]
[INFO] root ............................................... SUCCESS [ 0.034 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:18 min
mvnd clean verify 构建出错
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.6.0:report (report) on project jacoco-maven-plugin: Execution report of goal org.apache.maven.plugins:maven-plugin-plugin:3.6.0:report failed: A required class was missing while executing org.apache.maven.plugins:maven-plugin-plugin:3.6.0:report: org/jacoco/report/check/IViolationsOutput
[ERROR] -----------------------------------------------------
[ERROR] realm = plugin>org.apache.maven.plugins:maven-plugin-plugin:3.6.0
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/home/liangliangdai/.m2/repository/org/apache/maven/plugins/maven-plugin-plugin/3.6.0/maven-plugin-plugin-3.6.0.jar
[ERROR] urls[1] = file:/home/liangliangdai/.m2/repository/org/ow2/asm/asm/7.1/asm-7.1.jar
[ERROR] urls[2] = file:/home/liangliangdai/.m2/repository/org/sonatype/aether/aether-util/1.7/aether-util-1.7.jar
[ERROR] urls[3] = file:/home/liangliangdai/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar
[ERROR] urls[4] = file:/home/liangliangdai/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
[ERROR] urls[5] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar
[ERROR] urls[6] = file:/home/liangliangdai/.m2/repository/org/sonatype/sisu/sisu-inject-bean/1.4.2/sisu-inject-bean-1.4.2.jar
[ERROR] urls[7] = file:/home/liangliangdai/.m2/repository/org/sonatype/sisu/sisu-guice/2.1.7/sisu-guice-2.1.7-noaop.jar
[ERROR] urls[8] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar
[ERROR] urls[9] = file:/home/liangliangdai/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.6.0/maven-plugin-tools-api-3.6.0.jar
[ERROR] urls[10] = file:/home/liangliangdai/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-tools-generators/3.6.0/maven-plugin-tools-generators-3.6.0.jar
[ERROR] urls[11] = file:/home/liangliangdai/.m2/repository/org/ow2/asm/asm-commons/7.0/asm-commons-7.0.jar
[ERROR] urls[12] = file:/home/liangliangdai/.m2/repository/org/ow2/asm/asm-tree/7.0/asm-tree-7.0.jar
[ERROR] urls[13] = file:/home/liangliangdai/.m2/repository/org/ow2/asm/asm-analysis/7.0/asm-analysis-7.0.jar
[ERROR] urls[14] = file:/home/liangliangdai/.m2/repository/net/sf/jtidy/jtidy/r938/jtidy-r938.jar
[ERROR] urls[15] = file:/home/liangliangdai/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-tools-java/3.6.0/maven-plugin-tools-java-3.6.0.jar
[ERROR] urls[16] = file:/home/liangliangdai/.m2/repository/com/thoughtworks/qdox/qdox/2.0-M5/qdox-2.0-M5.jar
[ERROR] urls[17] = file:/home/liangliangdai/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-tools-annotations/3.6.0/maven-plugin-tools-annotations-3.6.0.jar
[ERROR] urls[18] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-archiver/3.6.0/plexus-archiver-3.6.0.jar
[ERROR] urls[19] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-io/3.0.1/plexus-io-3.0.1.jar
[ERROR] urls[20] = file:/home/liangliangdai/.m2/repository/commons-io/commons-io/2.6/commons-io-2.6.jar
[ERROR] urls[21] = file:/home/liangliangdai/.m2/repository/org/apache/commons/commons-compress/1.16.1/commons-compress-1.16.1.jar
[ERROR] urls[22] = file:/home/liangliangdai/.m2/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar
[ERROR] urls[23] = file:/home/liangliangdai/.m2/repository/org/iq80/snappy/snappy/0.4/snappy-0.4.jar
[ERROR] urls[24] = file:/home/liangliangdai/.m2/repository/org/tukaani/xz/1.8/xz-1.8.jar
[ERROR] urls[25] = file:/home/liangliangdai/.m2/repository/org/apache/maven/plugin-tools/maven-plugin-annotations/3.6.0/maven-plugin-annotations-3.6.0.jar
[ERROR] urls[26] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-sink-api/1.4/doxia-sink-api-1.4.jar
[ERROR] urls[27] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-logging-api/1.4/doxia-logging-api-1.4.jar
[ERROR] urls[28] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-site-renderer/1.4/doxia-site-renderer-1.4.jar
[ERROR] urls[29] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-core/1.4/doxia-core-1.4.jar
[ERROR] urls[30] = file:/home/liangliangdai/.m2/repository/xerces/xercesImpl/2.9.1/xercesImpl-2.9.1.jar
[ERROR] urls[31] = file:/home/liangliangdai/.m2/repository/xml-apis/xml-apis/1.3.04/xml-apis-1.3.04.jar
[ERROR] urls[32] = file:/home/liangliangdai/.m2/repository/org/apache/httpcomponents/httpclient/4.0.2/httpclient-4.0.2.jar
[ERROR] urls[33] = file:/home/liangliangdai/.m2/repository/commons-codec/commons-codec/1.3/commons-codec-1.3.jar
[ERROR] urls[34] = file:/home/liangliangdai/.m2/repository/org/apache/httpcomponents/httpcore/4.0.1/httpcore-4.0.1.jar
[ERROR] urls[35] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-decoration-model/1.4/doxia-decoration-model-1.4.jar
[ERROR] urls[36] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-module-xhtml/1.4/doxia-module-xhtml-1.4.jar
[ERROR] urls[37] = file:/home/liangliangdai/.m2/repository/org/apache/maven/doxia/doxia-module-fml/1.4/doxia-module-fml-1.4.jar
[ERROR] urls[38] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-i18n/1.0-beta-7/plexus-i18n-1.0-beta-7.jar
[ERROR] urls[39] = file:/home/liangliangdai/.m2/repository/org/apache/velocity/velocity-tools/2.0/velocity-tools-2.0.jar
[ERROR] urls[40] = file:/home/liangliangdai/.m2/repository/commons-beanutils/commons-beanutils/1.7.0/commons-beanutils-1.7.0.jar
[ERROR] urls[41] = file:/home/liangliangdai/.m2/repository/commons-digester/commons-digester/1.8/commons-digester-1.8.jar
[ERROR] urls[42] = file:/home/liangliangdai/.m2/repository/commons-chain/commons-chain/1.1/commons-chain-1.1.jar
[ERROR] urls[43] = file:/home/liangliangdai/.m2/repository/commons-logging/commons-logging/1.1/commons-logging-1.1.jar
[ERROR] urls[44] = file:/home/liangliangdai/.m2/repository/javax/servlet/servlet-api/2.3/servlet-api-2.3.jar
[ERROR] urls[45] = file:/home/liangliangdai/.m2/repository/dom4j/dom4j/1.1/dom4j-1.1.jar
[ERROR] urls[46] = file:/home/liangliangdai/.m2/repository/oro/oro/2.0.8/oro-2.0.8.jar
[ERROR] urls[47] = file:/home/liangliangdai/.m2/repository/sslext/sslext/1.2-0/sslext-1.2-0.jar
[ERROR] urls[48] = file:/home/liangliangdai/.m2/repository/org/apache/struts/struts-core/1.3.8/struts-core-1.3.8.jar
[ERROR] urls[49] = file:/home/liangliangdai/.m2/repository/antlr/antlr/2.7.2/antlr-2.7.2.jar
[ERROR] urls[50] = file:/home/liangliangdai/.m2/repository/org/apache/struts/struts-taglib/1.3.8/struts-taglib-1.3.8.jar
[ERROR] urls[51] = file:/home/liangliangdai/.m2/repository/org/apache/struts/struts-tiles/1.3.8/struts-tiles-1.3.8.jar
[ERROR] urls[52] = file:/home/liangliangdai/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar
[ERROR] urls[53] = file:/home/liangliangdai/.m2/repository/org/apache/maven/reporting/maven-reporting-impl/2.3/maven-reporting-impl-2.3.jar
[ERROR] urls[54] = file:/home/liangliangdai/.m2/repository/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar
[ERROR] urls[55] = file:/home/liangliangdai/.m2/repository/org/apache/maven/shared/maven-shared-utils/0.6/maven-shared-utils-0.6.jar
[ERROR] urls[56] = file:/home/liangliangdai/.m2/repository/com/google/code/findbugs/jsr305/2.0.1/jsr305-2.0.1.jar
[ERROR] urls[57] = file:/home/liangliangdai/.m2/repository/commons-validator/commons-validator/1.3.1/commons-validator-1.3.1.jar
[ERROR] urls[58] = file:/home/liangliangdai/.m2/repository/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar
[ERROR] urls[59] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar
[ERROR] urls[60] = file:/home/liangliangdai/.m2/repository/org/codehaus/plexus/plexus-velocity/1.1.8/plexus-velocity-1.1.8.jar
[ERROR] urls[61] = file:/home/liangliangdai/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[ERROR] urls[62] = file:/home/liangliangdai/.m2/repository/org/apache/maven/surefire/maven-surefire-common/2.19.1/maven-surefire-common-2.19.1.jar
[ERROR] urls[63] = file:/home/liangliangdai/.m2/repository/org/apache/maven/surefire/surefire-api/2.19.1/surefire-api-2.19.1.jar
[ERROR] urls[64] = file:/home/liangliangdai/.m2/repository/org/apache/maven/surefire/surefire-booter/2.19.1/surefire-booter-2.19.1.jar
[ERROR] urls[65] = file:/home/liangliangdai/.m2/repository/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar
[ERROR] urls[66] = file:/home/liangliangdai/.m2/repository/org/apache/velocity/velocity/1.7/velocity-1.7.jar
[ERROR] urls[67] = file:/home/liangliangdai/.m2/repository/commons-lang/commons-lang/2.4/commons-lang-2.4.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import from realm ClassRealm[maven.api, parent: null]]
[ERROR]
[ERROR] -----------------------------------------------------
[ERROR] : org.jacoco.report.check.IViolationsOutput
[ERROR] -> [Help 1]

并行构建可能导致失败。

Sonar-java | 10 ~ modules​

mvn-single: maven3 - single thread build - mvn clean verify

次数时间
102:27 min
202:28 min

mvn-multi: maven3 - multi thread build - mvn clean verify -T 4

Maven3 的 并行构建。

次数时间
102:18 min
202:23 min

mvnd-multi-cold: mvnd - multi thread build - "cold" daemon - mvnd clean verify -T 4

"cold" daemon 是指使用 mvnd --stop 停止 daemon 后进行构建。

次数时间
102:13 min
202:08 min

mvnd-multi-hot: mvnd - multi thread build - "hot" daemon - mvnd clean verify -T 4

"hot" daemon 是指 daemon 已经构建过几次。

次数时间
101:56 min
201:51 min

从 mvnd-multi-hot 与 mvn-single 可得最优情况下 mvnd 大约可以为此项目提高 20% 的构建速度。

结论及分析​

C/S 和 smart builder 是其快的原因​

Mvnd 借鉴 Gradle daemon 的思想,使 daemon 常驻后台,构建请求由 GraalVM client 转发,得益于 classloader cache 和 JIT 的特性, 下次构建无需重复加载已缓存的 plugin classes,同时热点代码信息也被记录,更快甚至直接 JIT不像 maven 每次构建都需要新建 jvm,故 C/S 架构是 mvnd 构建快的关键因素之一。

mvnd-multi-hot 优于 mvnd-multi-cold 的结果,很好的体现了 daemon 的优势。

Mvnd 借鉴 Takari smart builder 的技术,相比与 maven3 并行构建模型更优。此为 mvnd 构建快的另一关键因素。下图展示了 maven 3 和 mvnd 并行构建模型的区别: smartbuilder.png

mvnd-multi-cold,mvn-multi 双方都是并行构建且从 "cold" 启动,mvnd 略优,故优势很可能来自 smart build,当然样本数太少,不足以下定论,读者可自行测试。

Mvnd 适合本地构建,CI 有一定潜力​

当今主流的 CI 大多基于容器,目前 mvnd daemon 只支持来自本机 client 的构建请求,即各个 job 所属的容器只能利用自身启动的 daemon, 假如容器内只进行单次构建的话那就没那么大优势了,而本地构建无此限制,社区有类似的 讨论。

mvnd is primarily designed for iterative development on a developer workstation. I think it is worth trying as a drop-in replacement of stock Maven. In case of issues, the users are invited to report them in the project. I see little potential for mvnd in the area of continuous integration (CI).
来源: Conversation with Peter Palaga and Guillaume Nodet

并行构建不能无脑使用​

请自行阅读 并行构建可能带来的问题 以及作者向社区反馈的 问题。

参考​

· 3 min read
Shixu

What is Maven​

TL;DR

Lifecycles, phases and (plugin) goals​

Concepts​

Similar to gitlab pipelines,stages and jobs

  • Three built-in build lifecycles: default , clean and site

  • When we run a phase – all goals bound to this phase are executed in order. This won't only execute the specified phase but all the preceding phases as well

E.g. mvn clean install

  • A goal can bound to one or more build phases. A build phase can also have zero or more goals bound to it

default lifecycle bindings

  • A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation

E.g. mvn dependency:tree

Look up phases and goals​

Use mvn fr.jcgay.maven.plugins:buildplan-maven-plugin:list to list phases and goals in default lifecycle

Direct/Transitive dependency​

A -> B -> C , B, C is A's dependency, B is A's direct dependency, C is A's transitive dependency

Dependency conflict​

The different versions of same dependency are all needed by others

Dependency mediation​

  • Nearest definition

A -> B -> C -> D 2.0 and A -> E -> D 1.0 result will be D 1.0

  • First declaration
// A.xml
<dependecy>B</dependency>
<dependecy>C</dependency>

A -> B -> D 2.0 and A -> C -> D 1.0 result will be D 2.0

Solution​

  • According dependency mediation if have compatible versions
  • Use shade plugin if have not uncompatible versions

Best practice​

  • Although transitive dependencies can implicitly include desired dependencies, it is a good practice to explicitly specify the dependencies your source code uses directly. This best practice proves its value especially when the dependencies of your project change their dependencies

  • Use a dependencyManagement section in a parent pom.xml to avoid duplicating dependency information in child projects

  • The phases named with hyphenated-words (pre-, post-, or process-*) are not usually directly called from the command line. These phases sequence the build, producing intermediate results that are not useful outside the build. In the case of invoking integration-test, the environment may be left in a hanging state

Commonly used​

Commands​

  • Help making best practice more achievable: mvn dependency:analyze

  • Run ut singly: mvn -DfailIfNoTests=false -pl product.profile-service -Dtest=CheckSetHotelValidatorTest -am test

  • Download artifact: mvn dependency:get -DremoteRepositories=http://repo1.maven.org/maven2/ -DgroupId=com.google.code.gson -DartifactId=gson -Dversion=2.8.8

Refs​

· 14 min read
Shixu

过去几年里我一直使用Java。最近,用Go建立了一个小项目,然而 Go 生态系统中依赖注入(DI)功能缺乏让我震惊。于是我决定尝试使用 Uber 的 dig 库来构建我的项目,期间感触颇深。

我发现 DI 帮助我解决了之前在 Go 应用程序中遇到的很多问题 - 过度使用 init 函数,滥用全局变量和复杂的应用程序设置等。

在这篇文章中,我将介绍 DI ,然后在使用 DI 框架(通过 dig 库)前后写一些例子做对比。

DI 的简要概述​

依赖注入是指你的组件(通常在 go 中是 struct )在创建时,就应该获取它们依赖关系的一种思想。这与那些组件在初始化过程中,就建立自身依赖关系的反关联模式不同 。我们来看一个例子。

假设你构造Server 需要 Config 结构体。一种方法是在初始化期间 Server 构建 Config 。

type Server struct {
config *Config
}
func New() *Server {
return &Server{
config: buildMyConfigSomehow(),
}
}

看起来很方便。调用者甚至不必知道 Server 需要访问 Config 。这些都被我们的函数隐藏起来了。

然而,这存在一些缺点。首先,如果我们想要改变我们 Config 的构建方式,我们不得不改变所有调用构建代码的地方。例如,假设我们的 buildMyConfigSomehow 函数现在需要一个参数。每个调用处都需要访问该参数并需要将其传递给构造函数。

此外,这使得实现 Config 函数变得十分麻烦,我们得以某种方法进入 new 函数的内部,并创建Config 。

这是 DI 方式:

type Server struct {
config *Config
}
func New(config *Config) *Server {
return &Server{
config: config,
}
}

现在我们将 Server 与Config 分离 。我们可以根据自己的逻辑创造 Config 然后将结果传递给 New 函数。

此外,如果 Config 是一个接口,这为我们提供了一个简单的模拟途径 。只要 New 实现了我们的接口,就可以传递任何我们想要的东西。这使得测试实现了 Config 接口的 Server 很简单。

令人痛苦的是在创建 server 之前手动创建 config 。我们在这里创建了一个依赖关系 – 因为 server 依赖 Config, 所以需要首先创建 Config 。在真正的应用程序中,这些依赖会变得更加复杂,这会导致构建应用程序完成其工作所需的组件间的复杂逻辑 。

这是 DI 框架可以提供帮助的地方。 DI 框架通常提供两个功能:

  1. “提供”新组件。简而言之,这告诉 DI 框架一旦你有这些组件,还需要其他什么组件(依赖关系)以及如何去构建。
  2. “检索”构建组件。

DI 框架通常基于您告诉它的“ providers ”构建依赖图并确定如何构建对象。这在没有具体例子的情况下很难理解,所以让我们来看一个中等大小的例子。

示例程序​

我们来看http服务器端的代码 :客户端以 GET 方式请求 /people 路径时并返回 JSON 。我们将一步一步呈现代码,为简单起见,它们都存在于同一个包中(main)。请勿在真正的 Go 程序中执行此操作。可以在此处找到此示例的完整代码。

首先,让我们看看我们的 Person 。仅有一些被 JSON 标签标记的属性。

type Person struct {
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}

Person 有 Id,Name 和 Age 。

接下来让我们看看 Config 。与 Person 类似,它没有依赖关系。与 Person 不同的是,我们将提供构造函数。

type Config struct {
Enabled bool
DatabasePath string
Port string
}
func NewConfig() *Config {
return &Config{
Enabled: true,
DatabasePath: "./example.db",
Port: "8000",
}
}

Enabled 表示程序是否返回真实数据。DatabasePath 表示数据库的地址(使用 sqlite )。Port 表示服务器运行的端口。

下方函数用来打开数据库连接。它依赖于 Config 并返回 *sql.DB 。

接下来看看 PersonRepository。此结构负责从数据库中提取数据并反序列化为 Person 。

type PersonRepository struct {
database *sql.DB
}
func (repository *PersonRepository) FindAll() []*Person {
rows, _ := repository.database.Query(
`SELECT id, name, age FROM people;`
)
defer rows.Close()
people := []*Person{}
for rows.Next() {
var (
id int
name string
age int
)
rows.Scan(&id, &name, &age)
people = append(people, &Person{
Id: id,
Name: name,
Age: age,
})
}
return people
}
func NewPersonRepository(database *sql.DB) *PersonRepository {
return &PersonRepository{database: database}
}

PersonRepository的构建需要数据库连接。它有一个函数FindAll,此函数使用数据库连接信息并返回 Person 列表。

要在 HTTP 服务器和 PersonRepository 之间提供一层,我们需要创建 PersonService 。

type PersonService struct {
config *Config
repository *PersonRepository
}
func (service *PersonService) FindAll() []*Person {
if service.config.Enabled {
return service.repository.FindAll()
}
return []*Person{}
}
func NewPersonService(config *Config, repository *PersonRepository)
*PersonService {
return &PersonService{config: config, repository: repository}
}

我们的 PersonService 依赖于 Config 和 PersonRepository 。它有一个函数 FindAll ,如果启用了应用程序,则会有条件地调用 PersonRepository 。

最后,我们得到了 Server 。负责运行 HTTP 服务器并将适当的请求委托给 PersonService 。

type Server struct {
config *Config
personService *PersonService
}
func (s *Server) Handler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/people", s.people)
return mux
}
func (s *Server) Run() {
httpServer := &http.Server{
Addr: ":" + s.config.Port,
Handler: s.Handler(),
}
httpServer.ListenAndServe()
}
func (s *Server) people(w http.ResponseWriter, r *http.Request) {
people := s.personService.FindAll()
bytes, _ := json.Marshal(people)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(bytes)
}
func NewServer(config *Config, service *PersonService) *Server {
return &Server{
config: config,
personService: service,
}
}

Server 取决于 PersonService 和 Config 。

好的,我们了解了系统的所有组件。现在我们该如何在实际中初始化它们并启动我们的系统?

传统的 main()​

首先,让我们用传统方式编写 main() 。

func main() {
config := NewConfig()
db, err := ConnectDatabase(config)
if err != nil {
panic(err)
}
personRepository := NewPersonRepository(db)
personService := NewPersonService(config, personRepository)
server := NewServer(config, personService)
server.Run()
}

首先,我们创建 Config 。然后使用 Config 创建数据库连接。从而创建 PersonRepository 和PersonService 。最后,再创建 Server 并运行它。

这有些复杂。更糟糕的是,随着我们的应用程序的变得复杂, main 的复杂性也将继续增长。每次我们向任何组件添加新的依赖时,都必须通过 main 函数中的排序和逻辑来反映该依赖,以构建该组件。

您可能已经猜到,依赖注入框架可以帮助我们解决这个问题。一起来看看。

创建容器​

术语“ 容器(container) ”通常用在 DI 框架中,用于描述添加“ 提供者(providers) ”的内容,并从中请求构建对象。 dig 库用 Provide 函数为我们添加“ providers ”, Invoke 函数用于从容器中检索全部的构建对象。

首先,我们构建一个新容器。

container := dig.New()

现在我们可以添加新的提供者。为此,我们在容器上调用 Provide 函数。它只需要一个参数:一个函数。此函数可以包含任意数量的参数(表示要创建的组件的依赖关系)和一个或两个返回值(表示函数提供的组件以及可选的错误)。

container.Provide(func() *Config {
return NewConfig()
})

上面的代码说“我为容器提供了一种 Config 类型。为了构建它,我不需要任何其他东西。“现在我们已经向容器展示了如何构建 Config 类型,继续使用它来构建其他类型。

container.Provide(func(config *Config) (*sql.DB, error) {
return ConnectDatabase(config)
})

这段代码说“我为容器提供了一种 *sql.DB 类型。为了构建它,我需要一个 Config 。可以选择返回错误。“

在这两种情况下,我们没必要这样写。因为我们已经有了 NewConfig 和 ConnectDatabase 函数,我们可以直接使用他们作为容器的提供者。

container.Provide(NewConfig)
container.Provide(ConnectDatabase)

现在,我们可以从之前给容器提供的类型中创建组件。我们使用 Invoke 函数,函数采用单个参数 - 具有任意数量参数的函数。函数的参数是我们希望容器构建的类型。

container.Invoke(func(database *sql.DB) {
// sql.DB is ready to use here
})

容器做了一些非常聪明的东西,如下:

  • 容器认识到我们要求的是构建 *sql.DB
  • 它确定函数 ConnectDatabase 提供该类型
  • 接下来它确定 ConnectDatabase 函数依赖 Config
  • 它找到了 Config 的提供者,也就是 NewConfig
  • NewConfig 没有任何依赖关系,所以它被调用
  • NewConfig 的结果是一个 Config 传递给 ConnectDatabase
  • ConnectionDatabase 的结果是 *sql.DB 被传递给 Invoke

这是容器为我们做的很多工作。事实上,它做的更多。容器很智能,可以构建每种类型有且仅有一个实例。这意味着如果我们在多个地方(比如多个存储库)使用它,我们永远不会意外地创建第二个数据库连接。

较好的 main()写法​

现在知道了 dig 容器是如何工作的,让我们用它来构建一个较好的main 。

func BuildContainer() *dig.Container {
container := dig.New()
container.Provide(NewConfig)
container.Provide(ConnectDatabase)
container.Provide(NewPersonRepository)
container.Provide(NewPersonService)
container.Provide(NewServer)
return container
}
func main() {
container := BuildContainer()
err := container.Invoke(func(server *Server) {
server.Run()
})
if err != nil {
panic(err)
}
}

之前唯一没见过的就是 Invoke 的返回值 error 。如果任何提供者使用 Invoke 返回错误,我们调用 Invoke 将停止并返回该错误。

虽然这个例子很小,但应该很容易看出这种方法的一些好处超过了“常规“的 main 。随着应用程序变得越来越大,这些好处变得更加明显。

最重要的好处之一是将组件的创建与其依赖的创建分离。比如说,我们 PersonRepository 现在需要访问 Config 。我们所要做的就是更改 NewPersonRepository 构造函数以包含 Config 作为参数。代码其他任何内容没有发生改变。

其他的好处是没有全局状态,没有调用 init (依赖关系在需要时才创建,只创建一次,不需要容易出错的 init 设置),并且易于测试单个组件。想象一下,在测试中创建容器并要求完整构建对象进行测试。或者,创建一个对象需要所有的依赖。使用 DI ,这些都更容易。

一个值得传播的想法​

我相信依赖注入有助于构建更强大和可测试的应用程序。随着这些应用程序体量逐渐增大,尤为明显。 Go 非常适合构建大型应用程序,并且具有很好的 DI 工具 dig 。我相信 Go 社区应该接受 DI 并在更多的应用程序中使用它。

原文:dependency-injection-in-go