main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package main
  2. import (
  3. "gd_admin/impl"
  4. "gd_admin/impl/action"
  5. "gd_admin/impl/admin"
  6. "gd_admin/impl/auth"
  7. "gd_admin/impl/rbac"
  8. "gd_admin/thirdparty/oss"
  9. "flag"
  10. "fmt"
  11. "os"
  12. "strings"
  13. "time"
  14. "gd_admin/common.in/clinit"
  15. "gd_admin/common.in/config"
  16. "gd_admin/common.in/logger"
  17. "gd_admin/common.in/task"
  18. "github.com/astaxie/beego/orm"
  19. _ "github.com/go-sql-driver/mysql"
  20. //"github.com/gomodule/redigo/redis"
  21. "github.com/smallnest/rpcx/server"
  22. "github.com/smallnest/rpcx/serverplugin"
  23. gconfig "gd_admin/common.in/config"
  24. "github.com/rcrowley/go-metrics"
  25. "gopkg.in/ini.v1"
  26. )
  27. var (
  28. // 这里可以改默认值
  29. configFile = flag.String("config", "/etc/gd_admin/app.conf", "config file location")
  30. GitCommit = "library-import"
  31. Version = "library-import"
  32. )
  33. func prepare(projectName, runmode, key, logDir string, etcdAddrs []string, discoveryType string) {
  34. var conf *config.Configure
  35. if discoveryType == "k8s" {
  36. conf = config.GetConfigForK8s()
  37. if conf == nil {
  38. fmt.Printf("get conf failed\n\n")
  39. os.Exit(1)
  40. }
  41. } else {
  42. // 先行于读配置
  43. clinit.InitEtcd(etcdAddrs)
  44. fmt.Println("init etcd:",etcdAddrs)
  45. conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
  46. if conf == nil {
  47. fmt.Printf("get conf failed\n\n")
  48. os.Exit(1)
  49. }
  50. fmt.Println("conf:",*conf)
  51. }
  52. appname := conf.Rpc.Admin.Name
  53. // 指定mysql数据库,若无则使用默认数据库
  54. mysqldb := conf.Rpc.Admin.MysqlDB
  55. if mysqldb == "" {
  56. mysqldb = conf.Mysql.DefaultDB
  57. }
  58. // 指定redis数据库,若无则使用默认数据库
  59. /*redisdb := conf.Rpc.Admin.RedisDB
  60. if redisdb == "" {
  61. redisdb = conf.Redis.DefaultDB
  62. }*/
  63. // 连接数据库服务器
  64. clinit.InitMySQL(&clinit.MysqlConfig{
  65. User: conf.Mysql.User,
  66. Password: conf.Mysql.Password,
  67. Addr: conf.Mysql.Addr,
  68. DB: mysqldb,
  69. Charset: conf.Mysql.Charset,
  70. MaxIdle: conf.Mysql.MaxIdle,
  71. MaxConn: conf.Mysql.MaxConn,
  72. })
  73. // 连接redis服务器
  74. /*cache.InitRedis(&cache.RedisConfig{
  75. Network: conf.Redis.Network,
  76. Address: conf.Redis.Address,
  77. Password: conf.Redis.Password,
  78. DB: redisdb,
  79. ConnectTimeout: conf.Redis.ConnectTimeout,
  80. ReadTimeout: conf.Redis.ReadTimeout,
  81. WriteTimeout: conf.Redis.WriteTimeout,
  82. IdleTimeout: conf.Redis.IdleTimeout,
  83. MaxActive: conf.Redis.MaxActive,
  84. MaxIdle: conf.Redis.MaxIdle,
  85. Wait: conf.Redis.Wait,
  86. })
  87. */
  88. // 初始化logger
  89. ms, _ := conf.Rpc.Admin.Log.MaxSize.Int64()
  90. mb, _ := conf.Rpc.Admin.Log.MaxBackups.Int64()
  91. ma, _ := conf.Rpc.Admin.Log.MaxAge.Int64()
  92. maxSize := int(ms)
  93. maxBackups := int(mb)
  94. maxAge := int(ma)
  95. disableStacktrace := (conf.Rpc.Admin.Log.DisableStacktrace == "true")
  96. // 通用logger
  97. commonLogger := logger.InitLogger(runmode, fmt.Sprintf("%s/%s.log", logDir, appname),
  98. maxSize, maxBackups, maxAge, disableStacktrace)
  99. // 单独设置
  100. accessLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-access.log", logDir, appname),
  101. maxSize, maxBackups, maxAge)
  102. thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname),
  103. maxSize, maxBackups, maxAge)
  104. // 设置三方日志
  105. oss.SetLogger(thirdpartyLogger)
  106. //thirdpartyLogger := logger.NewInfoLogger(runmode, fmt.Sprintf("%s/%s-thirdparty.log", logDir, appname),
  107. // maxSize, maxBackups, maxAge)
  108. // access日志
  109. task.SetLogger(accessLogger)
  110. admin.SetLogger(commonLogger)
  111. rbac.SetLogger(commonLogger)
  112. auth.SetLogger(commonLogger)
  113. action.SetLogger(commonLogger)
  114. // 设置需要使用logger的地方
  115. // common日志
  116. impl.RegisterOrmModel()
  117. if runmode != "prod" {
  118. orm.Debug = true
  119. //redis.Debug = true
  120. }
  121. }
  122. func start(conf *config.Configure, serveAddr string, etcdAddrs []string) {
  123. s := server.NewServer()
  124. fmt.Println("conf:",*conf,serveAddr ,etcdAddrs)
  125. r := &serverplugin.EtcdRegisterPlugin{
  126. ServiceAddress: fmt.Sprintf("tcp@%s", serveAddr),
  127. EtcdServers: etcdAddrs,
  128. BasePath: conf.Rpc.BasePath,
  129. Metrics: metrics.NewRegistry(),
  130. UpdateInterval: time.Minute,
  131. }
  132. fmt.Println("111111111111",etcdAddrs)
  133. if err := r.Start(); err != nil {
  134. fmt.Printf("start failed. error:%s\n\n", err)
  135. os.Exit(1)
  136. }
  137. fmt.Println("22222222222222222222")
  138. s.Plugins.Add(r)
  139. fmt.Println("22222222222222222222")
  140. s.RegisterName(conf.Rpc.Admin.Name, new(impl.Rcvr), "")
  141. fmt.Println("22222222222222222222")
  142. s.Serve("tcp", serveAddr)
  143. }
  144. func startPeerToPeer(conf *config.Configure, serveAddr string) {
  145. s := server.NewServer()
  146. s.RegisterName(conf.Rpc.Admin.Name, new(impl.Rcvr), "")
  147. s.Serve("tcp", serveAddr)
  148. }
  149. func main() {
  150. flag.Parse()
  151. cfg, err := ini.Load(*configFile)
  152. if err != nil {
  153. fmt.Printf("Fail to read file: %v\n\n", err)
  154. os.Exit(1)
  155. }
  156. //appname := cfg.Section("").Key("appname").String()
  157. logDir := cfg.Section("").Key("log_dir").String()
  158. runmode := cfg.Section("").Key("runmode").String()
  159. //serveAddr := cfg.Section("").Key("serve_addr").String()
  160. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  161. encryptKey := cfg.Section("").Key("encrypt_key").String()
  162. discoveryType := cfg.Section("").Key("discovery_type").String()
  163. projectName := cfg.Section("").Key("project_name").String()
  164. fmt.Println("etcd addr",etcdAddrs)
  165. prepare(projectName, runmode, encryptKey, logDir, etcdAddrs, discoveryType)
  166. serveAddr := fmt.Sprintf("%s:%s",gconfig.Conf.Rpc.Admin.ServiceName,gconfig.Conf.Rpc.Admin.ServicePort.String())
  167. if discoveryType == "etcd" {
  168. fmt.Println("start etcd")
  169. start(gconfig.Conf, serveAddr, etcdAddrs)
  170. } else {
  171. startPeerToPeer(gconfig.Conf, serveAddr)
  172. }
  173. return
  174. }