242.8. 使用具有相同端口的多个路由


在同一个 CamelContext 中,您可以有多个来自 Netty4 HTTP 的路由,该 HTTP 共享同一端口(如 io.netty.bootstrap.ServerBootstrap 实例)。这样做需要在路由中有多个 bootstrap 选项,因为路由将共享相同的 io.netty.bootstrap.ServerBootstrap 实例。该实例将使用创建的第一个路由中的选项进行配置。

路由必须相同的选项是 org.apache.camel.component.netty4.NettyServerBootstrapConfiguration 配置类中定义的所有选项。如果您为另一个路由配置了不同的选项,Camel 将在启动时抛出异常,表示选项不相同。要缓解这个问题,请确保所有选项都相同。

下面是两个共享同一端口的路由的示例:

两个路由共享相同的端口

from("netty4-http:http://0.0.0.0:{{port}}/foo")
  .to("mock:foo")
  .transform().constant("Bye World");

from("netty4-http:http://0.0.0.0:{{port}}/bar")
  .to("mock:bar")
  .transform().constant("Bye Camel");
Copy to Clipboard

下面是一个错误配置的 2nd 路由示例,它没有与 org.apache.camel.component.netty4.NettyServerBootstrapConfiguration 选项作为 1st 路由相同的。这会导致 Camel 启动时失败。

两个共享同一端口的路由,但第二代路由被错误配置,并将在启动时失败

from("netty4-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("netty4-http:http://0.0.0.0:{{port}}/bar?ssl=true")
  .to("mock:bar")
  .transform().constant("Bye Camel");
Copy to Clipboard

242.8.1. 使用多个路由重复使用相同的服务器 bootstrap 配置

通过在 org.apache.camel.component.netty4.NettyServerBootstrapConfiguration 类型的单个实例中配置通用服务器 bootstrap 选项,我们可以在所有消费者中使用 bootstrapConfiguration 选项,以引用和重复使用所有消费者的相同选项。

<bean id="nettyHttpBootstrapOptions" class="org.apache.camel.component.netty4.NettyServerBootstrapConfiguration">
  <property name="backlog" value="200"/>
  <property name="connectionTimeout" value="20000"/>
  <property name="workerCount" value="16"/>
</bean>
Copy to Clipboard

在路由中,您引用这个选项,如下所示

<route>
  <from uri="netty4-http:http://0.0.0.0:{{port}}/foo?bootstrapConfiguration=#nettyHttpBootstrapOptions"/>
  ...
</route>

<route>
  <from uri="netty4-http:http://0.0.0.0:{{port}}/bar?bootstrapConfiguration=#nettyHttpBootstrapOptions"/>
  ...
</route>

<route>
  <from uri="netty4-http:http://0.0.0.0:{{port}}/beer?bootstrapConfiguration=#nettyHttpBootstrapOptions"/>
  ...
</route>
Copy to Clipboard
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat