Apache Camel Route:"Cannot assign requested address" 尝试从服务器获取数据时

Apache Camel Route: "Cannot assign requested address" when trying to fetch data from server

我想从一个非常简单的网络服务器获取数据,该服务器在浏览器中调用时显示一个页面,为我提供一些数据。该页面自动更新了几秒钟并显示:

analog input 0 is 1023
analog input 1 is 109
analog input 2 is 0
analog input 3 is 415
analog input 4 is 0
analog input 5 is 40

在最后的程序中,我想从页面中获取数据并通过apache camel 将其转换为流入时间序列。到目前为止,我了解到码头组件是要走的路,并按如下方式配置了我的路线:

public class HttpReaderRoute extends RouteBuilder {
    @Autowired
    private FrameworkConfig frameworkConfig;

    @Override
    public void configure() throws Exception {

        String sourceString = "jetty:http://server.i.want.to.poll:8080/?sessionSupport=true";
        String targetString = "stream:out";
        // String targetString = "influxdb://influxDb?databaseName=" //
        // + frameworkConfig.getInfluxDb().getDatabaseName() //
        // + "&retentionPolicy=" //
        // + frameworkConfig.getInfluxDb().getRetentionPolicy();

        System.out.println(sourceString);

        from(sourceString)
                .log("Received data server") //
                .to(targetString);
    }
}

但是,我得到一个 org.apache.camel.RuntimeCamelException: java.net.BindException: Cannot assign requested address 或者 - 如果我跳过使用格式 jetty:http:server.i.want.to.poll:8080,我得到一个 "permission denied".

相关配置class如下:

@Configuration
public class HttpClientConfig {

    @Bean
    public PoolingHttpClientConnectionManager poolingConnectionManager() {
        PoolingHttpClientConnectionManager poolingConnectionManager = new 

PoolingHttpClientConnectionManager();
            poolingConnectionManager.setMaxTotal(10);
            return poolingConnectionManager;
    }
}

也许值得一提的是我正在使用spring.boot。

我做错了什么?

您是否正在尝试使用 Apache Camel 从某些现有 Web 服务中轮询数据?如果是,你应该使用HTTP4 Component instead of Jetty componet

这是让您尽快进入下一步的快速答案。 你可以做类似

的事情
from("timer:hello?period=2000").routeId("test_route")
.to("http4://example.com/")
.log("${body}")
.to("file:target/messages");

添加依赖后

 <dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-http4</artifactId>
  <version>X.X.X</version>
 </dependency>

我们需要在这里进一步挖掘,因为人们在同一问题上绊倒的可能性很高,因为 CAMEL-13220 已于 2019 年 6 月删除了对码头组件的 Producer 支持。这意味着码头 不能再执行 HTTP 客户端功能了。

  • 代码示例中的路由以 from("jetty:http://server.i.want.to.poll:8080/?sessionSupport=true") 开头。该代码实际上设置了一个 HTTP 服务器(或 Camel 术语中的消费者,如 here in old docs 所示)。很可能,在 OP 的测试基础设施中:
  • server.i.want.to.polllocalhost
  • 有一些其他服务正在侦听 8080,可能是服务 OP 正在尝试连接。这可能是导致 java.net.BindException
  • 的原因
  • 过去Jetty组件既可以作为生产者,也可以作为消费者。如果 OP 使用 2.x 版本的 Camel,仍然可以使用 Jetty 组件作为 HTTP 客户端(生产者),路由如 from("direct:start").to("jetty://http://www.google.com")。但是,我建议在适用时坚持使用 HTTP/HTTP4 组件。

PS:我的测试设置是 Spring 引导应用程序,运行 Camel 3.0.0-M4