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


在同一个 CamelContext 中,您可以有多个来自 Netty4 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 Toggle word wrap

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

共享同一端口的两个路由,但第 2 个路由配置错误,并且在启动时会失败

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 Toggle word wrap

通过在 org.apache.camel.component.netty4.NettyServerBootstrapConfiguration 类型的单一实例中配置 common server bootstrap 选项,我们可以在 Netty4 HTTP 用户中使用 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 Toggle word wrap

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

<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 Toggle word wrap

如需了解更多详细信息和示例,请参阅 Netty HTTP Server Example。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat