java - Camel jetty 在 2 个端口和协议(protocol)上的休息方法

标签 java rest apache-camel jetty

是否可以在 Camel 中执行此操作: jetty 上有 2 个 REST 服务,第一个通过 http(例如在端口 1234 上),第二个通过 https(例如在端口 4321 上),我该如何配置它?这可能吗?

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

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

目前,当我尝试添加 2 条路线时,只有第二条路线有效。如何解决这个问题(我想做两次休息服务 - 首先在 jetty ,第二次在其他地方,但这不是一个好主意)。

代码如下所示:

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");

        }

    };
}

最佳答案

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

<小时/>

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

第一次尝试:如问题中所示。

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

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

人们可以尝试类似的方法(在创建 Jetty 端点并将它们添加到上下文中之后):

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

看起来其余定义已添加到 Camel 上下文中。在为第二个 RouteBuilder 创建路由时,第一个 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

这里也不异常(exception),但 Camel 启动了 3 条路线!

关于java - Camel jetty 在 2 个端口和协议(protocol)上的休息方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40803215/

相关文章:

java - 无法在小程序中使用 ucanaccess 方法连接或插入数据库

json - 将 JSON POST 请求从一个 REST API 转发到另一个

java - Dropwizard:如何为 GET/PUT 添加自定义验证

java - 如何将 Camel Unmarshal CSV 映射到 Dozer Bean

java - 为什么标签小部件在 Android 中位于内容之上?

java - Skip_unusable_indexes (oracle) 的 Hibernate 等效项

java - Maven 和 m2e - 使用依赖信息构建 jar

iphone - 如何在 iPhone 上的亚马逊上按 ISBN 搜索书籍

apache-camel - 更改编织语句的顺序时,CameladviceWith 的行为有所不同

java - Apache Camel : How to persist property or header between multiple Exchanges after split and subsequent Exception?