main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/main/rank/conf"
  10. rpc "go-common/app/service/main/rank/server/gorpc"
  11. "go-common/app/service/main/rank/server/http"
  12. "go-common/app/service/main/rank/service"
  13. "go-common/library/conf/env"
  14. ecode "go-common/library/ecode/tip"
  15. "go-common/library/log"
  16. "go-common/library/naming"
  17. "go-common/library/naming/discovery"
  18. xip "go-common/library/net/ip"
  19. "go-common/library/net/trace"
  20. )
  21. func main() {
  22. flag.Parse()
  23. if err := conf.Init(); err != nil {
  24. panic(err)
  25. }
  26. log.Init(conf.Conf.Log)
  27. defer log.Close()
  28. log.Info("start")
  29. trace.Init(conf.Conf.Tracer)
  30. defer trace.Close()
  31. ecode.Init(conf.Conf.Ecode)
  32. // int service
  33. svr := service.New(conf.Conf)
  34. rpcSvr := rpc.New(conf.Conf, svr)
  35. http.Init(conf.Conf, svr)
  36. // start discovery register
  37. var (
  38. err error
  39. cancel context.CancelFunc
  40. )
  41. if env.IP == "" {
  42. ip := xip.InternalIP()
  43. dis := discovery.New(nil)
  44. ins := &naming.Instance{
  45. Zone: env.Zone,
  46. Env: env.DeployEnv,
  47. AppID: env.AppID,
  48. Addrs: []string{
  49. "http://" + ip + ":" + env.HTTPPort,
  50. "gorpc://" + ip + ":" + env.GORPCPort,
  51. },
  52. }
  53. cancel, err = dis.Register(context.Background(), ins)
  54. if err != nil {
  55. panic(err)
  56. }
  57. }
  58. c := make(chan os.Signal, 1)
  59. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  60. for {
  61. s := <-c
  62. log.Info("get a signal %s", s.String())
  63. switch s {
  64. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  65. if cancel != nil {
  66. cancel()
  67. }
  68. rpcSvr.Close()
  69. time.Sleep(time.Second * 2)
  70. svr.Close()
  71. log.Info("rank-service exit")
  72. return
  73. case syscall.SIGHUP:
  74. default:
  75. return
  76. }
  77. }
  78. }