本文主要研究一下kingbus的main.gonode
kingbus/cmd/kingbus/main.gogit
package main import ( "flag" "fmt" "os" "os/signal" "syscall" "github.com/flike/kingbus/config" "github.com/flike/kingbus/log" "github.com/flike/kingbus/server" ) var ( //BuildDate used for generating build date in make command BuildDate string //BuildVersion used for generating kingbus version in make command BuildVersion string ) const banner string = ` __ _ __ / /__(_)___ ____ _/ /_ __ _______ / //_/ / __ \/ __ '/ __ \/ / / / ___/ / ,< / / / / / /_/ / /_/ / /_/ (__ ) /_/|_/_/_/ /_/\__, /_.___/\__,_/____/ /____/ ` func main() { configFile := flag.String("config", "./kingbus.yaml", "kingbus config file") printVersion := flag.Bool("version", false, "print kingbus version info") flag.Parse() //only print version if *printVersion { fmt.Printf("version is %s, build at %s\n", BuildVersion, BuildDate) return } //fmt.Print(banner) fmt.Printf("version is %s, build at %s\n", BuildVersion, BuildDate) if len(*configFile) == 0 { fmt.Println("must use a config file") return } serverCfg, err := config.NewKingbusServerConfig(*configFile) if err != nil { fmt.Printf("NewKingbusServerConfig error,err:%s\n", err.Error()) return } //init log log.InitLoggers(serverCfg.LogDir, serverCfg.LogLevel) defer log.UnInitLoggers() ks, err := server.NewKingbusServer(serverCfg) if err != nil { log.Log.Fatalf("main:NewKingbusServer error,err:%s", err) } sc := make(chan os.Signal, 1) signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, ) go func() { for { sig := <-sc if sig == syscall.SIGINT || sig == syscall.SIGTERM || sig == syscall.SIGQUIT { ks.Stop() os.Exit(0) } } }() ks.Run() }
kingbus/config/config.gogithub
func NewKingbusServerConfig(configPath string) (*KingbusServerConfig, error) { if configPath == "" { return nil, errors.New("config path is nil") } viper.SetConfigFile(configPath) if err := viper.ReadInConfig(); err != nil { return nil, err } //get raft node config raftNodeCfg, err := getRaftNodeConfig() if err != nil { return nil, err } adminURLs, err := types.NewURLs([]string{viper.GetString("admin-url")}) if err != nil { return nil, err } if len(adminURLs) != 1 { return nil, fmt.Errorf("adminURLs muster be 1") } ksCfg := &KingbusServerConfig{ RaftNodeCfg: *raftNodeCfg, AdminURLs: adminURLs, MetricsAddr: viper.GetString("metrics-addr"), LogDir: viper.GetString("log-dir"), LogLevel: viper.GetString("log-level"), } return ksCfg, nil }
kingbus/server/server.goui
func NewKingbusServer(cfg *config.KingbusServerConfig) (*KingbusServer, error) { s := new(KingbusServer) s.Cfg = cfg s.lead = atomic.NewUint64(0) err := s.starRaft(s.Cfg.RaftNodeCfg) if err != nil { log.Log.Errorf("NewKingbusServer:startRaft error,err:%s", err) return nil, err } err = s.startRaftPeer(s.Cfg.RaftNodeCfg.PeerURLs) if err != nil { log.Log.Errorf("NewKingbusServer:startRaftPeer error,err:%s", err) return nil, err } err = s.startAdminServer(s.Cfg.AdminURLs) if err != nil { log.Log.Errorf("NewKingbusServer:startAdminServer error,err:%s", err) return nil, err } err = s.newBinlogProgress() if err != nil { log.Log.Errorf("NewKingbusServer:newBinlogProgress error,err:%s", err) return nil, err } err = s.startPrometheus(s.Cfg.MetricsAddr) if err != nil { log.Log.Errorf("NewKingbusServer:startPrometheus error,err:%s", err) return nil, err } s.started = atomic.NewBool(false) return s, nil }
kingbus的main方法讀取config、version參數,而後經過config.NewKingbusServerConfig(*configFile)獲取serverCfg,以後經過server.NewKingbusServer(serverCfg)建立KingbusServer,而後執行ks.Run()atom