服務群組
功能說明
相同的介面可以使用服務分組來區分不同業務場景、不同使用需求或不同功能模組的不同實作方法。同時,這些不同實作提供的服務可以共存並支援相互呼叫。
使用場景
當一個介面有多個實作時,可以使用群組來區分。
參考用例
https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-group
使用方法
註解配置
服務提供者(註解配置)
使用 @DubboService 註解,新增 group 參數
@DubboService(group = "demo")
public class DemoServiceImpl implements DemoService {
...
}
@DubboService(group = "demo2")
public class Demo2ServiceImpl implements DemoService {
...
}
啟動 Dubbo 服務,您可以在註冊中心中看到具有相同服務名稱和不同群組的服務。以 Nacos 作為註冊中心為例,將顯示以下內容
服務消費者(註解配置)
使用 @DubboReference 註解,新增 group 參數
@DubboReference(group = "demo")
private DemoService demoService;
@DubboReference(group = "demo2")
private DemoService demoService2;
//group value is *, the identifier matches any service group
@DubboReference(group = "*")
private DemoService demoService2;
群組聚合
參考範例 https://github.com/apache/dubbo-samples/tree/master/2-advanced/dubbo-samples-merge
// Group aggregation, merging all groups and returning the result
@DubboReference(group = "*", merger = "true")
private DemoService demoService2;
// Group aggregation, merging specified groups and returning the result
@DubboReference(group = "merge,merge2", merger = "true")
private DemoService demoService2;
啟動 Dubbo 服務後,您可以在註冊中心中看到相同服務名稱在不同群組中的引用。以 Nacos 作為註冊中心為例,將顯示以下內容:
XML 配置
服務提供者(XML 配置)
使用 <dubbo:service /> 標籤,新增 group 參數
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="https://dubbo.dev.org.tw/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
https://dubbo.dev.org.tw/schema/dubbo
https://dubbo.dev.org.tw/schema/dubbo/dubbo.xsd">
...
<dubbo:service interface="org.apache.dubbo.example.service.DemoService" group="demo"/>
<dubbo:service interface="org.apache.dubbo.example.service.DemoService" group="demo2"/>
...
</beans>
啟動 Dubbo 服務,您可以在註冊中心中看到具有相同服務名稱和不同群組的服務。以 Nacos 作為註冊中心為例,將顯示以下內容
服務消費者(XML 配置)
使用 dubbo:reference/ 註解,新增 group 參數
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="https://dubbo.dev.org.tw/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
https://dubbo.dev.org.tw/schema/dubbo
https://dubbo.dev.org.tw/schema/dubbo/dubbo.xsd">
...
<!-- reference service interface -->
<dubbo:reference id="demoService" interface="org.apache.dubbo.example.service.DemoService" group="demo"/>
<dubbo:reference id="demoService2" interface="org.apache.dubbo.example.service.DemoService" group="demo2"/>
<!-- The group value is *, and the identifier matches any service group -->
<dubbo:reference id="demoService3" interface="org.apache.dubbo.example.service.DemoService" group="*"/>
...
</beans>
啟動 Dubbo 服務後,您可以在註冊中心中看到相同服務名稱在不同群組中的引用。以 Nacos 作為註冊中心為例,將顯示以下內容
API 配置
服務提供者(API 配置)
使用 org.apache.dubbo.config.ServiceConfig 類別,新增 group 參數
// ServiceConfig is a heavy object, which internally encapsulates the connection with the registration center and opens the service port
// Please cache by yourself, otherwise it may cause memory and connection leaks
ServiceConfig<DemoService> service = new ServiceConfig<>();
service.setInterface(DemoService.class);
service.setGroup("demo");
...
ServiceConfig<DemoService> service2 = new ServiceConfig<>();
service.setInterface(DemoService.class);
service.setGroup("demo2");
...
啟動 Dubbo 服務,您可以在註冊中心中看到具有相同服務名稱和不同群組的服務。以 Nacos 作為註冊中心為例,將顯示以下內容
服務消費者(API 配置)
使用 org.apache.dubbo.config.ReferenceConfig,新增 group 參數
// ReferenceConfig is a heavy object, which internally encapsulates the connection with the registration center and opens the service port
// Please cache by yourself, otherwise it may cause memory and connection leaks
ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
reference.setInterface(DemoService.class);
reference.setGroup("demo");
...
ReferenceConfig<DemoService> reference2 = new ReferenceConfig<>();
reference2.setInterface(DemoService.class);
reference2.setGroup("demo2");
...
ReferenceConfig<DemoService> reference3 = new ReferenceConfig<>();
reference3.setInterface(DemoService.class);
reference3.setGroup("*");
...
啟動 Dubbo 服務後,您可以在註冊中心中看到相同服務名稱在不同群組中的引用。以 Nacos 作為註冊中心為例,將顯示以下內容:
永遠只*呼叫*一個可用的群組實作。