package main import ( "gd_access_log/impl" "gd_access_log/impl/access_log" "gd_access_log/impl/h5_access_log" "gd_access_log/rpc_apis" "gd_access_log/rpc_apis/gd_management" "flag" "fmt" "log" "os" "os/signal" "strings" "syscall" "time" "gd_access_log/common.in/mq" "gd_access_log/common.in/cache" "gd_access_log/common.in/clinit" "gd_access_log/common.in/config" "gd_access_log/common.in/logger" "gd_access_log/common.in/utils" "github.com/astaxie/beego/orm" _ "github.com/go-sql-driver/mysql" //"github.com/gomodule/redigo/redis" "github.com/smallnest/rpcx/server" "github.com/smallnest/rpcx/serverplugin" gconfig "gd_access_log/common.in/config" "github.com/rcrowley/go-metrics" "gopkg.in/ini.v1" ) var ( // 这里可以改默认值 configFile = flag.String("config", "/etc/gd_access_log/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) 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.AccessLog.Name fmt.Println("init rabbitmq") // 初始化rabbitmq odsMq := mq.InitRabbitmq( conf.OdsRabbitmq.Addr, conf.OdsRabbitmq.Username, conf.OdsRabbitmq.Passwrod, conf.OdsRabbitmq.Vhost, conf.OdsRabbitmq.ExchangeName, conf.OdsRabbitmq.QueueName, conf.OdsRabbitmq.RouteBindKey, nil, true, 0, ) mq.SetOdsMq(odsMq) // 指定mysql数据库,若无则使用默认数据库 mysqldb := conf.Rpc.AccessLog.MysqlDB if mysqldb == "" { mysqldb = conf.LogMysql.DefaultDB } // 指定redis数据库,若无则使用默认数据库 redisdb := conf.Rpc.AccessLog.RedisDB if redisdb == "" { redisdb = conf.Redis.DefaultDB } // 连接数据库服务器 clinit.InitMySQL(&clinit.MysqlConfig{ User: conf.LogMysql.User, Password: conf.LogMysql.Password, Addr: conf.LogMysql.Addr, DB: mysqldb, Charset: conf.LogMysql.Charset, MaxIdle: conf.LogMysql.MaxIdle, MaxConn: conf.LogMysql.MaxConn, }) // 连接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, }) // 初始化logger /*ms, _ := conf.Rpc.AccessLog.Log.MaxSize.Int64() mb, _ := conf.Rpc.AccessLog.Log.MaxBackups.Int64() ma, _ := conf.Rpc.AccessLog.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.AccessLog.Log.DisableStacktrace == "true") commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), maxSize, maxBackups, maxAge, disableStacktrace) accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname), maxSize, maxBackups, maxAge) thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname), maxSize, maxBackups, maxAge) */ // 设置需要使用logger的地方 /*if err := mongo.MgoInit(conf.Mongo.Addr, conf.Mongo.User, conf.Mongo.Password); err != nil { fmt.Printf("init mongo failed:%v\n", err) os.Exit(-1) }*/ ms, _ := conf.Rpc.AccessLog.Log.MaxSize.Int64() mb, _ := conf.Rpc.AccessLog.Log.MaxBackups.Int64() ma, _ := conf.Rpc.AccessLog.Log.MaxAge.Int64() maxSize := int(ms) maxBackups := int(mb) maxAge := int(ma) disableStacktrace := (conf.Rpc.AccessLog.Log.DisableStacktrace == "true") // 通用logger commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname), appname, conf.Rpc.AccessLog.Log.Level, maxSize, maxBackups, maxAge, disableStacktrace) access_log.SetLogger(commonLogger) h5_access_log.SetLogger(commonLogger) impl.RegisterOrmModel() // common日志 if runmode != "prod" { orm.Debug = true //redis.Debug = false } } func start(conf *config.Configure, serveAddr string, etcdAddrs []string) { fmt.Printf("xxxx:%v\n", etcdAddrs) 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.AccessLog.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.AccessLog.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() logDir := cfg.Section("").Key("log_dir").String() runmode := cfg.Section("").Key("runmode").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() prepare( projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType) go utils.Free() go impl.CleanLocalCache() //utils.HandleExitSignal() gd_management.Watch() serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Rpc.AccessLog.ServiceName,gconfig.Conf.Rpc.AccessLog.ServicePort.String()) if discoveryType == "etcd" { fmt.Println(gconfig.Conf, serveAddr, etcdAddrs) start(gconfig.Conf, serveAddr, etcdAddrs) } else { startPeerToPeer(gconfig.Conf, serveAddr) } return }