关于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 retu "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 retu "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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://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>
作者:ruebass_nevil
来源链接:https://blog.csdn.net/weixin_43868594/article/details/84956886
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。