facade.go 862 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package discov
  2. import (
  3. "github.com/tal-tech/go-zero/core/discov/internal"
  4. "github.com/tal-tech/go-zero/core/logx"
  5. )
  6. type (
  7. Facade struct {
  8. endpoints []string
  9. registry *internal.Registry
  10. }
  11. FacadeListener interface {
  12. OnAdd(key, val string)
  13. OnDelete(key string)
  14. }
  15. )
  16. func NewFacade(endpoints []string) Facade {
  17. return Facade{
  18. endpoints: endpoints,
  19. registry: internal.GetRegistry(),
  20. }
  21. }
  22. func (f Facade) Client() internal.EtcdClient {
  23. conn, err := f.registry.GetConn(f.endpoints)
  24. logx.Must(err)
  25. return conn
  26. }
  27. func (f Facade) Monitor(key string, l FacadeListener) {
  28. f.registry.Monitor(f.endpoints, key, listenerAdapter{l})
  29. }
  30. type listenerAdapter struct {
  31. l FacadeListener
  32. }
  33. func (la listenerAdapter) OnAdd(kv internal.KV) {
  34. la.l.OnAdd(kv.Key, kv.Val)
  35. }
  36. func (la listenerAdapter) OnDelete(kv internal.KV) {
  37. la.l.OnDelete(kv.Key)
  38. }