• 【Dubbo3高级特性】「框架与服务」 服务分组及服务分组聚合实现


    Dubbo3的分组聚合能力调用机制

    分组聚合主要时根据定义在类上面以及在方法上进行相关的调用处理,通过分组对结果进行聚合并返回聚合后的结果。

    通过分组对结果进行聚合并返回聚合后的结果,比如菜单服务,用 group 区分同一接口的多种实现,现在消费方需从每种group中调用一次并返回结果,对结果进行合并之后返回,这样就可以实现聚合菜单项。

    NACOS注册中心

    properties文件

    dubbo.application.name=nacos-registry-demo-consumer
    dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
    
    • 1
    • 2

    xml文件

    
    
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubb
        "nacos-registry-demo-consumer/>
        <dubbo:registry address="nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    yaml文件

    dubbo:
      application:
      registry:
        address: nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
    
    • 1
    • 2
    • 3
    • 4

    服务分组

    主要通过使用服务分组区分服务接口的不同实现。

    特性说明

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

    使用场景

    当一个接口有多种实现时,可以用 group 区分。

    实战案例

    基础引入Maven依赖
    	
    	<properties>
            <source.level>1.8source.level>
            <target.level>1.8target.level>
            <dubbo.version>3.0.2.1dubbo.version>
            <spring.version>4.3.16.RELEASEspring.version>
            <junit.version>4.12junit.version>
            <maven-compiler-plugin.version>3.7.0maven-compiler-plugin.version>
        properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframeworkgroupId>
                    <artifactId>spring-framework-bomartifactId>
                    <version>${spring.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>org.apache.dubbogroupId>
                    <artifactId>dubbo-bomartifactId>
                    <version>${dubbo.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>org.apache.dubbogroupId>
                    <artifactId>dubbo-dependencies-zookeeperartifactId>
                    <version>${dubbo.version}version>
                    <type>pomtype>
                dependency>
            dependencies>
        dependencyManagement>
    
    <dependencies>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
            dependency>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-dependencies-zookeeperartifactId>
                <type>pomtype>
            dependency>
    
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
                <scope>testscope>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        <profiles>
            
            <profile>
                <id>javax.annotationid>
                <activation>
                    <jdk>[1.11,)jdk>
                activation>
                <dependencies>
                    <dependency>
                        <groupId>javax.annotationgroupId>
                        <artifactId>javax.annotation-apiartifactId>
                        <version>1.3.2version>
                    dependency>
                dependencies>
            profile>
        profiles>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>${maven-compiler-plugin.version}version>
                    <configuration>
                        <source>${source.level}source>
                        <target>${target.level}target>
                    configuration>
                plugin>
            plugins>
        build>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    定义基础接口
    public interface GroupProcessServce {
        String execute(String parameter);
    }
    
    • 1
    • 2
    • 3
    服务提供者annotation模式

    使用 @DubboService 注解,添加 group 参数

    实现实现类-Group1
    
    @DubboService(version = "1.0.0",group="g1")
    public class Group1ExecuteProcess implements GroupProcessServce {
        @Override
        public String execute(String parameter) {
    	    // TODO 实现处理
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    实现实现类-Group2
    
    @DubboService(version = "1.0.0",group="g2")
    public class Group2ExecuteProcess implements GroupProcessServce {
        @Override
        public String execute(String parameter) {
    	    // TODO 实现处理
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ####服务提供者 xml模式

    使用 标签,添加 group 参数

    
    
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:property-placeholder/>
    
        <dubbo:application name="group-server-provider"/>
    
        <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
    
        <dubbo:provider token="true"/>
    
        <dubbo:protocol name="dubbo" port="20880"/>
    
        <bean id="group1Service" class="org.apache.dubbo.samples.group.impl.Group1ExecuteProcess"/>
    
        <bean id="group2Service" class="org.apache.dubbo.samples.group.impl.Group2ExecuteProcess"/>
    
        <dubbo:service id="groupADubboService" group="g1" interface="org.apache.dubbo.samples.group.api.GroupService"
                       ref="groupAService"/>
    
        <dubbo:service id="groupBDubboService" group="g2" interface="org.apache.dubbo.samples.group.api.GroupService"
                       ref="groupBService"/>
    
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    服务消费者annotation模式

    使用 @DubboReference 注解,添加 group 参数

        @DubboReference(group="g1")
        private GroupProcessService groupProcessServiceA;
    
    	@DubboReference(group="g1")
        private GroupProcessService groupProcessServiceB;
    
    	//group值为*,标识匹配任意服务分组
    	@DubboReference(group = "*")
    	private GroupProcessService groupProcessService;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    服务消费者xml模式

    使用 dubbo:reference/ 注解,添加 group 参数

    
    
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:property-placeholder/>
    
        <dubbo:application name="demo-consumer"/>
    
        <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
    
        <dubbo:reference group="g1" id="groupAService" check="false"
                         interface="org.apache.dubbo.samples.group.api.Group1ExecuteProcess"/>
    
        <dubbo:reference group="g2" id="groupBService" check="false"
                         interface="org.apache.dubbo.samples.group.api.Group1ExecuteProcess"/>
    
    beans>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    服务提供端( API 配置)

    使用 org.apache.dubbo.config.ServiceConfig 类,添加 group 参数

    // ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
    // 请自行缓存,否则可能造成内存和连接泄漏
    ServiceConfig<DemoService> service = new ServiceConfig<>();
    service.setInterface(DemoService.class);
    service.setGroup("demo");
    ...
    
    ServiceConfig<DemoService> service2 = new ServiceConfig<>();
    service.setInterface(DemoService.class);
    service.setGroup("demo2");
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    启动 Dubbo 服务,可在注册中心看到相同服务名不同分组的服务,以 Nacos 作为注册中心为例,显示如下内容:

    服务消费端( API 配置)

    使用 org.apache.dubbo.config.ReferenceConfig,添加 group 参数

    // ReferenceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
    // 请自行缓存,否则可能造成内存和连接泄漏
    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("*");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    启动 Dubbo 服务

    启动 Dubbo 服务,可在注册中心看到相同服务名不同分组的服务,以 Nacos 作为注册中心为例,显示如下内容:

    服务提供者

    服务消费者

    分组聚合

    通过分组对结果进行聚合并返回聚合后的结果

    特性说明

    通过分组对结果进行聚合并返回聚合后的结果,比如菜单服务,用 group 区分同一接口的多种实现,现在消费方需从每种 group 中调用一次并返回结果,对结果进行合并之后返回,这样就可以实现聚合菜单项。

    使用场景

    服务分组和多版本
    使用方式
    搜索所有分组
    
    
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:property-placeholder/>
    
        <dubbo:application name="merge-consumer"/>
    
        <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
    
        <dubbo:reference id="mergeService" interface="org.apache.dubbo.samples.merge.api.MergeService"
                         group="*"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    合并指定分组
    
    
    
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <context:property-placeholder/>
    
        <dubbo:application name="merge-consumer"/>
    
        <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
    
        <dubbo:reference id="mergeService" interface="org.apache.dubbo.samples.merge.api.MergeService"
                         group="a,b"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    指定方法合并

    指定方法合并结果,其它未指定的方法,将只调用一个 Group

    <dubbo:reference interface="com.xxx.MenuService" group="*">
        <dubbo:method name="getMenuItems" merger="true" />
    dubbo:reference>
    
    • 1
    • 2
    • 3
    某个方法不合并

    某个方法不合并结果,其它都合并结果

    <dubbo:reference interface="com.xxx.MenuService" group="*" merger="true">
        <dubbo:method name="getMenuItems" merger="false" />
    dubbo:reference>
    
    • 1
    • 2
    • 3
    指定合并策略

    指定合并策略,缺省根据返回值类型自动匹配,如果同一类型有两个合并器时,需指定合并器的名称 合并结果扩展

    <dubbo:reference interface="com.xxx.MenuService" group="*">
        <dubbo:method name="getMenuItems" merger="mymerge" />
    dubbo:reference>
    
    • 1
    • 2
    • 3
    指定合并方法

    指定合并方法,将调用返回结果的指定方法进行合并,合并方法的参数类型必须是返回结果类型本身

    <dubbo:reference interface="com.xxx.MenuService" group="*">
        <dubbo:method name="getMenuItems" merger=".addAll" />
    dubbo:reference>
    
    • 1
    • 2
    • 3
  • 相关阅读:
    zk 系四大 L2 协议大 PK:进度、异同和生态
    vue使用smooth-signature实现移动端电子签字,包括横竖屏
    运动想象 (MI) 迁移学习系列 (9) : 数据对齐(EA)
    基于 Delphi 的前后端分离:之一
    并发:线程状态
    leetcode 42.接雨水,leetcode 503. 下一个更大元素Ⅱ
    高速花炮筒纸筒剪切机分切机设计(说明书+CAD图纸)
    【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
    Java集合对象拷贝,使用JDK 8 的函数式接口封装org.springframework.beans.BeanUtils工具类实现
    HTTP实现断点续传
  • 原文地址:https://blog.csdn.net/l569590478/article/details/127642460