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

简介: 【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>


相关文章
|
8月前
|
人工智能 Java API
Nacos 3.1.0 正式发布,支持 A2A 注册中心与 MCP 注册协议增强
3.1.0 发布核心全新功能-Agent 注册中心,助力构建基于 A2A 协议的多 Agent 协作的AI应用,同时 MCP 注册中心适配最新 MCP 官方注册中心协议及升级优化多项核心功能。
2181 78
|
负载均衡 Kubernetes 网络协议
注册中心如何选型?Eureka、Zookeeper、Nacos怎么选
这是小卷对分布式系统架构学习的第9篇文章,继续探讨注册中心的原理及选型。文章详细介绍了Eureka、Nacos的工作机制与特点,并对比了Eureka、Nacos、Consul和Zookeeper在一致性协议、健康检查、负载均衡等方面的差异。最后根据不同的应用场景给出了注册中心的选型建议,帮助读者理解如何选择最适合的注册中心。
1414 100
|
SpringCloudAlibaba 负载均衡 Dubbo
【SpringCloud Alibaba系列】Dubbo高级特性篇
本章我们介绍Dubbo的常用高级特性,包括序列化、地址缓存、超时与重试机制、多版本、负载均衡。集群容错、服务降级等。
2077 7
【SpringCloud Alibaba系列】Dubbo高级特性篇
|
存储 缓存 负载均衡
Nacos注册中心
Nacos注册中心
372 1
Nacos注册中心
|
负载均衡 Dubbo NoSQL
Dubbo框架的1个核心设计点
Java领域要说让我最服气的RPC框架当属Dubbo,原因有许多,但是最吸引我的还是它把远程调用这个事情设计得很有艺术。
Dubbo框架的1个核心设计点
|
Dubbo Cloud Native 应用服务中间件
阿里云的 Dubbo 和 Nacos 深度整合,提供了高效的服务注册与发现、配置管理等关键功能,简化了微服务治理,提升了系统的灵活性和可靠性。
在云原生时代,微服务架构成为主流。阿里云的 Dubbo 和 Nacos 深度整合,提供了高效的服务注册与发现、配置管理等关键功能,简化了微服务治理,提升了系统的灵活性和可靠性。示例代码展示了如何在项目中实现两者的整合,通过 Nacos 动态调整服务状态和配置,适应多变的业务需求。
625 2
|
负载均衡 Java Nacos
SpringCloud基础1——远程调用、Eureka,Nacos注册中心、Ribbon负载均衡
微服务介绍、SpringCloud、服务拆分和远程调用、Eureka注册中心、Ribbon负载均衡、Nacos注册中心
SpringCloud基础1——远程调用、Eureka,Nacos注册中心、Ribbon负载均衡
|
Dubbo Java 应用服务中间件
深入探讨了“dubbo+nacos+springboot3的native打包成功后运行出现异常”的原因及解决方案
本文深入探讨了“dubbo+nacos+springboot3的native打包成功后运行出现异常”的原因及解决方案。通过检查GraalVM版本兼容性、配置反射列表、使用代理类、检查配置文件、禁用不支持的功能、查看日志文件、使用GraalVM诊断工具和调整GraalVM配置等步骤,帮助开发者快速定位并解决问题,确保服务的正常运行。
761 1
|
Dubbo 应用服务中间件 Apache
Dubbo 应用切换 ZooKeeper 注册中心实例,流量无损迁移
如果 Dubbo 应用使用 ZooKeeper 作为注册中心,现在需要切换到新的 ZooKeeper 实例,如何做到流量无损?
317 4
|
Dubbo Java 应用服务中间件
Dubbo学习圣经:从入门到精通 Dubbo3.0 + SpringCloud Alibaba 微服务基础框架
尼恩团队的15大技术圣经,旨在帮助开发者系统化、体系化地掌握核心技术,提升技术实力,从而在面试和工作中脱颖而出。本文介绍了如何使用Dubbo3.0与Spring Cloud Gateway进行整合,解决传统Dubbo架构缺乏HTTP入口的问题,实现高性能的微服务网关。