Dubbo: 一次调用,如何获取所有提供方的返回结果?

Created on 19 Nov 2020  ·  13Comments  ·  Source: apache/dubbo

比如我写了一个HelloService

public interface HelloService {
    String hello();
}

有英文和中文两种实现:

public class ChineseHelloImpl implements HelloService {
    @Override
    public String hello() {
        return "你好";
    }
}

```java
public class EnglishHelloImpl implements HelloService {
@Override
public String hello() {
return "hello";
}
}

我的xml:
```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-4.0.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- dubbo配置  -->
    <dubbo:registry address="${dubbo.registry.address}"/>
    <dubbo:protocol name="dubbo" port="${dubbo.port}" threads="5"/>
    <dubbo:application name="app"/>
    <dubbo:consumer timeout="3000" check="false" cluster="smart" retries="0"/>

    <!-- 接口声明 -->
    <dubbo:reference id="clients" interface="HelloService" version="1.0.0"/>

</beans>

这是controller:

@RestController
public class TestController {

    @Resource
    private List<HelloService> clients;

    @RequestMapping("/hellos")
    public List<String> hellos() {
        List<String> result = new ArrayList<>();
        for (HelloService client : clients) {
            result.add(client.hello());
        }
        return result;
    }
}

怎么配置xml,或者说用什么方法,可以让调用方一次调用就拿到这两种返回结果?

Most helpful comment

dubbo本身可以实现这种merge功能的,参考MergeableClusterInvoker实现。
使用方式:,其中merge方式可以通过实现org.apache.dubbo.rpc.cluster.Merger接口来实现,dubbo内置了基本数据结构的merge方式。

All 13 comments

声明是没问题的,只是想在一次调用中调用所有服务提供方,并拿到返回结果集合。

这种我理解dubbo来做还有些麻烦,估计还是业务自己来实现更合适点

最近也做过类似的需求,定义一个标准接口由各个业务线去实现,通过服务分组区别,并行获取结果

是的是的,就是这种业务场景。问下大佬最后是怎么解决的?

发自我的iPhone

------------------ 原始邮件 ------------------
发件人: LiosWong <[email protected]>
发送时间: 2020年11月20日 13:28
收件人: apache/dubbo <[email protected]>
抄送: dragon-zhang <[email protected]>, Author <[email protected]>
主题: 回复:[apache/dubbo] 一次调用,如何获取所有提供方的返回结果? (#6927)

最近也做过类似的需求,定义一个标准接口由各个业务线去实现,通过服务分组区别,并行获取结果


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

自定义一个Cluster,应该可行。com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker,然后通过SPI载入

是的是的,就是这种业务场景。问下大佬最后是怎么解决的? 发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: LiosWong <[email protected]> 发送时间: 2020年11月20日 13:28 收件人: apache/dubbo <[email protected]> 抄送: dragon-zhang <[email protected]>, Author <[email protected]> 主题: 回复:[apache/dubbo] 一次调用,如何获取所有提供方的返回结果? (#6927) 最近也做过类似的需求,定义一个标准接口由各个业务线去实现,通过服务分组区别,并行获取结果 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

配置:


伪代码:

CompletableFuture[] cfs = bizTypeList.stream()
                    .map(bizType ->
                            CompletableFuture.supplyAsync(
                                    () -> Container.getHandler(bizType). hello(), Executor(线程池)
                            ).whenCompleteAsync(
                                    (r, ex) -> {
                                        // todo 获取线程执行结果
                                    }
                            )
                    ).toArray(CompletableFuture[]::new);
//等待总任务完成
CompletableFuture.allOf(cfs).join();

也是上层通过并行调用获取结果的

笑哭,我这边不知道具体的group叫啥。

发自我的iPhone

------------------ 原始邮件 ------------------
发件人: LiosWong <[email protected]>
发送时间: 2020年11月20日 16:47
收件人: apache/dubbo <[email protected]>
抄送: dragon-zhang <[email protected]>, Author <[email protected]>
主题: 回复:[apache/dubbo] 一次调用,如何获取所有提供方的返回结果? (#6927)

是的是的,就是这种业务场景。问下大佬最后是怎么解决的? 发自我的iPhone

------------------ 原始邮件 ------------------ 发件人: LiosWong <[email protected]> 发送时间: 2020年11月20日 13:28 收件人: apache/dubbo <[email protected]> 抄送: dragon-zhang <[email protected]>, Author <[email protected]> 主题: 回复:[apache/dubbo] 一次调用,如何获取所有提供方的返回结果? (#6927) 最近也做过类似的需求,定义一个标准接口由各个业务线去实现,通过服务分组区别,并行获取结果 — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

配置:
<dubbo:reference id="helloService1" interface="HelloService" version="1.0.0" group="A"/>
<dubbo:reference id="helloService2" interface="HelloService" version="1.0.0" group="B"/>
<dubbo:reference id="helloService3" interface="HelloService" version="1.0.0" group="C"/>

伪代码:
CompletableFuture[] cfs = bizTypeList.stream() .map(bizType -> CompletableFuture.supplyAsync( () -> Container.getHandler(bizType) . hello(), Executor(线程池) ).whenCompleteAsync( (r, ex) -> { // todo 获取结果 } ) ).toArray(CompletableFuture[]::new); //等待总任务完成 CompletableFuture.allOf(cfs).join();
也是上层通过并行调用获取结果的


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

我试下,感谢大佬。

发自我的iPhone

------------------ 原始邮件 ------------------
发件人: 空无 <[email protected]>
发送时间: 2020年11月20日 13:57
收件人: apache/dubbo <[email protected]>
抄送: dragon-zhang <[email protected]>, Author <[email protected]>
主题: 回复:[apache/dubbo] 一次调用,如何获取所有提供方的返回结果? (#6927)

自定义一个Cluster,应该可行。com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker,然后通过SPI载入


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

dubbo本身可以实现这种merge功能的,参考MergeableClusterInvoker实现。
使用方式:,其中merge方式可以通过实现org.apache.dubbo.rpc.cluster.Merger接口来实现,dubbo内置了基本数据结构的merge方式。

最近也做过类似的需求,定义一个标准接口由各个业务线去实现,通过服务分组区别,并行获取结果

这个我记得不能跨group吧?有点忘记了

dubbo本身可以实现这种merge功能的,参考MergeableClusterInvoker实现。
使用方式:,其中merge方式可以通过实现org.apache.dubbo.rpc.cluster.Merger接口来实现,dubbo内置了基本数据结构的merge方式。

才发现dubbo还有这个MergerCluster,拿这个做并行(merge)调用太方便了,赞一个

最近也做过类似的需求,定义一个标准接口由各个业务线去实现,通过服务分组区别,并行获取结果

这个我记得不能跨group吧?有点忘记了

可以的

dubbo本身可以实现这种merge功能的,参考MergeableClusterInvoker实现。
使用方式:,其中merge方式可以通过实现org.apache.dubbo.rpc.cluster.Merger接口来实现,dubbo内置了基本数据结构的merge方式。

感谢大佬,亲测有效。

Was this page helpful?
0 / 5 - 0 ratings