main.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "go-common/app/infra/config/conf"
  9. "go-common/app/infra/config/http"
  10. "go-common/app/infra/config/rpc/server"
  11. "go-common/app/infra/config/service/v1"
  12. "go-common/app/infra/config/service/v2"
  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. )
  19. func main() {
  20. flag.Parse()
  21. if err := conf.Init(); err != nil {
  22. log.Error("conf.Init() error(%v)", err)
  23. panic(err)
  24. }
  25. // init log
  26. log.Init(conf.Conf.Log)
  27. defer log.Close()
  28. // service init
  29. svr2 := v2.New(conf.Conf)
  30. svr := v1.New(conf.Conf)
  31. rpcSvr := rpc.New(conf.Conf, svr, svr2)
  32. http.Init(conf.Conf, svr, svr2, rpcSvr)
  33. // start discovery register
  34. var (
  35. err error
  36. cancel context.CancelFunc
  37. )
  38. if env.IP == "" {
  39. ip := xip.InternalIP()
  40. hn, _ := os.Hostname()
  41. dis := discovery.New(nil)
  42. ins := &naming.Instance{
  43. Zone: env.Zone,
  44. Env: env.DeployEnv,
  45. AppID: "config.service",
  46. Hostname: hn,
  47. Addrs: []string{
  48. "http://" + ip + ":" + env.HTTPPort,
  49. "gorpc://" + ip + ":" + env.GORPCPort,
  50. },
  51. }
  52. if cancel, err = dis.Register(context.Background(), ins); err != nil {
  53. panic(err)
  54. }
  55. }
  56. // end discovery register
  57. // init signal
  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("config-service 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. svr.Close()
  70. log.Info("config-service exit")
  71. return
  72. case syscall.SIGHUP:
  73. // TODO reload
  74. default:
  75. return
  76. }
  77. }
  78. }