基于dubbo分组group的一些总结

 更新时间:2023年03月21日 16:35:21   作者:普通网友  
这篇文章主要介绍了关于dubbo分组group的一些总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

服务分组

1.当一个接口有多种实现时,可用使用group分组。

实现代码如下:

package com.xxx.service;

public interface MyDubboGroupService {

	public String print();
	
}


package com.xxx.service.impl;

import com.xxx.service.MyDubboGroupService;

public class FeebackService implements MyDubboGroupService {

	@Override
	public String print() {
		// TODO Auto-generated method stub
		return "feedback";
	}

}


package com.xxx.service.impl;

import com.xxx.service.MyDubboGroupService;

public class CmsService implements MyDubboGroupService {

	@Override
	public String print() {
		// TODO Auto-generated method stub
		return "cms";
	}

}

applicationContext.xml 配置

<?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://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 配置Bean -->
	<bean id="feebackService" class="com.xxx.service.impl.FeebackService" />
	<bean id="cmsService" class="com.xxx.service.impl.CmsService" />
	
	<!-- 引入配置文件 -->
	<import resource="classpath:dubbo.xml" />
	
</beans>

dubbo.xml 配置

<?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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
    http://www.springframework.org/schema/beans/spring-beans.xsd        
    http://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 指定服务名字 -->
    <dubbo:application name="dubboGroup" />

    <!-- 声明服务注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

    <!-- 暴露你的服务地址 -->
    <dubbo:service interface="com.xxx.service.MyDubboGroupService" group="feedback" />
    <dubbo:service interface="com.xxx.service.MyDubboGroupService" group="cms" />

</beans>

调用端dubbo.xml 配置

<?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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
    http://www.springframework.org/schema/beans/spring-beans.xsd        
    http://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 指定web服务名字 -->
    <dubbo:application name="dubboGroup" />
    
    <!-- 声明服务注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />

    <!-- 暴露你的服务地址 -->
    <dubbo:reference id="feebackService" interface="com.xxx.service.MyDubboGroupService" group="feedback" />
    <dubbo:reference id="cmsService" interface="com.xxx.service.MyDubboGroupService" group="cms" />
    <!-- 任意组 -->
    <dubbo:reference id="autoService" interface="com.xxx.service.MyDubboGroupService" group="*" />
    
</beans>

调用代码如下:

package com.xxx.application;

import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xxx.service.MyDubboGroupService;

public class DubboApplication {
    
    public static void main(String[] args) throws IOException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 访问feedback接口
        MyDubboGroupService feebackService = (MyDubboGroupService) ctx.getBean("feebackService");
        System.out.println(feebackService.print());
        // 访问member接口
        MyDubboGroupService cmsService = (MyDubboGroupService) ctx.getBean("cmsService");
        System.out.println(cmsService.print());
        // 访问随机接口
        MyDubboGroupService autoService = (MyDubboGroupService) ctx.getBean("autoService");
        System.out.println(autoService.print());
    }
    
}

2.上面调用端dubbo.xml 配置出现的任意组:(2.2.0以上版本支持,总是只调一个可用组的实现)

<dubbo:reference id="autoService" interface="com.xxx.service.MyDubboGroupService" group="*" />

分组聚合

按组合并返回结果,比如菜单服务,接口一样,但有多种实现,用group区分,现在消费方需从每种group中调用一次返回结果,合并结果返回,这样就可以实现聚合菜单项。(从2.1.0版本开始支持)

配置如:(搜索所有分组)

<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" />

或:(合并指定分组)

<dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" />

或:(指定方法合并结果,其他未指定的方法,将只调用一个Group)

<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="true"/>
</dubbo:reference>

或:(某个方法不合并结果,其他都合并结果)

<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true">
    <dubbo:method name="getMenuItems" merger="false"/>
</dubbo:reference>

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

<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger="mymerge"/>
</dubbo:reference>

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

<dubbo:reference interface="com.xxx.MenuService" group="*">
    <dubbo:method name="getMenuItems" merger=".addAll"/>
</dubbo:reference>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Maven搭建springboot项目的方法步骤

    Maven搭建springboot项目的方法步骤

    这篇文章主要介绍了Maven搭建springboot项目的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • java的内部类和外部类用法讲解

    java的内部类和外部类用法讲解

    本文详细讲解了java的内部类和外部类用法,文中通过示例代码介绍的非常详细。对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • Java实现MD5消息摘要算法

    Java实现MD5消息摘要算法

    本篇文章主要介绍了Java实现MD5消息摘要算法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • springboot配置文件中使用${}注入值的两种方式小结

    springboot配置文件中使用${}注入值的两种方式小结

    这篇文章主要介绍了springboot配置文件中使用${}注入值的两种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java中的WeakHashMap详解

    Java中的WeakHashMap详解

    这篇文章主要介绍了Java中的WeakHashMap详解,WeakHashMap可能平时使用的频率并不高,但是你可能听过WeakHashMap会进行自动回收吧,下面就对其原理进行分析,需要的朋友可以参考下
    2023-09-09
  • 如何使用IDEA2022.1 创建Spring Boot项目

    如何使用IDEA2022.1 创建Spring Boot项目

    这篇文章主要介绍了如何使用IDEA2022.1 创建Spring Boot项目,大家在使用idea开发工具时发现给以往的版本略微的不同,细心的小编在此记录下,需要的朋友可以参考下
    2022-08-08
  • 多线程_解决Runnable接口无start()方法的情况

    多线程_解决Runnable接口无start()方法的情况

    这篇文章主要介绍了多线程_解决Runnable接口无start()方法的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • java的jps命令使用详解

    java的jps命令使用详解

    这篇文章介绍了java的jps命令使用详解,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • Java Scanner类用法及nextLine()产生的换行符问题实例分析

    Java Scanner类用法及nextLine()产生的换行符问题实例分析

    这篇文章主要介绍了Java Scanner类用法及nextLine()产生的换行符问题,结合实例形式分析了Scanner类功能、hasNextInt()和nextInt()方法使用及nextLine()产生的换行符问题解决方法,需要的朋友可以参考下
    2019-03-03
  • Java OpenCV利用KNN算法实现图像背景移除

    Java OpenCV利用KNN算法实现图像背景移除

    这篇文章主要为大家介绍了Java OpenCV利用K最邻近(KNN,K-NearestNeighbor)分类算法实现图像背景移除的示例代码,需要的可以参考一下
    2022-01-01

最新评论