main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-system/pb"
  14. "syscall"
  15. "time"
  16. "property-system/impl"
  17. "property-system/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. )
  47. // 执行注册的处理函数
  48. parser.Handle()
  49. }
  50. func run() {
  51. // 开始监听
  52. serveAddr := fmt.Sprintf("%s:%d", parser.Conf.Rpc.System.ServiceIp, parser.Conf.Rpc.System.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. cfg, err := ini.Load(*appConf)
  61. if err != nil {
  62. fmt.Printf("Fail to read file: %v\n\n", err)
  63. os.Exit(1)
  64. }
  65. serviceIp := cfg.Section("").Key("service_ip").String()
  66. parser.EctdRegister(parser.Conf.EtcdAddrs, parser.Conf.Rpc.System.ServiceName,
  67. fmt.Sprintf("%s:%v", serviceIp, parser.Conf.Rpc.System.ServicePort),
  68. parser.Conf.Rpc.Prefix)
  69. // 建立rpc客户端
  70. conns := pb.SetupClients()
  71. for _, conn := range conns {
  72. defer conn.Close()
  73. }
  74. go func() {
  75. if err := s.Serve(lis); err != nil {
  76. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  77. }
  78. }()
  79. // 优雅关闭服务器
  80. sigChan := make(chan os.Signal, 1)
  81. // 捕获信号
  82. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  83. sigValue := <-sigChan
  84. log.Printf("Got a signal:%v", sigValue)
  85. // 让tasker安全退出
  86. tasker.SignalNotify(sigValue)
  87. // 不管什么行为,都等待5秒退出
  88. log.Println("Start to shutdown server...")
  89. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  90. defer cancel()
  91. s.Stop()
  92. log.Println("Shutdown server finished.")
  93. }
  94. func main() {
  95. defer func() {
  96. database.Close()
  97. }()
  98. flag.Parse()
  99. if *version {
  100. showVersion()
  101. os.Exit(1)
  102. }
  103. prepare(*configFile)
  104. run()
  105. return
  106. }