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");
Copy to Clipboard Toggle word wrap

下面是一个错误配置的 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");
Copy to Clipboard Toggle word wrap

通过在 org.apache.camel.component.netty.NettyServerBootstrapConfiguration 类型的单个实例中配置通用服务器 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>
Copy to Clipboard Toggle word wrap

在您引用此选项的路由中,如下所示

<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>
Copy to Clipboard Toggle word wrap

详情请查看 Netty HTTP 服务器 示例。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat