The same interface can use service grouping to distinguish different implementation methods for different business scenarios, different usage requirements, or different functional modules. At the same time, the services provided by these different implementations can coexist and support mutual calls.
When an interface has multiple implementations, it can be distinguished by group.
https://github.com/apache/dubbo-samples/tree/master/dubbo-samples-group
Use @DubboService annotation, add group parameter
@DubboService(group = "demo")
public class DemoServiceImpl implements DemoService {
...
}
@DubboService(group = "demo2")
public class Demo2ServiceImpl implements DemoService {
...
}
Start the Dubbo service, and you can see services with the same service name and different groups in the registry. Taking Nacos as the registry as an example, the following content is displayed:
Use @DubboReference annotation to add group parameter
@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;
Reference Example 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;
After starting the Dubbo service, you can see the references of the same service name in different groups in the registration center. Taking Nacos as the registration center as an example, the following content is displayed:
Use <dubbo:service /> tag, add group parameter
<?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="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://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>
Start the Dubbo service, and you can see services with the same service name and different groups in the registry. Taking Nacos as the registry as an example, the following content is displayed:
Use dubbo:reference/ annotation to add group parameter
<?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="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://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>
After starting the Dubbo service, you can see the references of the same service name in different groups in the registration center. Taking Nacos as the registration center as an example, the following content is displayed:
Use org.apache.dubbo.config.ServiceConfig class, add group parameter
// 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");
...
Start the Dubbo service, and you can see services with the same service name and different groups in the registry. Taking Nacos as the registry as an example, the following content is displayed:
Use org.apache.dubbo.config.ReferenceConfig, add group parameter
// 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("*");
...
After starting the Dubbo service, you can see the references of the same service name in different groups in the registration center. Taking Nacos as the registration center as an example, the following content is displayed:
Always call* only one available group implementation