main.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2019 getensh.cc. All rights reserved.
  2. // Use of this source code is governed by getensh.cc.
  3. package main
  4. import (
  5. "context"
  6. "adm-ads/common.in/cache"
  7. "adm-ads/common.in/mq"
  8. "adm-ads/impl"
  9. "adm-ads/impl/handle"
  10. "adm-ads/impl/task"
  11. "adm-ads/model"
  12. "adm-ads/pb"
  13. dutils "adm-ads/utils"
  14. "flag"
  15. "fmt"
  16. "log"
  17. "os"
  18. "os/signal"
  19. "strings"
  20. "syscall"
  21. "time"
  22. //"adm-ads/common.in/cache"
  23. "adm-ads/common.in/clinit"
  24. "adm-ads/common.in/config"
  25. "adm-ads/common.in/logger"
  26. "adm-ads/common.in/utils"
  27. _ "github.com/go-sql-driver/mysql"
  28. "gopkg.in/ini.v1"
  29. )
  30. var (
  31. // 这里可以改默认值
  32. appConfigFile = flag.String("appconfig", "/etc/adm-ads/app.conf", "app config file location")
  33. configFile = flag.String("config", "/etc/adm/common.json", "config file location")
  34. version = flag.Bool("version", false, "config file location")
  35. GitCommit = "library-import"
  36. Version = "library-import"
  37. )
  38. func showVersion() {
  39. fmt.Println("Version: ", Version)
  40. fmt.Println("GitCommit:", GitCommit)
  41. }
  42. func prepare(filename string,etcdAddrs []string, discoveryType string) {
  43. var conf *config.Configure
  44. if discoveryType == "k8s" {
  45. config.SetConfigFile(filename)
  46. conf = config.GetConfigForK8s()
  47. if conf == nil {
  48. fmt.Printf("get conf failed\n\n")
  49. os.Exit(1)
  50. }
  51. } else {
  52. config.SetConfigFile(filename)
  53. conf = config.GetConfigForK8s()
  54. if conf == nil {
  55. fmt.Printf("get conf failed\n\n")
  56. os.Exit(1)
  57. }
  58. clinit.EctdHandler(etcdAddrs)
  59. /*// 先行于读配置
  60. clinit.InitEtcd(etcdAddrs)
  61. conf = config.GetConfig(projectName+"/"+runmode, key, clinit.GetEtcdClient())
  62. if conf == nil {
  63. fmt.Printf("get conf failed\n\n")
  64. os.Exit(1)
  65. }*/
  66. //rpc_apis.Init(etcdAddrs, conf)
  67. }
  68. // 指定mysql数据库,若无则使用默认数据库
  69. mysqldb := conf.Rpc.AdmAds.MysqlDb
  70. if mysqldb == "" {
  71. mysqldb = conf.Mysql.Db
  72. }
  73. // 连接数据库服务器
  74. clinit.InitMysqlGorm(
  75. conf.Mysql.User,
  76. conf.Mysql.Password,
  77. conf.Mysql.Addr,
  78. mysqldb,
  79. conf.Mysql.Charset,
  80. conf.Mysql.MaxIdle,
  81. conf.Mysql.MaxConn,
  82. conf.RunMode != "prod",
  83. )
  84. // 指定redis数据库,若无则使用默认数据库
  85. redisdb := conf.Rpc.AdmDws.RedisDb
  86. if redisdb == "" {
  87. redisdb = conf.Redis.Db
  88. }
  89. // 连接redis服务器
  90. cache.InitRedis(&cache.RedisConfig{
  91. Addrs: strings.Split(conf.Redis.Addrs, ","),
  92. Password: conf.Redis.Password,
  93. DB: redisdb,
  94. PoolSize: conf.Redis.PoolSize,
  95. MinIdleConns: conf.Redis.MinIdleConns,
  96. MaxRetries: conf.Redis.MaxRetries,
  97. IsCluster: conf.Redis.IsCluster,
  98. })
  99. // 建立rpc客户端
  100. conns := pb.SetupClients()
  101. for _, conn := range conns {
  102. defer conn.Close()
  103. }
  104. // 初始化logger
  105. ms, _ := conf.Log.MaxSize.Int64()
  106. mb, _ := conf.Log.MaxBackups.Int64()
  107. ma, _ := conf.Log.MaxAge.Int64()
  108. maxSize := int(ms)
  109. maxBackups := int(mb)
  110. maxAge := int(ma)
  111. disableStacktrace := (conf.Log.Stacktrace == "true")
  112. // 通用logger
  113. fmt.Println(fmt.Sprintf("%s/%s.log", conf.Log.Path, conf.Rpc.AdmAds.ServiceName))
  114. commonLogger := logger.InitLogger(conf.RunMode, fmt.Sprintf("%s/%s.log", conf.Log.Path, conf.Rpc.AdmAds.ServiceName), conf.Rpc.AdmAds.ServiceName, conf.Log.Level,
  115. maxSize, maxBackups, maxAge, disableStacktrace)
  116. // 单独设置
  117. accessLogger := logger.NewInfoLogger(conf.RunMode, fmt.Sprintf("%s/%s-access.log", conf.Log.Path, conf.Rpc.AdmAds.ServiceName), conf.Rpc.AdmAds.ServiceName, conf.Log.Level,
  118. maxSize, maxBackups, maxAge)
  119. // 设置需要使用logger的地方
  120. dutils.SetLogger(commonLogger)
  121. model.SetLogger(commonLogger)
  122. task.SetLogger(commonLogger)
  123. handle.SetLogger(commonLogger)
  124. handle.SetAccessLogger(accessLogger)
  125. fmt.Println("init rabbitmq")
  126. // 初始化rabbitmq
  127. consumerCount, _ := conf.AdsRabbitmq.ConsumerCount.Int64()
  128. fmt.Println("consumerCount:", consumerCount)
  129. if consumerCount == 0 {
  130. consumerCount = 2
  131. }
  132. adsMq := mq.InitRabbitmq(
  133. conf.AdsRabbitmq.Addr,
  134. conf.AdsRabbitmq.Username,
  135. conf.AdsRabbitmq.Passwrod,
  136. conf.AdsRabbitmq.Vhost,
  137. conf.AdsRabbitmq.ExchangeName,
  138. conf.AdsRabbitmq.QueueName,
  139. conf.AdsRabbitmq.RouteBindKey,
  140. impl.HandleAdsData,
  141. false,
  142. int(consumerCount),
  143. )
  144. adsMq.StartConsumer()
  145. }
  146. func start() {
  147. // 优雅关闭服务器
  148. sigChan := make(chan os.Signal, 1)
  149. // 捕获信号
  150. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  151. sigValue := <-sigChan
  152. log.Printf("Got a signal:%v", sigValue)
  153. // 不管什么行为,都等待5秒退出
  154. log.Println("Start to shutdown server...")
  155. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  156. defer cancel()
  157. log.Println("Shutdown server finished.")
  158. }
  159. func main() {
  160. flag.Parse()
  161. if *version {
  162. showVersion()
  163. }
  164. cfg, err := ini.Load(*appConfigFile)
  165. if err != nil {
  166. fmt.Printf("Fail to read file: %v\n\n", err)
  167. os.Exit(1)
  168. }
  169. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  170. discoveryType := cfg.Section("").Key("discovery_type").String()
  171. //utils.SetRunmode(runmode)
  172. prepare(*configFile,etcdAddrs, discoveryType)
  173. go utils.Free()
  174. start()
  175. return
  176. }