【问题标题】:Can Camel be used as sterling RESTful webservice client?Camel 可以用作标准的 RESTful Web 服务客户端吗?
【发布时间】:2013-08-01 22:20:42
【问题描述】:

假设我想利用 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 一起正常工作吗?

【问题讨论】:

    标签: rest apache-camel


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:
      1. 从 Bean -> 编组到 Json -> 到带有静态 URI 的 Ahc -> 从 Json -> 解组到 Bean。
      2. 从 Bean -> 编组到 Json -> 到 具有动态 URI 的 Ahc -> 从 Json 解组 -> 到 Bean。

      CAMEL 方法 to() 和 recipientList() 之间的区别在于 to-method 无法解析 camel 的动态参数,而 recipientList-method 可以。

      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");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-10
        • 2012-08-07
        • 1970-01-01
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 2014-09-04
        • 2016-09-06
        相关资源
        最近更新 更多