Camel SpringBoot rest servlet 连接被拒绝(camel 2.22.0)

Camel SpringBoot rest servlet connection refused (camel 2.22.0)

我无法让任何最简单的 springboot servlet rest api 示例在我的机器上运行。我只是想创建最简单的测试 api 来练习框架。我的 Routes RouteBuilder class:

中有以下代码
@Component
public class Routes extends RouteBuilder {
@Override
public void configure() {
    restConfiguration()//Bind the api servlet to the localhost port 8080
        .component("servlet").host("localhost").port(8080)
        .bindingMode(RestBindingMode.auto);

    rest("/api")//Log any get requests
    .get()
    .route().to("log:DEBUG?showBody=true&showHeaders=true");

}
}

但是,当我尝试使用 curl 调用此代码时,出现以下错误:

curl -X GET http://localhost:8080/api
curl: (7) Failed to connect to localhost port 8080: Connection refused

我正在使用 camel 2.22.0 和 SpringBoot 2.0.4.RELEASE。我是 运行 Ubuntu 20.04 LTS。

编辑: 我做了建议的更改,但我仍然通过 curl 得到相同的 Connection refused。我的代码现在看起来像这样:

restConfiguration()//Bind the api servlet to the localhost port 8080
    .component("servlet").host("localhost").port(8080)//Use camel default context path
    .bindingMode(RestBindingMode.auto);

    rest("/api")//Log any get requests
    .get()
    .route().to("log:DEBUG?showBody=true&showHeaders=true");

curl -X GET http://localhost:8080/camel/api -> Connection refused

现在我的 application.yml 中还有以下内容:

server:
  port: 8080 #Specify port for camel servlet
  max-http-header-size: 32768 # Maximum size in bytes of the HTTP message header.

默认情况下,Camel 使用上下文路径 /camel/*.

因此,您的 curl 命令应如下所示:

curl -X GET http://localhost:8080/camel/api

您可以通过以下方式控制上下文路径。

restConfiguration

restConfiguration()//Bind the api servlet to the localhost port 8080
   .component("servlet").host("localhost").port(8080)
    .contextPath("/test/*")
    .bindingMode(RestBindingMode.auto);

application.properties

camel.component.servlet.mapping.context-path=/test/*

对我来说,只有后者有效

这里有一点值得一提。

您在剩余定义中使用了 servlet 组件。在这种情况下,Camel 会忽略 port 配置并使用底层的 servlet 组件。当您使用 spring-boot 时,正在使用 tomcat 端口,默认情况下恰好是 8080。

如果由于某种原因,您更改了 tomcat 端口,您的其余服务端口也会更改。

例如,如果您更改 application.properties 中的服务器端口。

server.port=8180

您的其余服务使用该端口,忽略 restConfiguration 中的定义。

curl -X GET http://localhost:8180/camel/api

休息 DSL docs