1

假设我想利用 Camel 作为 RESTful Web 服务的客户端。但不确定骆驼是否足以胜任这种工作。我也想使用 http4 或 ahc 组件,而不是 cxf。

一般来说,我只需要两种路线:

  1. 从 Bean -> 编组到 Json -> 到带有静态 URI 的 Ahc -> 从 Json 解组 -> 到 Bean。静态 uri 示例:ahc:http://host/api/user/create
  2. 从 Bean -> 编组到 Json -> 到带有动态 URI 的 Ahc -> 从 Json 解组 -> 到 Bean。动态 uri 示例:ahc:http://host/api/user/id/1

我想有一个服务类以如下方式触发这样的路线:

UserService {

    @Autowired
    protected CamelContext restApiCamelContext;

    public UserCreateResponse createUser (UserModel user) {
        ... Camel's magick which starts create user route ...
    } 

    public UserModel getUserById (Long id) {        
        ... the id must be placed somehow into endpoint uri: http://host:port/api/user/id/${id} ...
        ... Camel's magick which get user by id ...
    }
}

UserService 应该在 Spring MVC 控制器中使用。

那么,有没有可能基于Camel的能力来实现这样一个UserService呢?如果是的话,那么它在大量用户请求进入弹簧控制器的高压下是否能正常工作?它可以与近百种不同的 uri 一起正常工作吗?

4

2 回答 2

0

您可以通过动态设置 CamelHttpUri 的消息头来更改请求 uri。如果您的业务逻辑很简单,我认为您可以创建一个简单的骆驼路线来完成这项工作。然后你使用camel ProducerTemplate 将请求发送到camel 路由。

于 2013-08-05T07:16:34.227 回答
0
  1. 从 Bean -> 编组到 Json -> 到带有静态 URI 的 Ahc -> 从 Json 解组 -> 到 Bean。
  2. 从 Bean -> 编组到 Json -> 到带有动态 URI 的 Ahc -> 从 Json 解组 -> 到 Bean。

CAMEL 方法 to() 和 recipientList() 之间的区别在于 to 方法无法解析骆驼的动态参数,而接收者列表方法可以。

from("restlet:/your/some/address/{sourceId}?restletMethods=GET")
.log("execute '${header.sourceId}' for something").to("log:WebRequestThroughput?groupSize=10")
.beanRef("yourServiceBeanRef", "serviceMethodName")
.marshal().json(JsonLibrary.Jackson)
.to("http://domain/address/?bridgeEndpoint=true&throwExceptionOnFailure=true")
.unmarshal().json(JsonLibrary.Jackson, YourResponseObject.class)
.beanRef("anotherServiceBeanRef", "anotherMethodName");


from("restlet:/your/address/{sourceId}?restletMethods=GET")
.log("execute '${header.sourceId}' for something").to("log:WebRequestThroughput?groupSize=10")
.beanRef("yourServiceBeanRef", "serviceMethodName")
.marshal().json(JsonLibrary.Jackson)
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST))
.recipientList(simple("http://domain/address/${header.sourceId}?bridgeEndpoint=true&throwExceptionOnFailure=true"))
.unmarshal().json(JsonLibrary.Jackson, YourResponseObject.class)
.beanRef("anotherServiceBeanRef", "anotherMethodName");
于 2014-08-07T08:50:09.287 回答