main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "gopkg.in/ini.v1"
  9. "log"
  10. "net"
  11. "os"
  12. "os/signal"
  13. "property-garden/pb"
  14. "syscall"
  15. "time"
  16. "property-garden/impl"
  17. "property-garden/parser"
  18. "git.getensh.com/common/gopkgs/database"
  19. "git.getensh.com/common/gopkgs/tasker"
  20. "google.golang.org/grpc"
  21. )
  22. var (
  23. configFile = flag.String("config", "conf/common.yaml", "config file location")
  24. appConf = flag.String("appconf", "conf/app.conf", "app conf 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(
  43. parser.MysqlHandler,
  44. parser.LoggerHandler,
  45. parser.RedisHandler,
  46. parser.MgoHandler,
  47. )
  48. // 执行注册的处理函数
  49. parser.Handle()
  50. }
  51. func run() {
  52. // 开始监听
  53. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.Garden.ServiceIp, parser.Conf.Rpc.Garden.ServicePort)
  54. log.Printf("Listening and serving TCP on %s\n", serveAddr)
  55. lis, err := net.Listen("tcp", serveAddr)
  56. if err != nil {
  57. os.Exit(1)
  58. }
  59. s := grpc.NewServer()
  60. impl.Register(s)
  61. cfg, err := ini.Load(*appConf)
  62. if err != nil {
  63. fmt.Printf("Fail to read file: %v\n\n", err)
  64. os.Exit(1)
  65. }
  66. serviceIp := cfg.Section("").Key("service_ip").String()
  67. if len(parser.Conf.EtcdAddrs) > 0 {
  68. parser.EctdRegister(parser.Conf.EtcdAddrs, parser.Conf.Rpc.Garden.ServiceName,
  69. fmt.Sprintf("%s:%v", serviceIp, parser.Conf.Rpc.Garden.ServicePort),
  70. parser.Conf.Rpc.Prefix)
  71. }
  72. // 建立rpc客户端
  73. conns := pb.SetupClients()
  74. for _, conn := range conns {
  75. defer conn.Close()
  76. }
  77. go func() {
  78. if err := s.Serve(lis); err != nil {
  79. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  80. }
  81. }()
  82. // 优雅关闭服务器
  83. sigChan := make(chan os.Signal, 1)
  84. // 捕获信号
  85. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  86. sigValue := <-sigChan
  87. log.Printf("Got a signal:%v", sigValue)
  88. // 让tasker安全退出
  89. tasker.SignalNotify(sigValue)
  90. // 不管什么行为,都等待5秒退出
  91. log.Println("Start to shutdown server...")
  92. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  93. defer cancel()
  94. s.Stop()
  95. log.Println("Shutdown server finished.")
  96. }
  97. func main() {
  98. defer func() {
  99. database.Close()
  100. }()
  101. flag.Parse()
  102. if *version {
  103. showVersion()
  104. os.Exit(1)
  105. }
  106. prepare(*configFile)
  107. run()
  108. return
  109. }