服务分组

使用服务分组区分服务接口的不同实现

功能描述

同一个接口可以使用服务分组区分不同业务场景、不同使用需求或不同功能模块的不同实现方式。同时,这些不同实现提供的服务可以共存并支持相互调用。

使用场景

当一个接口有多个实现时,可以通过分组进行区分。

参考用例

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 作为注册中心为例,将显示以下内容

image-service-group-1.png

服务消费者(注解配置)

使用 @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 作为注册中心为例,将显示以下内容: image-service-group-2.png

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.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       https://dubbo.apache.org/schema/dubbo
       https://dubbo.apache.org/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 作为注册中心为例,将显示以下内容

image-service-group-1.png

服务消费者(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.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       https://dubbo.apache.org/schema/dubbo
       https://dubbo.apache.org/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 作为注册中心为例,将显示以下内容

image-service-group-2.png

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 作为注册中心为例,将显示以下内容

image-service-group-1.png

服务消费者(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 作为注册中心为例,将显示以下内容: image-service-group-2.png

始终调用* 仅一个可用的分组实现