基于dubbo分組group的一些總結(jié)
服務(wù)分組
1.當(dāng)一個接口有多種實現(xiàn)時,可用使用group分組。
實現(xiàn)代碼如下:
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"> ?? ?<!-- 指定服務(wù)名字 --> ?? ?<dubbo:application name="dubboGroup" /> ?? ?<!-- 聲明服務(wù)注冊中心 --> ?? ?<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> ?? ?<!-- 暴露你的服務(wù)地址 --> ?? ?<dubbo:service interface="com.xxx.service.MyDubboGroupService" group="feedback" /> ?? ?<dubbo:service interface="com.xxx.service.MyDubboGroupService" group="cms" /> </beans>
調(diào)用端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服務(wù)名字 --> ?? ?<dubbo:application name="dubboGroup" /> ?? ? ?? ?<!-- 聲明服務(wù)注冊中心 --> ?? ?<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> ?? ?<!-- 暴露你的服務(wù)地址 --> ?? ?<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>
調(diào)用代碼如下:
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()); ?? ??? ?// 訪問隨機(jī)接口 ?? ??? ?MyDubboGroupService autoService = (MyDubboGroupService) ctx.getBean("autoService"); ?? ??? ?System.out.println(autoService.print()); ?? ?} ?? ? }
2.上面調(diào)用端dubbo.xml 配置出現(xiàn)的任意組:(2.2.0以上版本支持,總是只調(diào)一個可用組的實現(xiàn))
<dubbo:reference id="autoService" interface="com.xxx.service.MyDubboGroupService" group="*" />
分組聚合
按組合并返回結(jié)果,比如菜單服務(wù),接口一樣,但有多種實現(xiàn),用group區(qū)分,現(xiàn)在消費方需從每種group中調(diào)用一次返回結(jié)果,合并結(jié)果返回,這樣就可以實現(xiàn)聚合菜單項。(從2.1.0版本開始支持)
配置如:(搜索所有分組)
<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" />
或:(合并指定分組)
<dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" />
或:(指定方法合并結(jié)果,其他未指定的方法,將只調(diào)用一個Group)
<dubbo:reference interface="com.xxx.MenuService" group="*"> <dubbo:method name="getMenuItems" merger="true"/> </dubbo:reference>
或:(某個方法不合并結(jié)果,其他都合并結(jié)果)
<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true"> <dubbo:method name="getMenuItems" merger="false"/> </dubbo:reference>
或:(指定合并策略,缺省根據(jù)返回值類型自動匹配,如果同一類型有兩個合并器時,需指定合并器的名稱)
<dubbo:reference interface="com.xxx.MenuService" group="*"> <dubbo:method name="getMenuItems" merger="mymerge"/> </dubbo:reference>
或:(指定合并方法,將調(diào)用返回結(jié)果的指定方法進(jìn)行合并,合并方法的參數(shù)類型必須是返回結(jié)果類型本身)
<dubbo:reference interface="com.xxx.MenuService" group="*"> <dubbo:method name="getMenuItems" merger=".addAll"/> </dubbo:reference>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java內(nèi)存結(jié)構(gòu)和數(shù)據(jù)類型
本文重點給大家介紹java內(nèi)存結(jié)構(gòu)和數(shù)據(jù)類型知識,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-12-12Spring boot 應(yīng)用實現(xiàn)動態(tài)刷新配置詳解
這篇文章主要介紹了spring boot 配置動態(tài)刷新實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-09-09Java實現(xiàn)一鍵獲取Mysql所有表字段設(shè)計和建表語句的工具類
這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫一個工具類,可以實現(xiàn)一鍵獲取Mysql所有表字段設(shè)計和建表語句,感興趣的小伙伴可以了解一下2023-05-05解決SSLContext.getInstance()中參數(shù)設(shè)置TLS版本無效的問題
這篇文章主要介紹了解決SSLContext.getInstance()中參數(shù)設(shè)置TLS版本無效的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01