基于dubbo分組group的一些總結(jié)
服務(wù)分組
1.當(dāng)一個(gè)接口有多種實(shí)現(xiàn)時(shí),可用使用group分組。
實(shí)現(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ù)注冊(cè)中心 --> ?? ?<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ù)注冊(cè)中心 --> ?? ?<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");
?? ??? ?// 訪問(wèn)feedback接口
?? ??? ?MyDubboGroupService feebackService = (MyDubboGroupService) ctx.getBean("feebackService");
?? ??? ?System.out.println(feebackService.print());
?? ??? ?// 訪問(wèn)member接口
?? ??? ?MyDubboGroupService cmsService = (MyDubboGroupService) ctx.getBean("cmsService");
?? ??? ?System.out.println(cmsService.print());
?? ??? ?// 訪問(wèn)隨機(jī)接口
?? ??? ?MyDubboGroupService autoService = (MyDubboGroupService) ctx.getBean("autoService");
?? ??? ?System.out.println(autoService.print());
?? ?}
?? ?
}2.上面調(diào)用端dubbo.xml 配置出現(xiàn)的任意組:(2.2.0以上版本支持,總是只調(diào)一個(gè)可用組的實(shí)現(xiàn))
<dubbo:reference id="autoService" interface="com.xxx.service.MyDubboGroupService" group="*" />
分組聚合
按組合并返回結(jié)果,比如菜單服務(wù),接口一樣,但有多種實(shí)現(xiàn),用group區(qū)分,現(xiàn)在消費(fèi)方需從每種group中調(diào)用一次返回結(jié)果,合并結(jié)果返回,這樣就可以實(shí)現(xiàn)聚合菜單項(xiàng)。(從2.1.0版本開(kāi)始支持)

配置如:(搜索所有分組)
<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true" />
或:(合并指定分組)
<dubbo:reference interface="com.xxx.MenuService" group="aaa,bbb" merger="true" />
或:(指定方法合并結(jié)果,其他未指定的方法,將只調(diào)用一個(gè)Group)
<dubbo:reference interface="com.xxx.MenuService" group="*">
<dubbo:method name="getMenuItems" merger="true"/>
</dubbo:reference>
或:(某個(gè)方法不合并結(jié)果,其他都合并結(jié)果)
<dubbo:reference interface="com.xxx.MenuService" group="*" merger="true">
<dubbo:method name="getMenuItems" merger="false"/>
</dubbo:reference>
或:(指定合并策略,缺省根據(jù)返回值類(lèi)型自動(dòng)匹配,如果同一類(lèi)型有兩個(gè)合并器時(shí),需指定合并器的名稱(chēng))
<dubbo:reference interface="com.xxx.MenuService" group="*">
<dubbo:method name="getMenuItems" merger="mymerge"/>
</dubbo:reference>
或:(指定合并方法,將調(diào)用返回結(jié)果的指定方法進(jìn)行合并,合并方法的參數(shù)類(lèi)型必須是返回結(jié)果類(lèi)型本身)
<dubbo:reference interface="com.xxx.MenuService" group="*">
<dubbo:method name="getMenuItems" merger=".addAll"/>
</dubbo:reference>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven搭建springboot項(xiàng)目的方法步驟
這篇文章主要介紹了Maven搭建springboot項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
java的內(nèi)部類(lèi)和外部類(lèi)用法講解
本文詳細(xì)講解了java的內(nèi)部類(lèi)和外部類(lèi)用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
springboot配置文件中使用${}注入值的兩種方式小結(jié)
這篇文章主要介紹了springboot配置文件中使用${}注入值的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
如何使用IDEA2022.1?創(chuàng)建Spring?Boot項(xiàng)目
這篇文章主要介紹了如何使用IDEA2022.1?創(chuàng)建Spring?Boot項(xiàng)目,大家在使用idea開(kāi)發(fā)工具時(shí)發(fā)現(xiàn)給以往的版本略微的不同,細(xì)心的小編在此記錄下,需要的朋友可以參考下2022-08-08
多線程_解決Runnable接口無(wú)start()方法的情況
這篇文章主要介紹了多線程_解決Runnable接口無(wú)start()方法的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Java Scanner類(lèi)用法及nextLine()產(chǎn)生的換行符問(wèn)題實(shí)例分析
這篇文章主要介紹了Java Scanner類(lèi)用法及nextLine()產(chǎn)生的換行符問(wèn)題,結(jié)合實(shí)例形式分析了Scanner類(lèi)功能、hasNextInt()和nextInt()方法使用及nextLine()產(chǎn)生的換行符問(wèn)題解決方法,需要的朋友可以參考下2019-03-03
Java OpenCV利用KNN算法實(shí)現(xiàn)圖像背景移除
這篇文章主要為大家介紹了Java OpenCV利用K最鄰近(KNN,K-NearestNeighbor)分類(lèi)算法實(shí)現(xiàn)圖像背景移除的示例代碼,需要的可以參考一下2022-01-01

