按组合并返回结果,比如菜单服务,接口一样,但有多种实现,用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 groupList();
/**
* 测试分组聚合
* @return
*/
public Map 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 groupList() {
ArrayList arrayList = new ArrayList();
arrayList.add("张三");
arrayList.add("李四");
return arrayList;
}
public Map groupMap() {
Map map=new HashMap();
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 groupList() {
ArrayList arrayList = new ArrayList();
arrayList.add("王五");
arrayList.add("鲁智深");
return arrayList;
}
public Map groupMap() {
Map map=new HashMap();
map.put("user3", "王五");
map.put("user4", "鲁智深");
return map;
}
}
applicationContext.xml 配置:
dubbo.xml配置:
客户端实现:
package com.yncp.dubbo.service;
import java.util.List;
import java.util.Map;
public interface IDubboMergeService {
/**
* 测试分组聚合
* @return
*/
public String[] groupArray();
/**
* 测试分组聚合
* @return
*/
public List groupList();
/**
* 测试分组聚合
* @return
*/
public Map 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 entry : mergeService.groupMap().entrySet()) {
String key = entry.getKey();
Object value=entry.getValue();
System.out.println(key+" "+value);
}
}
}
dubbo.xml配置: