main.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "adm-ods/common.in/cache"
  6. "adm-ods/common.in/mq"
  7. "adm-ods/impl"
  8. "adm-ods/impl/analysis"
  9. "adm-ods/impl/handle"
  10. "adm-ods/model"
  11. "adm-ods/pb"
  12. dutils "adm-ods/utils"
  13. "context"
  14. "flag"
  15. "fmt"
  16. "log"
  17. "os"
  18. "os/signal"
  19. "strings"
  20. "syscall"
  21. "time"
  22. //"adm-ods/common.in/cache"
  23. "adm-ods/common.in/clinit"
  24. "adm-ods/common.in/config"
  25. "adm-ods/common.in/logger"
  26. "adm-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/adm-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. // 连接redis服务器
  92. fmt.Println("redis init start")
  93. cache.InitRedis(&cache.RedisConfig{
  94. Addrs: strings.Split(conf.Redis.Addrs, ","),
  95. Password: conf.Redis.Password,
  96. DB: redisdb,
  97. PoolSize: conf.Redis.PoolSize,
  98. MinIdleConns: conf.Redis.MinIdleConns,
  99. MaxRetries: conf.Redis.MaxRetries,
  100. IsCluster: conf.Redis.IsCluster,
  101. })
  102. fmt.Println("redis init finish")
  103. // 建立rpc客户端
  104. conns := pb.SetupClients()
  105. for _, conn := range conns {
  106. defer conn.Close()
  107. }
  108. fmt.Println("setup client finish")
  109. // 初始化logger
  110. ms, _ := conf.Log.MaxSize.Int64()
  111. mb, _ := conf.Log.MaxBackups.Int64()
  112. ma, _ := conf.Log.MaxAge.Int64()
  113. maxSize := int(ms)
  114. maxBackups := int(mb)
  115. maxAge := int(ma)
  116. disableStacktrace := (conf.Log.Stacktrace == "true")
  117. // 通用logger
  118. commonLogger := logger.InitLogger(conf.RunMode, fmt.Sprintf("%s/%s.log", conf.Log.Path, conf.Rpc.AdmOds.ServiceName), conf.Rpc.AdmOds.ServiceName, conf.Log.Level,
  119. maxSize, maxBackups, maxAge, disableStacktrace)
  120. // 单独设置
  121. 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,
  122. maxSize, maxBackups, maxAge)
  123. // 设置需要使用logger的地方
  124. dutils.SetLogger(commonLogger)
  125. analysis.SetLogger(commonLogger)
  126. handle.SetLogger(commonLogger)
  127. handle.SetAccessLogger(accessLogger)
  128. model.SetLogger(commonLogger)
  129. // access日志
  130. fmt.Println("init rabbitmq")
  131. // 初始化rabbitmq
  132. consumerCount, _ := conf.OdsRabbitmq.ConsumerCount.Int64()
  133. fmt.Println("consumerCount:", consumerCount)
  134. if consumerCount == 0 {
  135. consumerCount = 2
  136. }
  137. odsMq := mq.InitRabbitmq(
  138. conf.OdsRabbitmq.Addr,
  139. conf.OdsRabbitmq.Username,
  140. conf.OdsRabbitmq.Passwrod,
  141. conf.OdsRabbitmq.Vhost,
  142. conf.OdsRabbitmq.ExchangeName,
  143. conf.OdsRabbitmq.QueueName,
  144. conf.OdsRabbitmq.RouteBindKey,
  145. impl.HandleOdsData,
  146. false,
  147. int(consumerCount),
  148. )
  149. mq.SetOdsMq(odsMq)
  150. dwsMq := mq.InitRabbitmq(
  151. conf.DwsRabbitmq.Addr,
  152. conf.DwsRabbitmq.Username,
  153. conf.DwsRabbitmq.Passwrod,
  154. conf.DwsRabbitmq.Vhost,
  155. conf.DwsRabbitmq.ExchangeName,
  156. conf.DwsRabbitmq.QueueName,
  157. conf.DwsRabbitmq.RouteBindKey,
  158. nil,
  159. true,
  160. 0,
  161. )
  162. mq.SetDwsMq(dwsMq)
  163. odsMq.StartConsumer()
  164. }
  165. func start() {
  166. // 优雅关闭服务器
  167. sigChan := make(chan os.Signal, 1)
  168. // 捕获信号
  169. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  170. sigValue := <-sigChan
  171. log.Printf("Got a signal:%v", sigValue)
  172. // 不管什么行为,都等待5秒退出
  173. log.Println("Start to shutdown server...")
  174. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  175. defer cancel()
  176. log.Println("Shutdown server finished.")
  177. }
  178. func main() {
  179. flag.Parse()
  180. if *version {
  181. showVersion()
  182. }
  183. cfg, err := ini.Load(*appConfigFile)
  184. if err != nil {
  185. fmt.Printf("Fail to read file: %v\n\n", err)
  186. os.Exit(1)
  187. }
  188. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  189. discoveryType := cfg.Section("").Key("discovery_type").String()
  190. //utils.SetRunmode(runmode)
  191. prepare(*configFile, etcdAddrs, discoveryType)
  192. go utils.Free()
  193. start()
  194. return
  195. }