W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
對(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è)、訂閱、取消訂閱。顧名思義,概括了客戶端和服務(wù)端與注冊(cè)中心交互的動(dòng)作。針對(duì)普通接口級(jí)服務(wù)注冊(cè)發(fā)現(xiàn)場(chǎng)景,在Provider 服務(wù)啟動(dòng)時(shí),會(huì)將自身服務(wù)接口信息抽象為一個(gè) url,該 url 包含了客戶端發(fā)起調(diào)用所需的所有信息(ip、端口、協(xié)議等),服務(wù)端的注冊(cè)中心組件會(huì)將該 url 寫(xiě)入注冊(cè)中心(例如zk)??蛻舳藛?dòng)后,在服務(wù)引用 Refer 步驟會(huì)通過(guò)注冊(cè)中心組件訂閱(Subscribe)需要的服務(wù)信息,獲取到的服務(wù)信息以異步事件更新的形式寫(xiě)入客戶端緩存,從而在服務(wù)發(fā)現(xiàn)成功后,可以根據(jù)拿到的服務(wù) url 參數(shù),向?qū)?yīng)服務(wù)提供者發(fā)起調(diào)用。
Dubbogo 3.0 版本支持的注冊(cè)中心類(lèi)型如下:
注冊(cè)中心 | 注冊(cè)中心名(用于配置) |
---|---|
Zookeeper | zookeeper |
Nacos | nacos |
Etcd | etcd |
Consul | consul |
可通過(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()
簡(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()
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
下一
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: