1. 前言
Traefik版本为2.5.2,从今天开始,计划陆续看完Traefik的核心部分的源码。
本文是第一部分,Traefik的程序启动分析。
2. 主流程
main.go在cmd/traefik/traefik.go内,其主要流程为:
先构造3个Command启动器,分别是traefik,healthcheck,version,Command结构如下:
源码文件:github.com/traefik/paerser@v0.1.4/cli/commands.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package cli type Command struct { Name string Description string Configuration interface{} Resources []ResourceLoader Run func([]string) error CustomHelpFunc func(io.Writer, *Command) error Hidden bool // AllowArg if not set, disallows any argument that is not a known command or a sub-command. AllowArg bool subCommands []*Command } |
可以看到,每个Command都可以配置名字,资源,Run函数等。
然后通过cli.Execute(cmdTraefik)来启动上面3个服务,也就是调用Command内的Run函数。
ResourceLoader是用于配置的加载,有文件,参数,环境变量3种加载方法,下面会提到。