【问题标题】:Processing response of external web service with Apache Camel使用 Apache Camel 处理外部 Web 服务的响应
【发布时间】:2016-07-13 10:17:22
【问题描述】:

我正在考虑两个小型 Spring Boot 应用程序:

  • Applcation 1:在http://localhost:8081 上运行的小型 Web 服务,实现了一个简单的 Spring 控制器来响应 /camel 上的 GET 请求。该服务在访问http://localhost:8081/camel 时只返回“Hello world”。
  • 应用程序 2:一个小型应用程序,应该向应用程序 1 执行 GET 请求并将响应打印到控制台(在本例中为“Hello world”)。

使用 Springs RestTemplate,我可以在应用程序 2 中轻松实现这个 GET 请求:

RestTemplate template = new RestTemplate();
String response = template.getForBody("http://localhost:8081/camel", String.class);
System.out.println(response);

我现在的目标是使用 Apache Camel 而不是 Spring 的 RestTemplate。 我尝试定义以下 RouteBuilder:

public class MyRoutes extends RouteBuilder {
    @Override
    public void configure() throws Exception() {
        from("jetty:http://localhost:8081/camel").to("direct:processRest");

        from("direct:processRest").process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                System.out.println(exchange.getIn().getBody());
            }
        });
    }
}

但是,当我运行应用程序时,我收到以下错误:

org.apache.camel.spring.boot.CamelSpringBootInitializationException: java.net.BindException: Address already in use: bind

Spring Boot 或 Apache Camel 会自动尝试在端口 8081 上启动 Jetty 服务器,但其他 Web 服务(应用程序 1)已在此端口上运行。

有人知道如何避免这个问题吗?

【问题讨论】:

    标签: web-services spring-boot apache-camel


    【解决方案1】:

    您遇到的问题是因为您使用 Camel 的 Jetty 组件作为消费者,这会启动嵌入式 Jetty 服务器并将其绑定到端口 8081,从而与您的 Spring Boot 应用程序冲突。要发送 HTTP 请求,您应该使用 Jetty 组件作为生产者,即将您的路由定义更改为类似于

    from("scheduler:camelTest?delay=1000").to("jetty:http://localhost:8081/camel").to("direct:processRest");

    【讨论】:

    • 这个我已经明白了 :) 但我不明白如何让码头在端口 8282 上运行,但在路由规范中使用端口 8281。无论我尝试什么,Jetty 总是在路由中使用的端口上启动....
    • 好吧,由于您在应用程序 2 的路由声明中使用 from(),因此您将启动嵌入式 Jetty 并监听 /camel 路径上的请求
    • 哦,现在我明白了 - 抱歉,我似乎误解了你的问题。我会马上更新我的答案。
    猜你喜欢
    • 2015-03-30
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多