Dubbo-go 的注冊(cè)中心

2022-04-13 17:43 更新

注冊(cè)中心

1. Dubbo 的注冊(cè)中心是什么

對(duì)于 Dubbo-go 微服務(wù)框架,注冊(cè)中心在 RPC 場(chǎng)景下復(fù)雜保存 Provider 應(yīng)用的服務(wù)信息。Provider 注冊(cè)地址到注冊(cè)中心,Consumer 從注冊(cè)中心讀取和訂閱 Provider 地址列表。如圖所示:

關(guān)于 Dubbo 服務(wù)發(fā)現(xiàn)細(xì)節(jié),詳情可參考 Dubbo 官網(wǎng)的概念介紹

Dubbo-go 為注冊(cè)中心抽象了一套接口如下:

// Registry Extension - Registry
type Registry interface {
	common.Node

	// Register is used for service provider calling, register services
	// to registry. And it is also used for service consumer calling, register
	// services cared about, for dubbo's admin monitoring.
	Register(url *common.URL) error

	// UnRegister is required to support the contract:
	// 1. If it is the persistent stored data of dynamic=false, the
	//    registration data can not be found, then the IllegalStateException
	//    is thrown, otherwise it is ignored.
	// 2. Unregister according to the full url match.
	// url Registration information, is not allowed to be empty, e.g:
	// dubbo://10.20.153.10/org.apache.dubbo.foo.BarService?version=1.0.0&application=kylin
	UnRegister(url *common.URL) error

	// Subscribe is required to support the contract:
	// When creating new registry extension, pls select one of the
	// following modes.
	// Will remove in dubbogo version v1.1.0
	// mode1: return Listener with Next function which can return
	//        subscribe service event from registry
	// Deprecated!
	// subscribe(event.URL) (Listener, error)
	// Will replace mode1 in dubbogo version v1.1.0
	// mode2: callback mode, subscribe with notify(notify listener).
	Subscribe(*common.URL, NotifyListener) error

	// UnSubscribe is required to support the contract:
	// 1. If don't subscribe, ignore it directly.
	// 2. Unsubscribe by full URL match.
	// url Subscription condition, not allowed to be empty, e.g.
	// consumer://10.20.153.10/org.apache.dubbo.foo.BarService?version=1.0.0&application=kylin
	// listener A listener of the change event, not allowed to be empty
	UnSubscribe(*common.URL, NotifyListener) error
}

該接口主要包含四個(gè)方法,分別是注冊(cè)、反注冊(cè)、訂閱、取消訂閱。顧名思義,概括了客戶(hù)端和服務(wù)端與注冊(cè)中心交互的動(dòng)作。針對(duì)普通接口級(jí)服務(wù)注冊(cè)發(fā)現(xiàn)場(chǎng)景,在Provider 服務(wù)啟動(dòng)時(shí),會(huì)將自身服務(wù)接口信息抽象為一個(gè) url,該 url 包含了客戶(hù)端發(fā)起調(diào)用所需的所有信息(ip、端口、協(xié)議等),服務(wù)端的注冊(cè)中心組件會(huì)將該 url 寫(xiě)入注冊(cè)中心(例如zk)??蛻?hù)端啟動(dòng)后,在服務(wù)引用 Refer 步驟會(huì)通過(guò)注冊(cè)中心組件訂閱(Subscribe)需要的服務(wù)信息,獲取到的服務(wù)信息以異步事件更新的形式寫(xiě)入客戶(hù)端緩存,從而在服務(wù)發(fā)現(xiàn)成功后,可以根據(jù)拿到的服務(wù) url 參數(shù),向?qū)?yīng)服務(wù)提供者發(fā)起調(diào)用。

2. Dubbo-go 3.0 支持的注冊(cè)中心類(lèi)型

Dubbogo 3.0 版本支持的注冊(cè)中心類(lèi)型如下:

注冊(cè)中心 注冊(cè)中心名(用于配置)
Zookeeper zookeeper
Nacos nacos
Etcd etcd
Consul consul

3. 如何配置注冊(cè)中心

使用配置 API

  • 客戶(hù)端使用配置 API 設(shè)置注冊(cè)中心

可通過(guò)調(diào)用config.NewRegistryConfigWithProtocolDefaultPort方法,快速設(shè)置用于調(diào)試的注冊(cè)中心,支持zookeeper(127.0.0.1:2181) 和nacos(127.0.0.1:8848)

rc := config.NewRootConfigBuilder().
    SetConsumer(config.NewConsumerConfigBuilder().
        SetRegistryIDs("zookeeperID"). // use defined registryID
        Build()).
    AddRegistry("zookeeperID", config.NewRegistryConfigWithProtocolDefaultPort("zookeeper")).
    Build()

全部接口:可通過(guò)調(diào)用RegistryConfigBuilder提供的豐富接口進(jìn)行配置。

rc := config.NewRootConfigBuilder().
    SetConsumer(config.NewConsumerConfigBuilder().
        SetRegistryIDs("nacosRegistryID"). // use defined registryID
        AddReference("GreeterClientImpl",/*...*/).
        Build()
    AddRegistry("nacosRegistryID", config.NewRegistryConfigBuilder().
        SetProtocol("nacos").
        SetAddress("127.0.0.1:8848").
        SetGroup("dubbo-go").
        SetNamespace("dubbo").
        SetUsername("admin").
        SetPassword("admin").
        SetTimeout("3s").
        Build()).
    Build()
  • 服務(wù)端使用配置 API 設(shè)置配置中心

簡(jiǎn)易接口 config.NewRegistryConfigWithProtocolDefaultPort

rc := config.NewRootConfigBuilder().
    SetProvider(config.NewProviderConfigBuilder().
        AddService("GreeterProvider", /*...*/).
        SetRegistryIDs("registryKey").  // use defined registryID
        Build()).
    AddRegistry("registryKey", config.NewRegistryConfigWithProtocolDefaultPort("zookeeper")).
    Build()

全部接口:可通過(guò)調(diào)用RegistryConfigBuilder提供的豐富接口進(jìn)行配置。

rc := config.NewRootConfigBuilder().
    SetProvider(config.NewProviderConfigBuilder().
        AddService("GreeterProvider",/*...*/)
        SetRegistryIDs("registryKey"). // use defined registryID
        Build()).
    AddRegistry("registryKey", config.NewRegistryConfigBuilder().
        SetProtocol("nacos").
        SetAddress("127.0.0.1:8848").
        SetGroup("dubbo-go").
        SetNamespace("dubbo").
        SetUsername("admin").
        SetPassword("admin").
        SetTimeout("3s").
        Build()).
    Build()

使用配置文件

  • 客戶(hù)端/服務(wù)端
dubbo:
  registries:
    demoZK: # define registry-id 'demoZK'
      protocol: zookeeper # set registry protocol
      timeout: 3s
      address: 127.0.0.1:2181
  protocols:
    triple:
      name: tri
      port: 20000
  provider:
    registry-ids:
      - demoZK # use registry-id 'demoZK'
    services:
      GreeterProvider:
        protocol-ids: triple
        interface: com.apache.dubbo.sample.basic.IGreeter 
  consumer:
    registry-ids:
      - demoZK # use registry-id 'demoZK'
    references:
      GreeterClientImpl:
        protocol: tri
        interface: com.apache.dubbo.sample.basic.IGreeter 

下一


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)