component.go 1000 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package mhayaDiscovery
  2. import (
  3. cfacade "github.com/mhaya/facade"
  4. clog "github.com/mhaya/logger"
  5. cprofile "github.com/mhaya/profile"
  6. )
  7. const (
  8. Name = "discovery_component"
  9. )
  10. type Component struct {
  11. cfacade.Component
  12. cfacade.IDiscovery
  13. }
  14. func New() *Component {
  15. return &Component{}
  16. }
  17. func (*Component) Name() string {
  18. return Name
  19. }
  20. func (p *Component) Init() {
  21. config := cprofile.GetConfig("cluster").GetConfig("discovery")
  22. if config.LastError() != nil {
  23. clog.Error("`cluster` property not found in profile file.")
  24. return
  25. }
  26. mode := config.GetString("mode")
  27. if mode == "" {
  28. clog.Error("`discovery->mode` property not found in profile file.")
  29. return
  30. }
  31. discovery, found := discoveryMap[mode]
  32. if discovery == nil || !found {
  33. clog.Errorf("mode = %s property not found in discovery config.", mode)
  34. return
  35. }
  36. clog.Infof("Select discovery [mode = %s].", mode)
  37. p.IDiscovery = discovery
  38. p.IDiscovery.Load(p.App())
  39. }
  40. func (p *Component) OnStop() {
  41. p.IDiscovery.Stop()
  42. }