jelex@jelexxudeMacBook-Pro polaris % pwd
/Users/jelex/GolandProjects/polaris
jelex@jelexxudeMacBook-Pro polaris % go mod tidy
。。。。 报错:说我本机安装的golang 版本是 1.20, 但是这个项目mod文件中使用的版本是1.21,一看,确实如此:这2年懈怠了,没能及时更新golang版本,所以去官网下载最新的1.22.5版本,覆盖安装。。。
多说一句:后面运行发现有个警告:
WARNING: undefined behavior - version of Delve is too old for Go version 1.22.5 (maximum supported version 1.20)
同理下载最新即可:又是懈怠引起的问题, 说好的太想进步呢…
jelex@jelexxudeMacBook-Pro polaris % go install github.com/go-delve/delve/cmd/dlv@latest
准备新版本golang后,再次下载依赖:
jelex@jelexxudeMacBook-Pro polaris % go mod tidy
go: downloading github.com/polarismesh/specification v1.5.0
go: downloading google.golang.org/grpc v1.58.3
go: downloading go.uber.org/zap v1.23.0
go: downloading google.golang.org/protobuf v1.33.0
。。 。。。 。。。
jelex@jelexxudeMacBook-Pro polaris % go mod download
初步梳理下层次:
你没看错:main入口就一行代码,这是因为项目使用 cobra:
(Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.
)它和viper似乎联系密切:
main方法解释:In a Cobra app, typically the main.go file is very bare. It serves one purpose: to initialize Cobra.
准备知识:
In the following example, 'server' is a command, and 'port' is a flag:
hugo server --port=1313
Command is the central point of the application. Each interaction
that the application supports will be contained in a Command.
A command can have children commands and optionally run an action.
root command 有 3个children commands:
把读取到配置路径 保存 在 configFilePath 中:
注意这个类:因为里面有bootstrap.Start(configFilePath)
启动初步分析:从main() debug进入,一步步往下走:
还加了以下脚本工具的completion命令功能:
真正准备执行:
如果直接运行main(), 不加参数,会报错:
polaris server
Usage:
polaris-server [command]
Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
revision print revision with building date
start start running
version print version
Flags:
-h, --help help for polaris-server
Use "polaris-server [command] --help" for more information about a command.
Exiting.
其中的help completion 是程序默认给我们添加上的。
所以添加上 start 参数再运行:
根据args找到要执行的command:
正好对应我们一开始 start.go文件中的startCmd。。。
添加之后;就不会被下面822行拦截住(正是首次不加任何参数运行报错之处):
继续往下走:
之后就会调用到我们自己创建Command的函数点:
然后又报错了:说没找到配置文件:
2024/07/16 15:30:29 maxprocs: Leaving GOMAXPROCS=8: CPU quota undefined
[INFO] load config from conf/polaris-server.yaml
[ERROR] open conf/polaris-server.yaml: no such file or directory
[ERROR] load config fail
Exiting.
分析:还记得上面说过的start.go中的 init() 吗?要读取配置文件,但是没找到(因为我们没有放),
所以要么自己运行时指定c参数,要么在conf目录下放置polaris-server.yaml文件:
重新指定参数:这个配置文件本来就有,随着src.zip下载而来
start -c=release/conf/polaris-server.yaml
启动成功:FYI: 如果本地正启着polaris,需要先退出相关进程(ps -ef | grep polaris)
至此完成了polaris server的启动。但也仅此而已,因为没有启动console,所以没有办法打开localhost:8080查看控制台,也没有promethus + pushgateway, limiter限流功能…
而且本篇也只是初步启动分析,还没分析bootstrap里面的逻辑.故此篇为 环境启动运行.