Dubbo注冊中心與服務(wù)發(fā)現(xiàn)詳解
注冊中心概述
注冊中心是dubbo服務(wù)治理的核心組件,Dubbo依賴注冊中心的協(xié)調(diào)實(shí)現(xiàn)服務(wù)發(fā)現(xiàn),自動化的服務(wù)發(fā)現(xiàn)是微服務(wù)實(shí)現(xiàn)動態(tài)擴(kuò)容、負(fù)載均衡、流量治理的基礎(chǔ)。
Dubbo的服務(wù)發(fā)現(xiàn)機(jī)制經(jīng)歷了Dubbo2時代的接口級服務(wù)發(fā)現(xiàn)、Dubbo3時代的應(yīng)用級服務(wù)發(fā)現(xiàn)。
基本使用
SpringBoot開發(fā):在application.yaml中添加registry配置
dubbo:
registry:
address: { protocol }://${cluster-address}
- protocol:配置中心類型
- cluster-address:注冊中心的集群地址,比如
address: nacos://127.0.0.1:8848,如需要集群格式地址可使用backup參數(shù):address: nacos://localhost:8848?backup=localshot:8846,localshot:8847
配置中心與元數(shù)據(jù)中心
配置中心、元數(shù)據(jù)中心是實(shí)現(xiàn)Dubbo高階服務(wù)治理能力會依賴的組件,如流量管控規(guī)則等。
相比于注冊中心,這兩個組件的配置是可選的。
對于部分注冊中心類型(如Zookeeper、Nacos等),dubbo會默認(rèn)同時將其用作元數(shù)據(jù)中心和配置中心(建議保持默認(rèn)開啟狀態(tài))。
dubbo:
registry:
address: nacos://127.0.0.1:8848
dubbo解析后的默認(rèn)行為:
dubbo: registry: address: nacos://localhost:8848 config-center: address: nacos://localhost:8848 metadata-report: address: nacos://localhost:8848
如果不想使用nacos作為配置中心,可以通過一下兩個參數(shù)來調(diào)整默認(rèn)行為:
dubbo: registry: address: nacos://localhost:8848 use-as-config-center: false use-as-metadata-report: false config-center: address: apollo://localhost:8848
Zookeeper 注冊中心
基本配置
Maven依賴
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- 推薦 Zookeeper Server 3.8.0+ 版本 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- 僅當(dāng) Zookeeper Server 版本是 3.4.x 及以下時,使用此依賴
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
-->
非SpringBoot項(xiàng)目
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<version>${dubbo.version}</version>
<artifactId>dubbo</artifactId>
</dependency>
<!-- This dependency helps to introduce Curator and Zookeeper dependencies that are necessary for Dubbo to work with zookeeper as transitive dependencies. -->
<!-- 僅當(dāng) Zookeeper Server 版本是 3.4.x 及以下時,使用此依賴 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<!-- 僅當(dāng) Zookeeper Server 版本是 3.5.x 及以上時,使用此依賴
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
-->
</dependencies>
Zookeeper版本選擇
Dubbo3 3.3 版本及以上
| Zookeeper Server 版本 | Dubbo依賴 | Dubbo Starter 依賴(SpringBoot用戶) |
|---|---|---|
| 3.4.x 及以下 | dubbo-dependencies-zookeeper | dubbo-zookeeper-spring-boot-starter |
| 3.5.x 及以上 | dubbo-dependencies-zookeeper-curator5 | dubbo-zookeeper-curator5-spring-boot-starter |
Dubbo3 3.2 及以下、Dubbo2 2.7.x
| Zookeeper Server 版本 | Dubbo依賴 | Dubbo Starter 依賴(SpringBoot用戶) |
|---|---|---|
| 3.4.x 及以下 | dubbo-dependencies-zookeeper | 不支持(自行管理) |
| 3.5.x 及以上 | 不支持(自行管理) | 不支持(自行管理) |
配置并啟用Zookeeper
# application.yml dubbo: registry: address: zookeeper://localhost:2181
高級配置
認(rèn)證與鑒權(quán)
如果Zookeeper開啟認(rèn)證,
Dubbo支持指定username、password的方式傳入身份標(biāo)識·。
# application.yml dubbo: registry: address: zookeeper://localhost:2181 username: hello password: 123456
可以將參數(shù)擴(kuò)展在address上
# application.yml dubbo: registry: address: zookeeper://hello:123456@localhost:2181
分組隔離
通過制定group屬性,可以在同一個zookeeper集群內(nèi)實(shí)現(xiàn)微服務(wù)地址的邏輯隔離。
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
group: zookeeper-pre
其它配置
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
timeout: 30 * 1000 # 連接超時時間,默認(rèn) 30s
session: 60 * 1000 # 會話超時時間,默認(rèn) 60s
工作原理
Zookeeper注冊中心實(shí)現(xiàn)支持一下高可用能力:
- 當(dāng)提供者出現(xiàn)斷電等異常停機(jī)時,注冊中心能自動刪除提供者信息。
- 當(dāng)注冊中心重啟時,能自動恢復(fù)注冊數(shù)據(jù),以及訂閱請求。
- 當(dāng)回話過期時,能自動恢復(fù)注冊數(shù)據(jù),以及訂閱請求。
- 當(dāng)設(shè)置
registry.check=false時,記錄失敗注冊和訂閱請求,后臺定時重試。
接口級服務(wù)發(fā)現(xiàn)
- 服務(wù)提供者啟動時,向
/dubbo/com.doudou.demo.api.DemoService/providers目錄下寫入自己的URL地址。 - 服務(wù)消費(fèi)者啟動時,訂閱
/dubbo/com.doudou.demo.api.DemoService/providers目錄下提供的URL地址。并向/dubbo/com.doudou.demo.api.DemoService/consumers目錄下寫入自己的URL地址。 - 監(jiān)控中心啟動時,訂閱
/dubbo/com.doudou.demo.api.DemoService目錄下的所有提供者和消費(fèi)者URL地址。
可通過registry.group設(shè)置zookeeper的根節(jié)點(diǎn),不配置使用默認(rèn)的/dubbo根節(jié)點(diǎn)。
應(yīng)用級服務(wù)發(fā)現(xiàn)
應(yīng)用級服務(wù)發(fā)放以應(yīng)用名為粒度分發(fā)地址列表。服務(wù)提供者啟動著啟動時,向/service/app目錄下寫入自己的URL地址。
應(yīng)用級的URL只包含一些實(shí)例級別的參數(shù),如tri://ip:port?region=hangzhou。
可通過registry.group設(shè)置zookeeper的根節(jié)點(diǎn),默認(rèn)使用/services根節(jié)點(diǎn)。
在應(yīng)用級服務(wù)發(fā)現(xiàn)模型中,接口級別的配置信息由服務(wù)提供者與服務(wù)消費(fèi)者之間自行同步協(xié)商,不再由注冊中心服務(wù)同步,從而大大減少了注冊中心的地址同步壓力。
接口應(yīng)用映射
在應(yīng)用級服務(wù)發(fā)現(xiàn)中,zookeeper注冊中心還會存儲一份額外的元數(shù)據(jù),用于解決接口名到應(yīng)用名之間的映射。存儲路徑:/dubbo/mapping/service。
元數(shù)據(jù)
如果使用應(yīng)用級服務(wù)發(fā)現(xiàn)的集中式元數(shù)據(jù)模式,默認(rèn)是點(diǎn)對點(diǎn)模式,可通過dubbo.registry.metadata-type=remote開啟。在zookeeper服務(wù)/dubbo/metadata目錄下會保存每個應(yīng)用對于機(jī)器的元數(shù)據(jù)信息。
Nacos注冊中心
基本配置
maven依賴
springboot應(yīng)用
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
非springboot應(yīng)用
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
Nacos版本
| Dubbo | 推薦Nacos版本 | Nacos兼容范圍 |
|---|---|---|
| 3.3.0 | 2.3.0 | 2.x |
| 3.2.21 | 2.1.0 | 2.x |
| 3.1.11 | 2.0.9 | 2.x |
| 3.0.10 | 2.0.9 | 2.x |
| 2.7.21 | 1.x最新版本 | 1.x |
| 2.6.0 | 1.x最新版本 | 1.x |
配置并啟用Nacos
# application.yml (Spring Boot) dubbo: registry: address: nacos://localhost:8848
高級配置
認(rèn)證
# application.yml (Spring Boot) dubbo: registry: address: nacos://localhost:8848?username=nacos&password=nacos
或
# dubbo.properties dubbo.registry.address: nacos://nacos:nacos@localhost:8848
自定義命名空間
# application.yml (Spring Boot) dubbo: registry: address: nacos://localhost:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932
或
# application.yml (Spring Boot) dubbo: registry: address: nacos://localhost:8848 parameters.namespace: 5cbb70a5-xxx-xxx-xxx-d43479ae0932
自定義分組
# application.yml dubbo: registry: address: nacos://localhost:8848 group: dubbo
如果不配置的話,group 是由 Nacos 默認(rèn)指定。group 和 namespace 在 Nacos 中代表不同的隔離層次,通常來說 namespace 用來隔離不同的用戶或環(huán)境,group 用來對同一環(huán)境內(nèi)的數(shù)據(jù)做進(jìn)一步歸組。
注冊接口級消費(fèi)者
Dubbo 3.0.0 版本以后,增加了是否注冊消費(fèi)者的參數(shù),如果需要將消費(fèi)者注冊到 nacos 注冊中心上,需要將參數(shù)(register-consumer-url)設(shè)置為true,默認(rèn)是false。
# application.yml
dubbo:
registry:
address: nacos://localhost:8848?register-consumer-url=true
或
# application.yml
dubbo:
registry:
address: nacos://localhost:8848
parameters.register-consumer-url: true
多注冊中心
關(guān)聯(lián)服務(wù)與多注冊中心
全局默認(rèn)注冊中心
Dubbo框架會自動執(zhí)行以下動作:
- 對于所有的Service服務(wù),向所有全局默認(rèn)注冊中心注冊服務(wù)地址。
- 對于所有的Reference服務(wù),向所有全局默認(rèn)注冊中心訂閱服務(wù)地址。
# application.yml (Spring Boot)
dubbo:
registries:
nacosPreRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: pre
group: DUBBO_GROUP
nacosTestRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: test1
group: DUBBO_GROUP
@DubboService
public class DemoServiceImpl implements DemoService {}
@DubboService
public class HelloServiceImpl implements HelloService {}
設(shè)置全局默認(rèn)注冊中心
dubbo.registry.default用來設(shè)置默認(rèn)注冊中心,默認(rèn)值為true,即被視作全局注冊中心。
未指定注冊中心的服務(wù)自動注冊或訂閱全局默認(rèn)注冊中心。
# application.yml (Spring Boot)
dubbo:
registries:
nacosPreRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: pre
group: DUBBO_GROUP
default: true # 默認(rèn)是true
nacosTestRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: test1
group: DUBBO_GROUP
default: false
顯示關(guān)聯(lián)服務(wù)于注冊中心
以下配置,DemoService只注冊到nacosPreRegistry配置地址,HelloService只注冊到nacosTestRegistry配置地址。
@DubboService(registry = {"nacosPreRegistry"})
public class DemoServiceImpl implements DemoService {}
@DubboService(registry = {"nacosTestRegistry"})
public class HelloServiceImpl implements HelloService {}
多注冊中心訂閱
從單個服務(wù)訂閱的視角,如果存在多個注冊中心訂閱的情況,則可以根據(jù)注冊中心間的地址是否聚合分為兩種情況:多注冊中心地址不聚合;多注冊中心地址聚合。
多注冊中心地址不聚合
若注冊中心標(biāo)記為dubbo.registry.default:true,則所有的服務(wù)都會某人注冊到這些注冊中心,消費(fèi)者也會聚合訂閱這些注冊中心。
不聚合配置
- 取消全局默認(rèn)標(biāo)記:確保注冊中心未配置
dubbo.registry.default:true。 - 顯示指定服務(wù)于注冊中心的關(guān)系: 通過服務(wù)級配置隔離注冊中心。
# application.yml (Spring Boot)
dubbo:
registries:
nacosPreRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: pre
group: DUBBO_GROUP
default: true # 默認(rèn)是true
nacosTestRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: test1
group: DUBBO_GROUP
default: false
@DubboService(registry = {"nacosPreRegistry"})
public class DemoServiceImpl implements DemoService {}
@DubboService(registry = {"nacosTestRegistry"})
public class HelloServiceImpl implements HelloService {}
多注冊中心地址聚合
通過設(shè)置多個注冊中心為全局默認(rèn),使服務(wù)同時注冊到多個注冊中心,消費(fèi)者也從所有默認(rèn)注冊中心訂閱地址。
服務(wù)提供者@DubboService或服務(wù)消費(fèi)者@DubboReference為指定reigstry配置時,會訂閱所有的默認(rèn)注冊中心。
# application.yml (Spring Boot)
dubbo:
registries:
nacosPreRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: pre
group: DUBBO_GROUP
default: true # 默認(rèn)是true
nacosTestRegistry:
address: nacos://nacos:nacos@127.0.0.1:8848
parameters.namespace: test1
group: DUBBO_GROUP
default: true
Multicast注冊中心
Multicast 注冊中心不需要啟動任何中心節(jié)點(diǎn),只要廣播地址一樣,就可以互相發(fā)現(xiàn)。
使用
# 服務(wù)提供者與服務(wù)消費(fèi)者
# application.yaml
dubbo:
registry:
address: multicast://224.5.6.7:1234
工作原理
基本流程
- 提供方啟動時廣播自己的地址。
- 消費(fèi)方啟動時廣播訂閱請求。
- 提供方接收到訂閱請求時,單播自己的地址給訂閱者,如果設(shè)置了
unicast=false,則廣播給訂閱者。 - 消費(fèi)方收到提供方地址后,連接該地址進(jìn)行RPC調(diào)用。
使用限制
組播受網(wǎng)絡(luò)結(jié)構(gòu)限制,只適合小規(guī)模應(yīng)用或開發(fā)階段使用。組播地址段:224.0.0.0 - 239.255.255.255
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud Eureka 注冊與發(fā)現(xiàn)操作步驟詳解
這篇文章主要介紹了Spring Cloud Eureka 注冊與發(fā)現(xiàn)操作步驟詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
SpringBoot之那些注入不了的Spring占位符(${}表達(dá)式)問題
這篇文章主要介紹了SpringBoot之那些注入不了的Spring占位符(${}表達(dá)式)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
java 分轉(zhuǎn)元與元轉(zhuǎn)分實(shí)現(xiàn)操作
這篇文章主要介紹了java 分轉(zhuǎn)元與元轉(zhuǎn)分實(shí)現(xiàn)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot多數(shù)據(jù)源的兩種實(shí)現(xiàn)方式實(shí)例
最近在項(xiàng)目開發(fā)中,需要為一個使用MySQL數(shù)據(jù)庫的SpringBoot項(xiàng)目,新添加一個PLSQL數(shù)據(jù)庫數(shù)據(jù)源,下面這篇文章主要給大家介紹了關(guān)于SpringBoot多數(shù)據(jù)源的兩種實(shí)現(xiàn)方式,需要的朋友可以參考下2022-04-04
如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境)
spark程序可以通過pom.xml的文件配置,添加spark-core依賴,可以直接在IDEA中編寫spark程序并運(yùn)行結(jié)果,這篇文章主要介紹了如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境),需要的朋友可以參考下2024-02-02
聊聊spring @Transactional 事務(wù)無法使用的可能原因
這篇文章主要介紹了spring @Transactional 事務(wù)無法使用的可能原因,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

