【Dubbo3高级特性】「框架与服务」 Nacos作为注册中心-服务分组及服务分组聚合实现

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

分组聚合主要时根据定义在类上面以及在方法上进行相关的调用处理,通过分组对结果进行聚合并返回聚合后的结果。
通过分组对结果进行聚合并返回聚合后的结果,比如菜单服务,用 group 区分同一接口的多种实现,现在消费方需从每种group中调用一次并返回结果,对结果进行合并之后返回,这样就可以实现聚合菜单项。

NACOS注册中心

properties文件

properties 复制代码
dubbo.application.name=nacos-registry-demo-consumer
dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos

xml文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~   Licensed to the Apache Software Foundation (ASF) under one or more
  ~   contributor license agreements.  See the NOTICE file distributed with
  ~   this work for additional information regarding copyright ownership.
  ~   The ASF licenses this file to You under the Apache License, Version 2.0
  ~   (the "License"); you may not use this file except in compliance with
  ~   the License.  You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~   Unless required by applicable law or agreed to in writing, software
  ~   distributed under the License is distributed on an "AS IS" BASIS,
  ~   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~   See the License for the specific language governing permissions and
  ~   limitations under the License.
  ~
  -->

<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
    <dubbo:application name="nacos-registry-demo-consumer/>
    <dubbo:registry address="nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos"/>
</beans>

yaml文件

yaml 复制代码
dubbo:
  application:
  registry:
    address: nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos

服务分组

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

特性说明

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

使用场景

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

实战案例

基础引入Maven依赖

xml 复制代码
	
	<properties>
        <source.level>1.8</source.level>
        <target.level>1.8</target.level>
        <dubbo.version>3.0.2.1</dubbo.version>
        <spring.version>4.3.16.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
        <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <profiles>
        <!-- For jdk 11 above JavaEE annotation -->
        <profile>
            <id>javax.annotation</id>
            <activation>
                <jdk>[1.11,)</jdk>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>javax.annotation</groupId>
                    <artifactId>javax.annotation-api</artifactId>
                    <version>1.3.2</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${source.level}</source>
                    <target>${target.level}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

定义基础接口

java 复制代码
public interface GroupProcessServce {
    String execute(String parameter);
}

服务提供者annotation模式

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

实现实现类-Group1
java 复制代码
@DubboService(version = "1.0.0",group="g1")
public class Group1ExecuteProcess implements GroupProcessServce {
    @Override
    public String execute(String parameter) {
	    // TODO 实现处理
    }
}

实现实现类-Group2

java 复制代码
@DubboService(version = "1.0.0",group="g2")
public class Group2ExecuteProcess implements GroupProcessServce {
    @Override
    public String execute(String parameter) {
	    // TODO 实现处理
    }
}

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

使用 <dubbo:service /> 标签,添加 group 参数

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~   Licensed to the Apache Software Foundation (ASF) under one or more
  ~   contributor license agreements.  See the NOTICE file distributed with
  ~   this work for additional information regarding copyright ownership.
  ~   The ASF licenses this file to You under the Apache License, Version 2.0
  ~   (the "License"); you may not use this file except in compliance with
  ~   the License.  You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~   Unless required by applicable law or agreed to in writing, software
  ~   distributed under the License is distributed on an "AS IS" BASIS,
  ~   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~   See the License for the specific language governing permissions and
  ~   limitations under the License.
  ~
  -->

<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>

服务消费者annotation模式

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

java 复制代码
    @DubboReference(group="g1")
    private GroupProcessService groupProcessServiceA;

	@DubboReference(group="g1")
    private GroupProcessService groupProcessServiceB;

	//group值为*,标识匹配任意服务分组
	@DubboReference(group = "*")
	private GroupProcessService groupProcessService;

服务消费者xml模式

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

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~   Licensed to the Apache Software Foundation (ASF) under one or more
  ~   contributor license agreements.  See the NOTICE file distributed with
  ~   this work for additional information regarding copyright ownership.
  ~   The ASF licenses this file to You under the Apache License, Version 2.0
  ~   (the "License"); you may not use this file except in compliance with
  ~   the License.  You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~   Unless required by applicable law or agreed to in writing, software
  ~   distributed under the License is distributed on an "AS IS" BASIS,
  ~   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~   See the License for the specific language governing permissions and
  ~   limitations under the License.
  ~
  -->

<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>

服务提供端( API 配置)

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

java 复制代码
// 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");
...

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

服务消费端( API 配置)

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

java 复制代码
// 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("*");

启动 Dubbo 服务

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

服务提供者
服务消费者

分组聚合

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

特性说明

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

使用场景

服务分组和多版本

使用方式
搜索所有分组
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<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>
合并指定分组
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<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>
指定方法合并

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

xml 复制代码
<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="true" />
</dubbo:reference>
某个方法不合并

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

xml 复制代码
<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true">
    <dubbo:method name="getMenuItems" merger="false" />
</dubbo:reference>
指定合并策略

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

xml 复制代码
<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="mymerge" />
</dubbo:reference>
指定合并方法

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

xml 复制代码
<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger=".addAll" />
</dubbo:reference>
相关推荐
IT_陈寒8 分钟前
Vue3性能优化实战:7个被低估的Composition API技巧让渲染提速40%
前端·人工智能·后端
superman超哥37 分钟前
仓颉编译器优化揭秘:尾递归优化的原理与实践艺术
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·尾递归·仓颉编译器
自由生长202437 分钟前
保障缓存和数据库尽量一致的策略
后端
海南java第二人1 小时前
Spring Bean作用域深度解析:从单例到自定义作用域的全面指南
java·后端·spring
悟空码字1 小时前
SpringBoot 整合 Nacos,让微服务像外卖点单一样简单
java·spring boot·后端
橘子131 小时前
C++多态
后端
golang学习记1 小时前
🔥 Go Gin 不停机重启指南:让服务在“洗澡搓背”中无缝升级
后端
MX_93592 小时前
Spring的命名空间
java·后端·spring
moyueheng2 小时前
Python 工具生态深度解析:从 Pyright 到 Astral 家族
后端
逛街的猫啊2 小时前
【AI 专栏】JSON-RPC
ai·rpc·json