main.go 2.8 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-company/pb"
  14. "syscall"
  15. "time"
  16. "property-company/impl"
  17. "property-company/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.Company.ServiceIp, parser.Conf.Rpc.Company.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. if len(parser.Conf.EtcdAddrs) > 0 {
  67. parser.EctdRegister(parser.Conf.EtcdAddrs, parser.Conf.Rpc.Company.ServiceName,
  68. fmt.Sprintf("%s:%v", serviceIp, parser.Conf.Rpc.Company.ServicePort),
  69. parser.Conf.Rpc.Prefix)
  70. }
  71. // 建立rpc客户端
  72. conns := pb.SetupClients()
  73. for _, conn := range conns {
  74. defer conn.Close()
  75. }
  76. go func() {
  77. if err := s.Serve(lis); err != nil {
  78. log.Fatalf("HTTP server listen failed. err: %s\n", err.Error())
  79. }
  80. }()
  81. // 优雅关闭服务器
  82. sigChan := make(chan os.Signal, 1)
  83. // 捕获信号
  84. signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
  85. sigValue := <-sigChan
  86. log.Printf("Got a signal:%v", sigValue)
  87. // 让tasker安全退出
  88. tasker.SignalNotify(sigValue)
  89. // 不管什么行为,都等待5秒退出
  90. log.Println("Start to shutdown server...")
  91. _, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  92. defer cancel()
  93. s.Stop()
  94. log.Println("Shutdown server finished.")
  95. }
  96. func main() {
  97. defer func() {
  98. database.Close()
  99. }()
  100. flag.Parse()
  101. if *version {
  102. showVersion()
  103. os.Exit(1)
  104. }
  105. prepare(*configFile)
  106. run()
  107. return
  108. }