104.8. 例子
在以下路由中,我们使用 Netty HTTP 作为 HTTP 服务器,它返回一个硬编码的"Bye World"消息。
from("netty-http:http://0.0.0.0:8080/foo")
.transform().constant("Bye World");
我们也可以使用 Camel 调用此 HTTP 服务器,以及 ProducerTemplate,如下所示:
String out = template.requestBody("netty-http:http://0.0.0.0:8080/foo", "Hello World", String.class);
System.out.println(out);
我们以输出形式返回 "Bye World"。
104.8.1. 如何让 Netty 匹配通配符 复制链接链接已复制到粘贴板!
默认情况下,Netty HTTP 仅在确切的 uri 上匹配。但是,您可以指示 Netty 匹配前缀。例如:
from("netty-http:http://0.0.0.0:8123/foo").to("mock:foo");
在上述 Netty HTTP 的路由中,只有在 uri 完全匹配时才会匹配,因此,如果您输入 http://0.0.0.0:8123/foo,但如果输入 http://0.0.0.0:8123/foo/bar 不匹配,它将匹配。
因此,如果要启用通配符匹配,如下所示:
from("netty-http:http://0.0.0.0:8123/foo?matchOnUriPrefix=true").to("mock:foo");
因此,Netty 与以 foo 开头的任何端点匹配。
要匹配 任何 端点,您可以执行以下操作:
from("netty-http:http://0.0.0.0:8123?matchOnUriPrefix=true").to("mock:foo");
104.8.2. 在同一端口使用多个路由 复制链接链接已复制到粘贴板!
在同一 CamelContext 中,您可以使用来自 Netty HTTP 的多个路由来共享同一端口(如 io.netty.bootstrap.ServerBootstrap 实例)。执行此操作需要在路由中有多个 bootstrap 选项,因为路由将共享相同的 io.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");
104.8.3. 使用多个路由重复使用相同的服务器 bootstrap 配置 复制链接链接已复制到粘贴板!
通过在 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="connectionTimeout" 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>
104.8.4. 使用 OSGi 容器中多个捆绑包的多个路由重复使用相同的服务器引导配置 复制链接链接已复制到粘贴板!
详情请查看上述 Netty HTTP 服务器 示例。
104.8.5. 实施反向代理 复制链接链接已复制到粘贴板!
netty HTTP 组件可以充当反向代理,在这种情况下,Exchange.HTTP_SCHEME、Exchange.HTTP_HOST 和 Exchange.HTTP_PORT 标头是从 HTTP 请求请求行中收到的绝对 URL 填充的。
以下是只是将响应从原始服务器转换为大写的 HTTP 代理的示例。
from("netty-http:proxy://0.0.0.0:8080")
.toD("netty-http:"
+ "${headers." + Exchange.HTTP_SCHEME + "}://"
+ "${headers." + Exchange.HTTP_HOST + "}:"
+ "${headers." + Exchange.HTTP_PORT + "}")
.process(this::processResponse);
void processResponse(final Exchange exchange) {
final NettyHttpMessage message = exchange.getIn(NettyHttpMessage.class);
final FullHttpResponse response = message.getHttpResponse();
final ByteBuf buf = response.content();
final String string = buf.toString(StandardCharsets.UTF_8);
buf.resetWriterIndex();
ByteBufUtil.writeUtf8(buf, string.toUpperCase(Locale.US));
}