关于dubbo分组group的一些总结

  • Post author:
  • Post category:其他




服务分组

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>



版权声明:本文为weixin_43868594原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。