main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "log"
  9. "net"
  10. "os"
  11. "os/signal"
  12. "syscall"
  13. "time"
  14. "adm-management/etcd"
  15. "adm-management/impl"
  16. "adm-management/parser"
  17. "git.getensh.com/common/gopkgsv2/database"
  18. "git.getensh.com/common/gopkgsv2/tasker"
  19. "gopkg.in/ini.v1"
  20. "google.golang.org/grpc"
  21. )
  22. var (
  23. configFile = flag.String("config", "/etc/adm/common.yaml", "config file location")
  24. appConf = flag.String("local_config", "/etc/adm-management/app.conf", "config file location")
  25. version = flag.Bool("version", false, "print the version")
  26. GitCommit = "library-import"
  27. Version = "library-import"
  28. )
  29. func showVersion() {
  30. fmt.Println("Version: ", Version)
  31. fmt.Println("GitCommit:", GitCommit)
  32. }
  33. func prepare(filename string) {
  34. // 加载配置
  35. err := parser.LoadConfig(filename)
  36. if err != nil {
  37. fmt.Printf("get conf failed, err: %+v\n\n", err)
  38. os.Exit(1)
  39. }
  40. // 注册处理函数
  41. // parser.Register(parser.MysqlHandler, parser.RedisHandler, parser.LoggerHandler)
  42. parser.Register(parser.LoggerHandler, parser.MysqlHandler, parser.RabbitmqHandler)
  43. // 执行注册的处理函数
  44. parser.Handle()
  45. // 注册rabbitmq
  46. }
  47. func run(discoveryType, etcdAddr, serviceIp, servicePort string) {
  48. if discoveryType == "etcd" {
  49. etcd.Init(etcdAddr, serviceIp, servicePort)
  50. }
  51. // 开始监听
  52. serveAddr := serviceIp + ":" + servicePort
  53. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  54. lis, err := net.Listen("tcp", serveAddr)
  55. if err != nil {
  56. os.Exit(1)
  57. }
  58. s := grpc.NewServer()
  59. impl.Register(s)
  60. go func() {
  61. if err := s.Serve(lis); err != nil {
  62. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  63. }
  64. }()
  65. // 优雅关闭服务器
  66. sigChan := make(chan os.Signal, 1)
  67. // 捕获信号
  68. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  69. sigValue := <-sigChan
  70. log.Printf("Got a signal:%v", sigValue)
  71. // 让tasker安全退出
  72. tasker.SignalNotify(sigValue)
  73. // 不管什么行为,都等待5秒退出
  74. log.Println("Start to shutdown server...")
  75. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  76. defer cancel()
  77. s.Stop()
  78. log.Println("Shutdown server finished.")
  79. }
  80. func main() {
  81. defer func() {
  82. database.Close()
  83. }()
  84. flag.Parse()
  85. if *version {
  86. showVersion()
  87. os.Exit(1)
  88. }
  89. // 读取app.conf内容
  90. cfg, err := ini.Load(*appConf)
  91. if err != nil {
  92. fmt.Printf("get app.conf failed, err:%+v\n", err)
  93. os.Exit(1)
  94. }
  95. discoveryType := cfg.Section("").Key("discoveryType").String()
  96. serviceIp := cfg.Section("").Key("serviceIp").String()
  97. servicePort := cfg.Section("").Key("servicePort").String()
  98. etcdAddr := cfg.Section("").Key("etcdAddr").String()
  99. prepare(*configFile)
  100. // go utils.Free()
  101. run(discoveryType, etcdAddr, serviceIp, servicePort)
  102. return
  103. }