下载源码
github
注意etcd的导入路径和github地址是不相符的。
导入路径:go.etcd.io/etcd
bbolt也是:go.etcd.io/bbolt
编译
make build
会在bin目录下生成etcd和etcdctl
./bin/etcd --version
etcd Version: 3.5.0-pre
Git SHA: 3ef2ad8e1
Go Version: go1.12.7
Go OS/Arch: darwin/amd64
./bin/etcdctl version
etcdctl version: 3.5.0-pre
API version: 3.5
入口文件
etcd: ./main.go
etcdctl: ./etcdctl/main.go
先只关心etcd吧,etcdctl和clientv3以后再说
main.go
package main
import "go.etcd.io/etcd/etcdmain"
func main() {
etcdmain.Main()
}
非常简单, cobra风格的,入口main函数啥也不干,调用命令行解析函数
main_test.go
func TestMain(t *testing.T) {
// don't launch etcd server when invoked via go test
// Note: module name has /v3 now
if strings.HasSuffix(os.Args[0], "v3.test") {
t.Skip("skip launching etcd server when invoked via go test")
}
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "-test.") {
t.Skip("skip launching etcd server when invoked via go test")
}
notifier := make(chan os.Signal, 1)
signal.Notify(notifier, syscall.SIGINT, syscall.SIGTERM)
go main()
<-notifier
}
逻辑很简单,就是启动etcd server直到收到信号,那么主协程退出,进程也就退出了。
要做test不能只是简单的go test,具体的test要看Makefile和./test脚本。暂时略过,只看程序的主题结构。