123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package main
- import (
- "gd_management/impl/accounting"
- "gd_management/impl/order"
- "gd_management/impl/provider"
- "gd_management/impl/try_api"
- "gd_management/rpc_apis"
- "gd_management/thirdparty"
- "flag"
- "fmt"
- "log"
- "os"
- "os/signal"
- "strings"
- "syscall"
- "time"
- "gd_management/impl"
- "gd_management/impl/base_api"
- "gd_management/impl/error_code"
- "gd_management/impl/h5"
- "gd_management/impl/user_merchant"
- "gd_management/common.in/cache"
- "gd_management/common.in/clinit"
- "gd_management/common.in/config"
- "gd_management/common.in/logger"
- "gd_management/common.in/task"
- "gd_management/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_management/common.in/config"
- "github.com/rcrowley/go-metrics"
- "gopkg.in/ini.v1"
- )
- var (
- // 这里可以改默认值
- configFile = flag.String("config", "/etc/gd_management/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 {
- // 先行于读配置
- fmt.Printf("xxx:%v\n", etcdAddrs)
- 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.Management.Name
- // 指定mysql数据库,若无则使用默认数据库
- mysqldb := conf.Rpc.Management.MysqlDB
- if mysqldb == "" {
- mysqldb = conf.Mysql.DefaultDB
- }
- // 指定redis数据库,若无则使用默认数据库
- redisdb := conf.Rpc.Management.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,
- })
- // 连接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.Service.Log.MaxSize.Int64()
- mb, _ := conf.Rpc.Service.Log.MaxBackups.Int64()
- ma, _ := conf.Rpc.Service.Log.MaxAge.Int64()
- maxSize := int(ms)
- maxBackups := int(mb)
- maxAge := int(ma)
- disableStacktrace := (conf.Rpc.Service.Log.DisableStacktrace == "true")
- //通用logger
- 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的地方
- // common日志
- base_api.SetLogger(commonLogger)
- h5.SetLogger(commonLogger)
- order.SetLogger(commonLogger)
- error_code.SetLogger(commonLogger)
- user_merchant.SetLogger(commonLogger)
- accounting.SetLogger(commonLogger)
- provider.SetLogger(commonLogger)
- try_api.SetLogger(commonLogger)
- task.SetLogger(accessLogger)
- // 第三方日志
- thirdparty.SetLogger(thirdpartyLogger)
- if runmode != "prod" {
- orm.Debug = true
- //redis.Debug = true
- }
- impl.RegisterModel()
- impl.LoadRedis()
- impl.InitProviderRelation()
- go impl.CleanLocalCache()
- }
- func start(conf *config.Configure,serveAddr string, etcdAddrs []string) {
- s := server.NewServer()
- fmt.Printf("base path:%v, etcdaddrs:%v\n", conf.Rpc.BasePath,serveAddr)
- 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.Management.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.Management.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()
- //serveAddr := cfg.Section("").Key("serve_addr").String()
- logDir := cfg.Section("").Key("log_dir").String()
- runmode := cfg.Section("").Key("runmode").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)
- fmt.Printf("%v\n%v\n%v\n",
- config.Conf.GdApiAppKey,
- config.Conf.GdApiAppSecret,
- config.Conf.GdApiHost)
- go utils.Free()
- serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Rpc.Management.ServiceName,gconfig.Conf.Rpc.Management.ServicePort.String())
- if discoveryType == "etcd" {
- start(gconfig.Conf, serveAddr, etcdAddrs)
- } else {
- startPeerToPeer(gconfig.Conf, serveAddr)
- }
- return
- }
|