main.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. "time"
  10. "go-common/app/service/main/account/conf"
  11. rpc "go-common/app/service/main/account/rpc/server"
  12. "go-common/app/service/main/account/server/grpc"
  13. "go-common/app/service/main/account/server/http"
  14. "go-common/app/service/main/account/service"
  15. "go-common/library/log"
  16. "go-common/library/net/rpc/warden"
  17. "go-common/library/net/rpc/warden/resolver/livezk"
  18. "go-common/library/net/trace"
  19. )
  20. const (
  21. discoveryID = "account.service"
  22. )
  23. func main() {
  24. flag.Parse()
  25. if err := conf.Init(); err != nil {
  26. log.Error("conf.Init() error(%v)", err)
  27. panic(err)
  28. }
  29. log.Init(conf.Conf.Log)
  30. defer log.Close()
  31. trace.Init(conf.Conf.Tracer)
  32. defer trace.Close()
  33. log.Info("account-service start")
  34. // service init
  35. svr := service.New(conf.Conf)
  36. rpcSvr := rpc.New(conf.Conf, svr)
  37. // warden init
  38. var wardensvr *warden.Server
  39. if conf.Conf.WardenServer != nil {
  40. var err error
  41. if wardensvr, err = grpc.Start(conf.Conf, svr); err != nil {
  42. panic(fmt.Sprintf("start warden server fail! %s", err))
  43. }
  44. cancel, err := livezk.Register(conf.Conf.LiveZK, conf.Conf.WardenServer.Addr, discoveryID)
  45. if err != nil {
  46. panic(fmt.Sprintf("register grpc service into live zookeeper error: %s", err))
  47. }
  48. defer cancel()
  49. }
  50. http.Init(conf.Conf, svr)
  51. // signal handler
  52. c := make(chan os.Signal, 1)
  53. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  54. for {
  55. s := <-c
  56. log.Info("account-service get a signal %s", s.String())
  57. switch s {
  58. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  59. log.Info("account-service exit")
  60. rpcSvr.Close()
  61. if wardensvr != nil {
  62. wardensvr.Shutdown(context.Background())
  63. }
  64. time.Sleep(time.Second)
  65. return
  66. case syscall.SIGHUP:
  67. // TODO reload
  68. default:
  69. return
  70. }
  71. }
  72. }