按组合并返回结果,比如菜单服务,接口一样,但有多种实现,用group区分,现在消费方需要从每种group中调用一次返回结果,合并结果返回,这样就可以实现聚合菜单项。
实现代码如下:
- 服务器端实现。
package com.yncp.dubbo.service;
import java.util.List;
import java.util.Map;
public interface IDubboMergeService {
/**
* 测试分组聚合
* @return
*/
public String[] groupArray();
/**
* 测试分组聚合
* @return
*/
public List<String> groupList();
/**
* 测试分组聚合
* @return
*/
public Map<String,Object> groupMap();
}
package com.yncp.dubbo.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yncp.dubbo.service.IDubboMergeService;
public class DubboMergeService1Impl implements IDubboMergeService {
public List<String> groupList() {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("张三");
arrayList.add("李四");
return arrayList;
}
public Map<String, Object> groupMap() {
Map<String,Object> map=new HashMap<String, Object>();
map.put("user1", "张三");
map.put("user2", "李四");
return map;
}
@Override
public String[] groupArray() {
return new String[]{"A","B","C","D"};
}
}
package com.yncp.dubbo.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yncp.dubbo.service.IDubboMergeService;
public class DubboMergeService2Impl implements IDubboMergeService {
public String[] groupArray() {
return new String[]{"A","B","C","D","E","F"};
}
public List<String> groupList() {
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("王五");
arrayList.add("鲁智深");
return arrayList;
}
public Map<String, Object> groupMap() {
Map<String,Object> map=new HashMap<String, Object>();
map.put("user3", "王五");
map.put("user4", "鲁智深");
return map;
}
}
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="mergeService1" class="com.yncp.dubbo.service.impl.DubboMergeService1Impl"/>
<bean id="mergeService2" class="com.yncp.dubbo.service.impl.DubboMergeService2Impl"/>
<!-- 引入配置文件 -->
<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">
<!-- 指定web服务名字 -->
<dubbo:application name="DubboMerge"/>
<!-- 声明服务注册中心 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 指定传输层通信协议 -->
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:protocol name="rmi" port="1099"/>
<!-- 暴露你的服务地址 -->
<dubbo:service
ref="mergeService1"
interface="com.yncp.dubbo.service.IDubboMergeService"
protocol="dubbo,rmi"
group="new"
/>
<dubbo:service
ref="mergeService2"
interface="com.yncp.dubbo.service.IDubboMergeService"
protocol="dubbo,rmi"
group="old"
/>
</beans>
客户端实现:
package com.yncp.dubbo.service;
import java.util.List;
import java.util.Map;
public interface IDubboMergeService {
/**
* 测试分组聚合
* @return
*/
public String[] groupArray();
/**
* 测试分组聚合
* @return
*/
public List<String> groupList();
/**
* 测试分组聚合
* @return
*/
public Map<String,Object> groupMap();
}
package com.yncp.dubbo.service;
import java.io.IOException;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboStart {
public static void main(String[] args) throws IOException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//分组聚合
IDubboMergeService mergeService=(IDubboMergeService) ctx.getBean("mergeService");
for (String string : mergeService.groupArray()) {
System.out.println(string);
}
for (String string : mergeService.groupList()) {
System.out.println(string);
}
for (Map.Entry<String, Object> entry : mergeService.groupMap().entrySet()) {
String key = entry.getKey();
Object value=entry.getValue();
System.out.println(key+" "+value);
}
}
}
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="DubboMerge"/>
<!-- 声明服务注册中心 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 指定传输层通信协议 -->
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:protocol name="rmi" port="1010"/>
<!-- 暴露你的服务地址 -->
<dubbo:reference
id="mergeService"
interface="com.yncp.dubbo.service.IDubboMergeService"
protocol="dubbo" group="*">
<dubbo:method name="groupArray" merger="true"/>
<dubbo:method name="groupList" merger="true"/>
<dubbo:method name="groupMap" merger="true"/>
</dubbo:reference>
</beans>