基于dubbo分组group的一些总结
目录
- 服务分组
- 分组聚合
- 总结
服务分组
1.当一个接口有多种实现时,可用使用group分组。
实现代码如下:
package com.xxx.service; public interface MyDubb开发者_Go培训oGroupService { 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" xshttp://www.devze.comi: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.pri编程nt()); // 访问随机接口 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版本开始支持)
配置如:(搜索所有分组)
<dubandroidbo: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>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。
精彩评论