区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令一

  区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。

Fabric 1.0源代码笔记 之 Peer #peer根命令入口及加载子命令

1、加载环境变量配置和配置文件

Fabric支持通过环境变量对部分配置进行更新,如:CORE_LOGGING_LEVEL为输出的日志级别、CORE_PEER_ID为Peer的ID等。
此部分功能由第三方包viper来实现,viper除支持环境变量的配置方式外,还支持配置文件方式。
如下代码为加载环境变量配置,其中cmdRoot为"core",即CORE_开头的环境变量。

viper.SetEnvPrefix(cmdRoot)
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
//代码在peer/main.go

加载配置文件,同样由第三方包viper来实现,具体代码如下:
其中cmdRoot为"core",即/etc/hyperledger/fabric/core.yaml。

err := common.InitConfig(cmdRoot) 
//代码在peer/main.go

如下代码为common.InitConfig(cmdRoot)的具体实现:

config.InitViper(nil, cmdRoot)
err := viper.ReadInConfig()
//代码在peer/common/common.go

另附config.InitViper(nil, cmdRoot)的代码实现:
优先从环境变量FABRIC_CFG_PATH中获取配置文件路径,其次为当前目录、开发环境目录(即:src/github.com/hyperledger/fabric/sampleconfig)、和OfficialPath(即:/etc/hyperledger/fabric)。
AddDevConfigPath是对addConfigPath的封装,目的是通过GetDevConfigDir()调取sampleconfig路径。

var altPath = os.Getenv("FABRIC_CFG_PATH")
if altPath != "" {
    addConfigPath(v, altPath)
} else {
    addConfigPath(v, "./")
    err := AddDevConfigPath(v)
    addConfigPath(v, OfficialPath)
}
viper.SetConfigName(configName)
//代码在core/config/config.go

2、加载命令行工具和命令

Fabric支持类似peer node start、peer channel create、peer chaincode install这种命令、子命令、命令选项的命令行形式。
此功能由第三方包cobra来实现,以peer chaincode install -n test_cc -v 1.0 -p
其中peer、chaincode、install、-n分别为命令、子命令、子命令的子命令、命令选项。

如下代码为mainCmd的初始化,其中Use为命令名称,PersistentPreRunE先于Run执行用于初始化日志系统,Run此处用于打印版本信息或帮助信息。

var mainCmd = &cobra.Command{
    Use: "peer",
    PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
        loggingSpec := viper.GetString("logging_level")

        if loggingSpec == "" {
            loggingSpec = viper.GetString("logging.peer")
        }
        flogging.InitFromSpec(loggingSpec) //初始化flogging日志系统

        return nil
    },
    Run: func(cmd *cobra.Command, args []string) {
        if versionFlag {
            fmt.Print(version.GetInfo())
        } else {
            cmd.HelpFunc()(cmd, args)
        }
    },
}
//代码在peer/main.go

如下代码为添加命令行选项,-v, --version、--logging-level和--test.coverprofile分别用于版本信息、日志级别和测试覆盖率分析。

mainFlags := mainCmd.PersistentFlags()
mainFlags.BoolVarP(&versionFlag, "version", "v", false, "Display current version of fabric peer server")
mainFlags.String("logging-level", "", "Default logging level and overrides, see core.yaml for full syntax")
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
testCoverProfile := ""
mainFlags.StringVarP(&testCoverProfile, "test.coverprofile", "", "coverage.cov", "Done")
//代码在peer/main.go

如下代码为逐一加载peer命令下子命令:node、channel、chaincode、clilogging、version。

mainCmd.AddCommand(version.Cmd())
mainCmd.AddCommand(node.Cmd())
mainCmd.AddCommand(chaincode.Cmd(nil))
mainCmd.AddCommand(clilogging.Cmd(nil))
mainCmd.AddCommand(channel.Cmd(nil))
//代码在peer/main.go 

mainCmd.Execute()为命令启动。

3、初始化日志系统(输出对象、日志格式、日志级别)

如下为初始日志系统代码入口,其中loggingSpec取自环境变量CORE_LOGGING_LEVEL或配置文件中logging.peer,即:全局的默认日志级别。

flogging.InitFromSpec(loggingSpec)
//代码在peer/main.go

未完待续感谢关注兄弟连区块链教程分享!

猜你喜欢

转载自blog.51cto.com/14041296/2313473
今日推荐