// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package main import ( "flag" "fmt" "gd_adm_data/impl" "gd_adm_data/impl/v1/query" "log" "os" "os/signal" "strings" "syscall" "time" "git.getensh.com/common/gopkgsv2/database" "gd_adm_data/common.in/cache" "gd_adm_data/common.in/clinit" "gd_adm_data/common.in/config" "gd_adm_data/common.in/logger" "gd_adm_data/common.in/task" "gd_adm_data/common.in/utils" _ "github.com/go-sql-driver/mysql" "github.com/smallnest/rpcx/server" "github.com/smallnest/rpcx/serverplugin" gconfig "gd_adm_data/common.in/config" "github.com/rcrowley/go-metrics" "gopkg.in/ini.v1" ) var ( // 这里可以改默认值 configFile = flag.String("config", "/etc/gd_adm_data/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) } } 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) } } appname := conf.Rpc.AdmData.Name // 指定mysql数据库,若无则使用默认数据库 mysqldb := conf.Rpc.AdmData.MysqlDB if mysqldb == "" { mysqldb = conf.AdmMysql.DefaultDB } // 指定redis数据库,若无则使用默认数据库 redisdb := conf.Rpc.AdmData.RedisDB if redisdb == "" { redisdb = conf.Redis.DefaultDB } // 连接数据库服务器 maxIdle, _ := conf.AdmMysql.MaxIdle.Int64() maxConn, _ := conf.AdmMysql.MaxConn.Int64() database.Setup(conf.AdmMysql.User, conf.AdmMysql.Password, conf.AdmMysql.Addr, mysqldb, conf.AdmMysql.Charset, int(maxIdle), int(maxConn), true) /*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.AdmData.Log.MaxSize.Int64() mb, _ := conf.Rpc.AdmData.Log.MaxBackups.Int64() ma, _ := conf.Rpc.AdmData.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.AdmData.Log.DisableStacktrace == "true") // 通用logger commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Rpc.AdmData.Log.Level, maxSize, maxBackups, maxAge, disableStacktrace) // 单独设置 accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), appname, conf.Rpc.AdmData.Log.Level, maxSize, maxBackups, maxAge) // 设置需要使用logger的地方 // common日志 // access日志 query.SetLogger(commonLogger) task.SetLogger(accessLogger) // 第三方日志 } 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.AdmData.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.AdmData.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.AdmData.ServiceName, gconfig.Conf.Rpc.AdmData.ServicePort.String()) if discoveryType == "etcd" { start(gconfig.Conf, serveAddr, etcdAddrs) } else { startPeerToPeer(gconfig.Conf, serveAddr) } return }