service.go 907 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package service
  2. import (
  3. "context"
  4. "os"
  5. "sync"
  6. "go-common/app/infra/discovery/conf"
  7. "go-common/app/infra/discovery/dao"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. // Service discovery main service
  11. type Service struct {
  12. c *conf.Config
  13. client *bm.Client
  14. registry *dao.Registry
  15. nodes *dao.Nodes
  16. tLock sync.RWMutex
  17. tree map[int64]string // treeid->appid
  18. env *env
  19. }
  20. type env struct {
  21. Region string
  22. Zone string
  23. }
  24. // New get a discovery service
  25. func New(c *conf.Config) (s *Service, cancel context.CancelFunc) {
  26. s = &Service{
  27. c: c,
  28. client: bm.NewClient(c.HTTPClient),
  29. registry: dao.NewRegistry(),
  30. nodes: dao.NewNodes(c),
  31. tree: make(map[int64]string),
  32. }
  33. s.getEnv()
  34. s.syncUp()
  35. cancel = s.regSelf()
  36. go s.nodesproc()
  37. return
  38. }
  39. func (s *Service) getEnv() {
  40. s.env = &env{
  41. Region: os.Getenv("REGION"),
  42. Zone: os.Getenv("ZONE"),
  43. }
  44. }