是否可以在 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/