240.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");
下面是一个错误配置的 2nd 路由示例,它没有与 org.apache.camel.component.netty.NettyServerBootstrapConfiguration 选项作为 1st 路由相同的。这会导致 Camel 启动时失败。
两个共享同一端口的路由,但第二代路由被错误配置,并将在启动时失败
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");240.8.1. 使用多个路由重复使用相同的服务器 bootstrap 配置
通过在 org.apache.camel.component.netty.NettyServerBootstrapConfiguration 类型的单一实例中配置通用服务器 bootstrap 选项,我们可以在所有消费者中使用 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>