main.go 2.9 KB

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