注册中心

注册中心源码解读

Dubbogo 为注册中心抽象了一套接口如下:

  1. // Registry Extension - Registry
  2. type Registry interface {
  3. common.Node
  4. // Register is used for service provider calling, register services
  5. // to registry. And it is also used for service consumer calling, register
  6. // services cared about, for dubbo's admin monitoring.
  7. Register(url *common.URL) error
  8. // UnRegister is required to support the contract:
  9. // 1. If it is the persistent stored data of dynamic=false, the
  10. // registration data can not be found, then the IllegalStateException
  11. // is thrown, otherwise it is ignored.
  12. // 2. Unregister according to the full url match.
  13. // url Registration information, is not allowed to be empty, e.g:
  14. // dubbo://10.20.153.10/org.apache.dubbo.foo.BarService?version=1.0.0&application=kylin
  15. UnRegister(url *common.URL) error
  16. // Subscribe is required to support the contract:
  17. // When creating new registry extension, pls select one of the
  18. // following modes.
  19. // Will remove in dubbogo version v1.1.0
  20. // mode1: return Listener with Next function which can return
  21. // subscribe service event from registry
  22. // Deprecated!
  23. // subscribe(event.URL) (Listener, error)
  24. // Will replace mode1 in dubbogo version v1.1.0
  25. // mode2: callback mode, subscribe with notify(notify listener).
  26. Subscribe(*common.URL, NotifyListener) error
  27. // UnSubscribe is required to support the contract:
  28. // 1. If don't subscribe, ignore it directly.
  29. // 2. Unsubscribe by full URL match.
  30. // url Subscription condition, not allowed to be empty, e.g.
  31. // consumer://10.20.153.10/org.apache.dubbo.foo.BarService?version=1.0.0&application=kylin
  32. // listener A listener of the change event, not allowed to be empty
  33. UnSubscribe(*common.URL, NotifyListener) error
  34. }

该接口主要包含四个方法,分别是注册、反注册、订阅、取消订阅。顾名思义,概括了客户端和服务端与注册中心交互的动作。针对普通接口级服务注册发现场景,在Provider 服务启动时,会将自身服务接口信息抽象为一个 url,该 url 包含了客户端发起调用所需的所有信息(ip、端口、协议等),服务端的注册中心组件会将该 url 写入注册中心(例如zk)。客户端启动后,在服务引用 Refer 步骤会通过注册中心组件订阅(Subscribe)需要的服务信息,获取到的服务信息以异步事件更新的形式写入客户端缓存,从而在服务发现成功后,可以根据拿到的服务 url 参数,向对应服务提供者发起调用。

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)