繁体   English   中英

REST 调用不适用于运行在 Docker 中的 Camel

[英]REST call not working with Camel running in Docker

我有这条骆驼 Rest 路线:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.model.rest.RestBindingMode;
import spark.Spark;

public class MainCamel {

public static void main(final String[] args) throws Exception {
    final Main camelMain = new Main();
    camelMain.configure().addRoutesBuilder(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            this.getContext().getRegistry().bind("healthcheck", CheckUtil.class);
            this.restConfiguration()
                    .bindingMode(RestBindingMode.auto)
                    .component("netty-http")
                    .host("localhost")
                    .port(11010);
            this.rest("/healthcheck")
                    .get()
                    .description("Healthcheck for docker")
                    .outType(Integer.class)
                    .to("bean:healthcheck?method=healthCheck");
        }
    });

    // spark
    Spark.port(11011);
    Spark.get("/hello", (req, res) -> "Hello World");

    System.out.println("ready");
    camelMain.run(args);
}

public static class CheckUtil {
    public Integer healthCheck() {
        return 0;
    }
}

}

我还使用 Spark 创建了第二个 REST 服务器。 如果代码在 Docker 容器中执行,Camel 路由将不起作用。 Exception: org.apache.http.NoHttpResponseException: localhost:11010 failed to respond Spark 服务器工作正常。

但是,当直接在 IntelliJ 中执行代码时,两个 REST 服务器都可以工作。 当然两个端口都暴露在容器中。

您正在将 Netty HTTP 服务器绑定到localhost 这意味着它将无法处理来自容器外部的请求。

.host("localhost")更改为.host("0.0.0.0")以便服务器侦听所有可用的网络接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM