Apache的骆驼汇聚多个REST服务响应 [英] Apache camel to aggregate multiple REST service responses

查看:357
本文介绍了Apache的骆驼汇聚多个REST服务响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的骆驼,不知道我怎么能骆驼使用实现下面提到的使用情况,

我们有一个REST Web服务,可以说,它有两个服务操作马蹄莲和callB。
现在我们在打这个实际的Web服务的URL拦截之前的客户端请求前ESB层。

现在我想要做这样的事情 -
揭露ESB的URL客户机将实际调用。在ESB中,我们使用的是骆驼的码头组件,它只是代理这个服务调用。因此,可以说这个网址时/我的服务/扫描/ ​​

现在在收到该请求@ESB,我想打电话给这两个REST端点(水芋和callB) - >获取他们的反应 - 萨和RESB - >它汇总到一个响应对象resScan - >返回给客户端。

所有我现在是 -

 <路线ID =MyServiceScanRoute>
<从uri=\"jetty:http://{host}.{port}./my-service/scan/?matchOnUri$p$pfix=true&bridgeEndpoint=true\"/>
<! - 设置服务特定的头,监控等 - >
<! - 呼叫performScan - >
<到URI =直接:performScan/>
< /路由><路线ID =SubRoute_performScan>
<从URI =直接:performScan/>
<! - 我该怎么办?
让马蹄莲,callB服务电话。
得到他们的答复RESA,RESB。
聚合这些反应resScan
  - >
< /路由>


解决方案

我觉得你不必要的复杂解决一点点。 :)在我的愚见最好的方式来调用两个 independed 远程Web服务和连接结果是:


  • 通话服务的并行使用组播

  • 使用汇总结果的<一个href=\"http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/aggregate/GroupedExchangeAggregationStrategy.html\">GroupedExchangeAggregationStrategy

为解决路由上面可能看起来像:

 从(直接:serviceFacade)
  .multicast(新GroupedExchangeAggregationStrategy())。parallelProcessing()
    .enrich(http://google.com?q=Foo).enrich(http://google.com?q=Bar)
  。结束();

交换传递到直接:serviceFacadeResponse 将包含属性 Exchange.GROUPED_EXCHANGE 设置为来电的结果列表你的服务(谷歌搜索在我的例子)。

这就是你怎么还接线直接:serviceFacade 来码头终点:

<$p$p><$c$c>from(\"jetty:http://0.0.0.0:8080/myapp/myComplexService\").enrich(\"direct:serviceFacade\").setBody(property(Exchange.GROUPED_EXCHANGE));

现在所有的HTTP请求被你使用码头组件将产生由两个通话的子服务串联反应ESB公开服务网址。

有关信息和端点的动态部分进一步考虑

在终端中使用静态URL很多情况下是不足以达到你所需要的。你也可以将它传递给每个Web服务之前需要prepare有效载荷。

一般而言 - 用于实现在很大程度上取决于你用​​它来使用Web服务(在组件上动态终结点或有效载荷参数路由类型的 HTTP CXFRS ,的的Restlet 时,RSS等)。每个组件的程度并在其中您可以动态配置的方式各不相同。

如果您的端点/载荷应动态地影响你也可以考虑以下选项:

交换的 preprocess副本传递给每个端点使用<$ C $ç>上prepareRef组播终点选项。你可以用它来指代定制处理器,将它传递给组播的端点之前修改的有效载荷。这可能是撰写与HTTP组件的Exchange.HTTP_URI头prepareRef好办法。

使用收件人列表 (也提供 parallelProcessing 作为组播一样)动态创建REST端点的URL。

使用分配器模式(用 parallelProcessing 启用)分割请求转换成专用于每个服务较小的消息。再次此选项可以工作pretty以及与 Exchange.HTTP_URI HTTP组件的头。这将只工作,如果两个子服务可以使用相同的端点类型来定义

正如你所看到的骆驼是pretty灵活,为您提供实现在许多方面你的目标。考虑你的问题的情况下,并选择适合您的最佳解决方案。

如果你告诉我REST URL的更具体的例子,你想在每个请求聚合服务叫我可以建议你我会选择哪种方案,以及如何实现它。的特别重要的是要知道该请求的一部分是动态的。我还需要知道你想使用(这将取决于您将收到来自服务的数据的类型)。

该服务消费者

I m new to Camel and wondering how I can implement below mentioned use case using Camel,

We have a REST web service and lets say it has two service operations callA and callB. Now we have ESB layer in the front that intercepts the client requests before hitting this actual web service URLs.

Now I m trying to do something like this - Expose a URL in ESB that client will actually call. In the ESB we are using Camel's Jetty component which just proxies this service call. So lets say this URL be /my-service/scan/

Now on receiving this request @ESB, I want to call these two REST endpoints (callA and callB) -> Get their responses - resA and resB -> Aggregate it to a single response object resScan -> return to the client.

All I have right now is -

<route id="MyServiceScanRoute">
<from uri="jetty:http://{host}.{port}./my-service/scan/?matchOnUriPrefix=true&amp;bridgeEndpoint=true"/>
<!-- Set service specific headers, monitoring etc. -->  
<!-- Call performScan -->
<to uri="direct:performScan"/>
</route>

<route id="SubRoute_performScan">
<from uri="direct:performScan"/>
<!--  HOW DO I??
Make callA, callB service calls. 
Get their responses resA, resB.
Aggregate these responses to resScan
 -->
</route>

解决方案

I think that you unnecessarily complicate the solution a little bit. :) In my humble opinion the best way to call two independed remote web services and concatenate the results is to:

The routing for the solution above may look like:

from("direct:serviceFacade")
  .multicast(new GroupedExchangeAggregationStrategy()).parallelProcessing()
    .enrich("http://google.com?q=Foo").enrich("http://google.com?q=Bar")
  .end();

Exchange passed to the direct:serviceFacadeResponse will contain property Exchange.GROUPED_EXCHANGE set to list of results of calls to your services (Google Search in my example).

And that's how could you wire the direct:serviceFacade to Jetty endpoint:

from("jetty:http://0.0.0.0:8080/myapp/myComplexService").enrich("direct:serviceFacade").setBody(property(Exchange.GROUPED_EXCHANGE));

Now all HTTP requests to the service URL exposed by you on ESB using Jetty component will generate responses concatenated from the two calls to the subservices.

Further considerations regarding the dynamic part of messages and endpoints

In many cases using static URL in endpoints is insufficient to achieve what you need. You may also need to prepare payload before passing it to each web service.

Generally speaking - the type of routing used to achieve dynamic endpoints or payloads parameters in highly dependent on the component you use to consume web services (HTTP, CXFRS, Restlet, RSS, etc). Each component varies in the degree and a way in which you can configure it dynamically.

If your endpoints/payloads should be affected dynamically you could also consider the following options:

Preprocess copy of exchange passed to each endpoint using the onPrepareRef option of the Multicast endpoint. You can use it to refer to the custom processor that will modify the payload before passing it to the Multicast's endpoints. This may be good way to compose onPrepareRef with Exchange.HTTP_URI header of HTTP component.

Use Recipient List (which also offers parallelProcessing as the Multicast does) to dynamically create the REST endpoints URLs.

Use Splitter pattern (with parallelProcessing enabled) to split the request into smaller messages dedicated to each service. Once again this option could work pretty well with Exchange.HTTP_URI header of HTTP component. This will work only if both sub-services can be defined using the same endpoint type.

As you can see Camel is pretty flexible and offers you to achieve your goal in many ways. Consider the context of your problem and choose the solution that fits you the best.

If you show me more concrete examples of REST URLs you want to call on each request to the aggregation service I could advice you which solution I will choose and how to implement it. The particularly important is to know which part of the request is dynamic. I also need to know which service consumer you want to use (it will depend on the type of data you will receive from the services).

这篇关于Apache的骆驼汇聚多个REST服务响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆