main.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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-dws/common.in/cache"
  7. "adm-dws/common.in/mq"
  8. "adm-dws/impl"
  9. "adm-dws/impl/handle"
  10. "adm-dws/impl/task"
  11. "adm-dws/model"
  12. "adm-dws/pb"
  13. dutils "adm-dws/utils"
  14. "flag"
  15. "fmt"
  16. "log"
  17. "os"
  18. "os/signal"
  19. "strings"
  20. "syscall"
  21. "time"
  22. //"adm-dws/common.in/cache"
  23. "adm-dws/common.in/clinit"
  24. "adm-dws/common.in/config"
  25. "adm-dws/common.in/logger"
  26. "adm-dws/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-dws/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. */
  67. //rpc_apis.Init(etcdAddrs, conf)
  68. }
  69. // 指定mysql数据库,若无则使用默认数据库
  70. mysqldb := conf.Rpc.AdmDws.MysqlDb
  71. if mysqldb == "" {
  72. mysqldb = conf.Mysql.Db
  73. }
  74. fmt.Println("init mysql start")
  75. // 连接数据库服务器
  76. clinit.InitMysqlGorm(
  77. conf.Mysql.User,
  78. conf.Mysql.Password,
  79. conf.Mysql.Addr,
  80. mysqldb,
  81. conf.Mysql.Charset,
  82. conf.Mysql.MaxIdle,
  83. conf.Mysql.MaxConn,
  84. conf.RunMode != "prod",
  85. )
  86. fmt.Println("init mysql end")
  87. // 指定redis数据库,若无则使用默认数据库
  88. redisdb := conf.Rpc.AdmDws.RedisDb
  89. if redisdb == "" {
  90. redisdb = conf.Redis.Db
  91. }
  92. fmt.Println("init redis start")
  93. // 连接redis服务器
  94. cache.InitRedis(&cache.RedisConfig{
  95. Addrs: strings.Split(conf.Redis.Addrs, ","),
  96. Password: conf.Redis.Password,
  97. DB: redisdb,
  98. PoolSize: conf.Redis.PoolSize,
  99. MinIdleConns: conf.Redis.MinIdleConns,
  100. MaxRetries: conf.Redis.MaxRetries,
  101. IsCluster: conf.Redis.IsCluster,
  102. })
  103. fmt.Println("init redis end")
  104. fmt.Println("init client start")
  105. // 建立rpc客户端
  106. conns := pb.SetupClients()
  107. for _, conn := range conns {
  108. defer conn.Close()
  109. }
  110. fmt.Println("init client end")
  111. ms, _ := conf.Log.MaxSize.Int64()
  112. mb, _ := conf.Log.MaxBackups.Int64()
  113. ma, _ := conf.Log.MaxAge.Int64()
  114. maxSize := int(ms)
  115. maxBackups := int(mb)
  116. maxAge := int(ma)
  117. disableStacktrace := (conf.Log.Stacktrace == "true")
  118. // 通用logger
  119. commonLogger := logger.InitLogger(conf.RunMode, fmt.Sprintf("%s/%s.log", conf.Log.Path, conf.Rpc.AdmDws.ServiceName), conf.Rpc.AdmDws.ServiceName, conf.Log.Level,
  120. maxSize, maxBackups, maxAge, disableStacktrace)
  121. // 单独设置
  122. accessLogger := logger.NewInfoLogger(conf.RunMode, fmt.Sprintf("%s/%s-access.log", conf.Log.Path, conf.Rpc.AdmDws.ServiceName), conf.Rpc.AdmDws.ServiceName, conf.Log.Level,
  123. maxSize, maxBackups, maxAge)
  124. // 设置需要使用logger的地方
  125. dutils.SetLogger(commonLogger)
  126. model.SetLogger(commonLogger)
  127. task.SetLogger(commonLogger)
  128. handle.SetLogger(commonLogger)
  129. handle.SetAccessLogger(accessLogger)
  130. fmt.Println("loading map start")
  131. task.Load()
  132. fmt.Println("loading map end")
  133. fmt.Println("init rabbitmq start")
  134. // 初始化rabbitmq
  135. consumerCount, _ := conf.DwsRabbitmq.ConsumerCount.Int64()
  136. fmt.Println("consumerCount:", consumerCount)
  137. if consumerCount == 0 {
  138. consumerCount = 2
  139. }
  140. fmt.Printf("rmqaddr:%v,%v,%v\n", conf.DwsRabbitmq.Addr, conf.AdsRabbitmq.Addr, conf.Md5Rabbitmq.Addr)
  141. dwsMq := mq.InitRabbitmq(
  142. conf.DwsRabbitmq.Addr,
  143. conf.DwsRabbitmq.Username,
  144. conf.DwsRabbitmq.Passwrod,
  145. conf.DwsRabbitmq.Vhost,
  146. conf.DwsRabbitmq.ExchangeName,
  147. conf.DwsRabbitmq.QueueName,
  148. conf.DwsRabbitmq.RouteBindKey,
  149. impl.HandleDwsData,
  150. true,
  151. int(consumerCount),
  152. )
  153. mq.SetDwsMq(dwsMq)
  154. adsMq := mq.InitRabbitmq(
  155. conf.AdsRabbitmq.Addr,
  156. conf.AdsRabbitmq.Username,
  157. conf.AdsRabbitmq.Passwrod,
  158. conf.AdsRabbitmq.Vhost,
  159. conf.AdsRabbitmq.ExchangeName,
  160. conf.AdsRabbitmq.QueueName,
  161. conf.AdsRabbitmq.RouteBindKey,
  162. nil,
  163. true,
  164. 0,
  165. )
  166. mq.SetAdsMq(adsMq)
  167. /*
  168. md5Mq := mq.InitRabbitmq(
  169. conf.Md5Rabbitmq.Addr,
  170. conf.Md5Rabbitmq.Username,
  171. conf.Md5Rabbitmq.Passwrod,
  172. conf.Md5Rabbitmq.Vhost,
  173. conf.Md5Rabbitmq.ExchangeName,
  174. conf.Md5Rabbitmq.QueueName,
  175. conf.Md5Rabbitmq.RouteBindKey,
  176. nil,
  177. true,
  178. 0,
  179. )
  180. mq.SetMd5Mq(md5Mq)
  181. */
  182. dwsMq.StartConsumer()
  183. fmt.Println("init rabbitmq end")
  184. }
  185. func start() {
  186. // 优雅关闭服务器
  187. sigChan := make(chan os.Signal, 1)
  188. // 捕获信号
  189. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  190. sigValue := <-sigChan
  191. log.Printf("Got a signal:%v", sigValue)
  192. // 不管什么行为,都等待5秒退出
  193. log.Println("Start to shutdown server...")
  194. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  195. defer cancel()
  196. log.Println("Shutdown server finished.")
  197. }
  198. func main() {
  199. flag.Parse()
  200. if *version {
  201. showVersion()
  202. }
  203. cfg, err := ini.Load(*appConfigFile)
  204. if err != nil {
  205. fmt.Printf("Fail to read file: %v\n\n", err)
  206. os.Exit(1)
  207. }
  208. runmode := cfg.Section("").Key("runmode").String()
  209. etcdAddrs := strings.Split(cfg.Section("").Key("etcd_addrs").String(), ",")
  210. discoveryType := cfg.Section("").Key("discovery_type").String()
  211. utils.SetRunmode(runmode)
  212. prepare(*configFile,etcdAddrs, discoveryType)
  213. go utils.Free()
  214. start()
  215. return
  216. }