cmd.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package boot
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/gomodule/redigo/redis"
  11. "github.com/spf13/viper"
  12. "go.uber.org/zap"
  13. "passport/app/router"
  14. "passport/app/rpc"
  15. "passport/client/mysql"
  16. "passport/client/sredis"
  17. "passport/logger"
  18. )
  19. const (
  20. // gracefulTimeout controls how long we wait before forcefully terminating
  21. gracefulTimeout = 3 * time.Second
  22. )
  23. func Run() int {
  24. var err error
  25. StoreInit()
  26. err = InitModel()
  27. if err != nil {
  28. panic(err)
  29. }
  30. go StartHttpServer()
  31. go rpc.StartGrpcServer()
  32. return handleSignals()
  33. }
  34. //初始化相关必要持久化服务
  35. func StoreInit() {
  36. //连接mysql
  37. var err error
  38. // 连接数据库
  39. _, err = mysql.Dial("mysql", viper.GetString("mysql.address"))
  40. if err != nil {
  41. panic(err)
  42. }
  43. //初始化redis
  44. sredis.MasterPool = &redis.Pool{ //实例化一个连接池
  45. MaxIdle: 16, //最初的连接数量
  46. // MaxActive:1000000, //最大连接数量
  47. MaxActive: 0, //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
  48. IdleTimeout: 300, //连接关闭时间 300秒 (300秒不使用自动关闭)
  49. Dial: func() (redis.Conn, error) { //要连接的redis数据库
  50. conn, err := redis.Dial("tcp", viper.GetString("redis.master.address"))
  51. if err != nil {
  52. return nil, err
  53. }
  54. if viper.GetString("redis.master.password") != "" {
  55. _, err = conn.Do("AUTH", viper.GetString("redis.master.password"))
  56. if err != nil {
  57. return nil, err
  58. }
  59. }
  60. return conn, err
  61. },
  62. }
  63. sredis.SalvePool = &redis.Pool{ //实例化一个连接池
  64. MaxIdle: 16, //最初的连接数量
  65. // MaxActive:1000000, //最大连接数量
  66. MaxActive: 0, //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
  67. IdleTimeout: 300, //连接关闭时间 300秒 (300秒不使用自动关闭)
  68. Dial: func() (redis.Conn, error) { //要连接的redis数据库
  69. conn, err := redis.Dial("tcp", viper.GetString("redis.slave.address"))
  70. if err != nil {
  71. return nil, err
  72. }
  73. if viper.GetString("redis.slave.password") != "" {
  74. _, err = conn.Do("AUTH", viper.GetString("redis.slave.password"))
  75. if err != nil {
  76. return nil, err
  77. }
  78. }
  79. return conn, err
  80. },
  81. }
  82. }
  83. //启动服务
  84. func StartHttpServer() {
  85. //
  86. r := gin.Default()
  87. router.StepRouters(r)
  88. addr := fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))
  89. logger.Info(fmt.Sprintf("Http Server has been started. http://%s", addr))
  90. logger.Fatal("start http server", zap.Error(http.ListenAndServe(addr, r)))
  91. }
  92. func handleSignals() int {
  93. signalCh := make(chan os.Signal, 4)
  94. signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
  95. WAIT:
  96. // Wait for a signal
  97. var sig os.Signal
  98. select {
  99. case s := <-signalCh:
  100. sig = s
  101. }
  102. //log.Info(fmt.Sprintf("Caught signal: %v", sig))
  103. // Check if this is a SIGHUP
  104. if sig == syscall.SIGHUP {
  105. //重新加载配置
  106. goto WAIT
  107. }
  108. // Check if we should do a graceful leave
  109. graceful := false
  110. if sig == syscall.SIGTERM || sig == os.Interrupt {
  111. graceful = true
  112. }
  113. // Fail fast if not doing a graceful leave
  114. if !graceful {
  115. return 1
  116. }
  117. // Attempt a graceful leave
  118. gracefulCh := make(chan struct{})
  119. //log.Info("Gracefully shutting down...")
  120. go func() {
  121. //if err := c.Sys.Leave(); err != nil {
  122. // log.Error(fmt.Sprintf("Error: %s", err))
  123. // return
  124. //}
  125. close(gracefulCh)
  126. }()
  127. // Wait for leave or another signal
  128. select {
  129. case <-signalCh:
  130. return 1
  131. case <-time.After(gracefulTimeout):
  132. return 1
  133. case <-gracefulCh:
  134. return 0
  135. }
  136. }