241.8. 使用具有相同端口的多个路由
在同一个 CamelContext 中,您可以有多个来自 Netty HTTP 的路由,它们共享相同的端口(如 org.jboss.netty.bootstrap.ServerBootstrap 实例)。这样做需要路由中的多个 bootstrap 选项相同,因为路由将共享相同的 org.jboss.netty.bootstrap.ServerBootstrap 实例。该实例将配置有第一个路由的选项。
路由的选项必须相同,这是 org.apache.camel.component.netty.NettyServerBootstrapConfiguration 配置类中定义的所有选项。如果您使用不同的选项配置了另一个路由,则 Camel 会在启动时抛出异常,表示选项不相同。要缓解这个问题,请确保所有选项都相同。
下面是一个共享同一端口的两个路由的示例。
共享同一端口的两个路由
from("netty-http:http://0.0.0.0:{{port}}/foo")
.to("mock:foo")
.transform().constant("Bye World");
from("netty-http:http://0.0.0.0:{{port}}/bar")
.to("mock:bar")
.transform().constant("Bye Camel");
这里是一个错误配置的 2 个路由示例,它没有相同的 org.apache.camel.component.netty.NettyServerBootstrapConfiguration 选项作为 1st 路由。这将导致 Camel 在启动时失败。
共享同一端口的两个路由,但第 2 个路由配置错误,并且在启动时会失败
from("netty-http:http://0.0.0.0:{{port}}/foo")
.to("mock:foo")
.transform().constant("Bye World");
// we cannot have a 2nd route on same port with SSL enabled, when the 1st route is NOT
from("netty-http:http://0.0.0.0:{{port}}/bar?ssl=true")
.to("mock:bar")
.transform().constant("Bye Camel");
241.8.1. 使用多个路由重复使用相同的服务器 bootstrap 配置 复制链接链接已复制到粘贴板!
通过在 org.apache.camel.component.netty.NettyServerBootstrapConfiguration 类型的单一实例中配置 common server bootstrap 选项,我们可以在 Netty HTTP 用户中使用 bootstrapConfiguration 选项来引用并重复使用所有消费者中的相同选项。
<bean id="nettyHttpBootstrapOptions" class="org.apache.camel.component.netty.NettyServerBootstrapConfiguration">
<property name="backlog" value="200"/>
<property name="connectTimeout" value="20000"/>
<property name="workerCount" value="16"/>
</bean>
在路由中,您可以引用这个选项,如下所示
<route>
<from uri="netty-http:http://0.0.0.0:{{port}}/foo?bootstrapConfiguration=#nettyHttpBootstrapOptions"/>
...
</route>
<route>
<from uri="netty-http:http://0.0.0.0:{{port}}/bar?bootstrapConfiguration=#nettyHttpBootstrapOptions"/>
...
</route>
<route>
<from uri="netty-http:http://0.0.0.0:{{port}}/beer?bootstrapConfiguration=#nettyHttpBootstrapOptions"/>
...
</route>
241.8.2. 在 OSGi 容器中使用多个捆绑包间使用多个路由重复使用相同的服务器 bootstrap 配置 复制链接链接已复制到粘贴板!
如需了解更多详细信息和示例,请参阅 Netty HTTP Server Example。