您好,登录后才能下订单哦!
本文小编为大家详细介绍“基于dubbo分组group怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于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>
读到这里,这篇“基于dubbo分组group怎么实现”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。