// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package main import ( "flag" "fmt" "gd_vehicle/impl" "gd_vehicle/rpc_apis" "gd_vehicle/thirdparty" dutils "gd_vehicle/utils" "log" "os" "os/signal" "strings" "syscall" "time" "gd_vehicle/common.in/cache" "gd_vehicle/common.in/clinit" "gd_vehicle/common.in/config" "gd_vehicle/common.in/logger" "gd_vehicle/common.in/task" "gd_vehicle/common.in/utils" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" "github.com/smallnest/rpcx/server" "github.com/smallnest/rpcx/serverplugin" gconfig "gd_vehicle/common.in/config" "github.com/rcrowley/go-metrics" "gopkg.in/ini.v1" ) var ( // 这里可以改默认值 configFile = flag.String("config", "/etc/gd_vehicle/app.conf", "config file location") version = flag.Bool("version", false, "config file location") GitCommit = "library-import" Version = "library-import" ) func showVersion() { fmt.Println("Version: ", Version) fmt.Println("GitCommit:", GitCommit) } func prepare(projectName, runmode, key, logDir string, etcdAddrs []string, discoveryType string) { var conf *config.Configure if discoveryType == "k8s" { conf = config.GetConfigForK8s() if conf == nil { fmt.Printf("get conf failed\n\n") os.Exit(1) } rpc_apis.InitForK8s(conf) } else { // 先行于读配置 clinit.InitEtcd(etcdAddrs) utils.SetEtcdKeyApi(clinit.GetEtcdClient()) conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient()) if conf == nil { fmt.Printf("get conf failed\n\n") os.Exit(1) } rpc_apis.Init(etcdAddrs, conf) } appname := conf.Rpc.Vehicle.Name // 指定mysql数据库,若无则使用默认数据库 mysqldb := conf.Rpc.Vehicle.MysqlDB if mysqldb == "" { mysqldb = conf.Mysql.DefaultDB } mysqldb = "" // 指定redis数据库,若无则使用默认数据库 redisdb := conf.Rpc.Vehicle.RedisDB if redisdb == "" { redisdb = conf.Redis.DefaultDB } // 连接数据库服务器 clinit.InitMySQL(&clinit.MysqlConfig{ User: conf.Mysql.User, Password: conf.Mysql.Password, Addr: conf.Mysql.Addr, DB: mysqldb, Charset: conf.Mysql.Charset, MaxIdle: conf.Mysql.MaxIdle, MaxConn: conf.Mysql.MaxConn, }) // 初始化mongodb数据库 //clinit.InitMongo(conf.Mongo.Addr, conf.Mongo.User, conf.Mongo.Password) // 连接redis服务器 cache.InitRedis(&cache.RedisConfig{ Addrs: strings.Split(conf.Redis.Addrs, ","), Password: conf.Redis.Password, DB: redisdb, PoolSize: conf.Redis.PoolSize, MinIdleConns: conf.Redis.MinIdleConns, MaxRetries: conf.Redis.MaxRetries, IsCluster: conf.Redis.IsCluster, }) //rpc_apis.Init(etcdAddrs, conf) // clinit.InitUniqueID() // clinit.InitElastic(conf.Elastic.Addr, conf.Elastic.Sniff == "true") // 初始化logger ms, _ := conf.Rpc.Vehicle.Log.MaxSize.Int64() mb, _ := conf.Rpc.Vehicle.Log.MaxBackups.Int64() ma, _ := conf.Rpc.Vehicle.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.Vehicle.Log.DisableStacktrace == "true") // 通用logger commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Rpc.Vehicle.Log.Level, maxSize, maxBackups, maxAge, disableStacktrace) // 单独设置 accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), appname, conf.Rpc.Vehicle.Log.Level, maxSize, maxBackups, maxAge) thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname), appname, conf.Rpc.Vehicle.Log.Level, maxSize, maxBackups, maxAge) // 设置需要使用logger的地方 // common日志 impl.SetLogger(commonLogger) dutils.SetLogger(commonLogger) // access日志 task.SetLogger(accessLogger) // 第三方日志 thirdparty.SetLogger(thirdpartyLogger) impl.RegisterOrmModel() dutils.ProviderApiHystrixLoad() // 打开第三方包调试开关 if runmode != "prod" { orm.Debug = true //redis.Debug = true } } func start(conf *config.Configure, serveAddr string, etcdAddrs []string) { s := server.NewServer() r := &serverplugin.EtcdRegisterPlugin{ ServiceAddress: fmt.Sprintf("tcp@%s", serveAddr), EtcdServers: etcdAddrs, BasePath: conf.Rpc.BasePath, Metrics: metrics.NewRegistry(), UpdateInterval: time.Minute, } if err := r.Start(); err != nil { fmt.Printf("start failed. error:%s\n\n", err) os.Exit(1) } s.Plugins.Add(r) s.RegisterName(conf.Rpc.Vehicle.Name, new(impl.Rcvr), "") go func() { if err := s.Serve("tcp", serveAddr); err != nil { log.Fatalf("HTTP server listen failed. err: %s\n", err.Error()) } }() // 捕获信号 sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) sigValue := <-sigChan log.Printf("Got a signal:%v", sigValue) r.Stop() time.Sleep(5 * time.Second) log.Println("Shutdown server finished.") } func startPeerToPeer(conf *config.Configure, serveAddr string) { s := server.NewServer() s.RegisterName(conf.Rpc.Vehicle.Name, new(impl.Rcvr), "") s.Serve("tcp", serveAddr) } func main() { flag.Parse() if *version { showVersion() } cfg, err := ini.Load(*configFile) if err != nil { fmt.Printf("Fail to read file: %v\n\n", err) os.Exit(1) } //appname := cfg.Section("").Key("appname").String() runmode := cfg.Section("").Key("runmode").String() logDir := cfg.Section("").Key("log_dir").String() //serveAddr := cfg.Section("").Key("serve_addr").String() etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",") encryptKey := cfg.Section("").Key("encrypt_key").String() discoveryType := cfg.Section("").Key("discovery_type").String() projectName := cfg.Section("").Key("project_name").String() utils.SetRunmode(runmode) prepare(projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType) go utils.Free() serveAddr := fmt.Sprintf("%s:%s", gconfig.Conf.Rpc.Vehicle.ServiceName, gconfig.Conf.Rpc.Vehicle.ServicePort.String()) if discoveryType == "etcd" { start(gconfig.Conf, serveAddr, etcdAddrs) } else { startPeerToPeer(gconfig.Conf, serveAddr) } return }