main.go 5.4 KB

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