main.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "go-common/app/service/main/seq-server/conf"
  10. "go-common/app/service/main/seq-server/http"
  11. rpc "go-common/app/service/main/seq-server/rpc/server"
  12. "go-common/app/service/main/seq-server/service"
  13. "go-common/library/conf/env"
  14. "go-common/library/log"
  15. "go-common/library/naming"
  16. "go-common/library/naming/discovery"
  17. xip "go-common/library/net/ip"
  18. "go-common/library/net/trace"
  19. )
  20. func main() {
  21. flag.Parse()
  22. if err := conf.Init(); err != nil {
  23. log.Error("conf.Init() error(%v)", err)
  24. panic(err)
  25. }
  26. // init log
  27. log.Init(conf.Conf.Log)
  28. defer log.Close()
  29. trace.Init(conf.Conf.Tracer)
  30. defer trace.Close()
  31. log.Info("seq-server start")
  32. // service init
  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: "seq.server",
  48. Addrs: []string{
  49. "http://" + ip + ":" + env.HTTPPort,
  50. "gorpc://" + ip + ":" + env.GORPCPort,
  51. },
  52. }
  53. if cancel, err = dis.Register(context.Background(), ins); err != nil {
  54. panic(err)
  55. }
  56. }
  57. // end discovery register
  58. // init signal
  59. c := make(chan os.Signal, 1)
  60. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  61. for {
  62. s := <-c
  63. log.Info("seq-server get a signal %s", s.String())
  64. switch s {
  65. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  66. if cancel != nil {
  67. cancel()
  68. }
  69. rpcSvr.Close()
  70. time.Sleep(time.Second * 2)
  71. log.Info("seq-server exit")
  72. return
  73. case syscall.SIGHUP:
  74. // TODO reload
  75. default:
  76. return
  77. }
  78. }
  79. }