1

本文主要研究一下kingbus的main.go

main.go

kingbus/cmd/kingbus/main.go

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()
}
  • main方法读取config、version参数,然后通过config.NewKingbusServerConfig(*configFile)获取serverCfg,之后通过server.NewKingbusServer(serverCfg)创建KingbusServer,然后执行ks.Run()

config.go

kingbus/config/config.go

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
}
  • NewKingbusServerConfig方法通过viper来解析配置文件;KingbusServerConfig定义了RaftNodeCfg、AdminURLs、MetricsAddr、LogDir、LogLevel属性

server.go

kingbus/server/server.go

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
}
  • NewKingbusServer主要是通过KingbusServerConfig创建KingbusServer;这里执行了KingbusServer的starRaft、startRaftPeer、startAdminServer、newBinlogProgress、startPrometheus

小结

kingbus的main方法读取config、version参数,然后通过config.NewKingbusServerConfig(*configFile)获取serverCfg,之后通过server.NewKingbusServer(serverCfg)创建KingbusServer,然后执行ks.Run()

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论