您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“基于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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。