【问题标题】:Camel jetty rest methods on 2 ports and protocols2个端口和协议上的骆驼码头休息方法
【发布时间】:2016-11-25 10:50:34
【问题描述】:

是否可以在骆驼中做到这一点: 2个休息服务站在码头上,首先是http(例如在端口1234上)和第二个https(例如在端口4321上),我该如何配置它?这可能吗?

我需要接收的效果(示例网址):

http://localhost:1234/firstHttpMethod
http://localhost:1234/secondHttpMethod
https://localhost:4321/firstHttpsMethod
https://localhost:4321/secondHttpsMethod

目前,当我尝试添加 2 条路线时,只有第二条在工作。如何解决这个问题(我有一个想法要做 2 次休息服务 - 首先在码头上,其次在其他地方,但它不是一个好的概念)。

代码如下:

camelContext.addRoutes(firstJettyBuilder());
camelContext.addRoutes(secondJettyBuilder());

protected RouteBuilder firstJettyBuilder()
{
    return new RouteBuilder()
    {
        @Override
        public void configure()
            throws Exception
        {

            restConfiguration()
                .component("jetty")
                .host("localhost") 
                .port(42300) 
                .scheme("https") 
                .bindingMode(RestBindingMode.json)
                .dataFormatProperty("json.in.disableFeatures", "FAIL_ON_UNKNOWN_PROPERTIES")
                .dataFormatProperty("json.in.enableFeatures", "FAIL_ON_NULL_FOR_PRIMITIVES");

            configureSSL();
        }

        private void configureSSL()
        {
            final JettyHttpComponent jettyComponent = camelContext.getComponent("jetty", JettyHttpComponent.class);

            final Map<String, Object> sslSocketConnectorProperties = new HashMap<>();

            sslSocketConnectorProperties.put("keyStorePath", KEYSTORE);
            sslSocketConnectorProperties.put("trustStorePath", KEYSTORE);

            sslSocketConnectorProperties.put("keyStorePassword", KEYSTORE_PASSWORD);
            sslSocketConnectorProperties.put("trustStorePassword", KEYSTORE_PASSWORD);

            jettyComponent.setSslSocketConnectorProperties(sslSocketConnectorProperties);
        }
    };
}
protected RouteBuilder createPosJettyBuilder()
{
    return new RouteBuilder()
    {
        @Override
        public void configure()
            throws Exception
        {

            restConfiguration()
                .component("jetty")
                .host("localhost") 
                .port(42302) 
                .scheme("http")
                .bindingMode(RestBindingMode.json)
                .dataFormatProperty("json.in.disableFeatures", "FAIL_ON_UNKNOWN_PROPERTIES")
                .dataFormatProperty("json.in.enableFeatures", "FAIL_ON_NULL_FOR_PRIMITIVES");

        }

    };
}

【问题讨论】:

  • 请同时提供您的路线配置。
  • 第二个工作是什么意思?当您启动骆驼上下文时,两条路线都会出现吗?只有一条路线?
  • 取决于调用第一个或第二个 jettyBuilder 的顺序,我只能连接到其中一个服务

标签: java rest apache-camel jetty


【解决方案1】:

简短的回答:我不认为这在同一个 Camel 上下文中是可能的,因为我可以称之为错误。在不同的上下文中可能是可能的。


这是调试后的一些观察结果。

第一次尝试:如问题。

Camel 对两种配置使用相同的 Jetty 端点。第二个 RouteBuilder 覆盖第一个的端点配置。因此,预期的第一台服务器根本没有运行。

第二次尝试:多个 Jetty 端点。

人们可能会尝试类似(在创建 Jetty 端点并将它们添加到上下文之后):

this.restConfiguration("jetty")....
this.rest("/path").... // good
...
this.restConfiguration("jetty-tls")....
this.rest("/path").... // produces exception!

看起来其余定义已添加到 Camel 上下文中。在为第二个 RouteBuilder 创建路由时,第一个的定义已经存在。 Camel 想要创建 2 条路径相同的路由并抛出异常:

Failed to start route ... because of Multiple consumers for the same endpoint is not allowed: jetty:...

不幸的是,跳过其中一个构建器中的其余定义不是一种选择。

奖励尝试:多个 Jetty 端点和不同的路径。

人们希望至少这会起作用:

this.restConfiguration("jetty")....
this.rest("/path1").... // good
...
this.restConfiguration("jetty-tls")....
this.rest("/path2").... // good

这里没有例外,但骆驼开始了 3 条路线!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2019-07-07
    • 1970-01-01
    相关资源
    最近更新 更多