Red Hat Training

A Red Hat training course is available for Red Hat Fuse

12.5. 配置 Netty 运行时

概述

HTTP 服务供应商和 HTTP 用户使用 Netty 运行时,使用分离的端点。可以配置运行时的线程池,您还可以通过 Netty 运行时为 HTTP 服务提供商设置一些安全设置。

Maven 依赖项

如果您使用 Apache Maven 作为构建系统,您可以在项目的 pom.xml 文件中添加以下依赖项,将 Netty 运行时(用于定义 Web 服务端点)的服务器端实施添加到项目中:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-netty-server</artifactId>
    <version>${cxf-version}</version>
</dependency>

您可以通过在项目的 pom.xml 文件中包括以下依赖项,将 Netty 运行时(用于定义 Web 服务客户端)的客户端实现添加到项目中:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-netty-client</artifactId>
    <version>${cxf-version}</version>
</dependency>

命名空间

用于配置 Netty 运行时的元素在命名空间 http://cxf.apache.org/transports/http-netty-server/configuration 中定义。通常会使用前缀 httpn 来引用。要使用 Netty 配置元素,您必须将 例 12.16 “Netty Runtime Configuration 命名空间” 中显示的行添加到端点配置文件的 Bean 元素中。另外,您必须将配置元素的命名空间添加到 xsi:schemaLocation 属性中。

例 12.16. Netty Runtime Configuration 命名空间

<beans ...
       xmlns:httpn="http://cxf.apache.org/transports/http-netty-server/configuration"
       ...
       xsi:schemaLocation="...
               http://cxf.apache.org/transports/http-netty-server/configuration
            http://cxf.apache.org/schemas/configuration/http-netty-server.xsd
               ...">

engine-factory 元素

httpn:engine-factory 元素是用于配置供应用使用的 Netty 运行时的根元素。它具有单个必需属性 总线,其值是管理所配置的 Netty 实例的 总线 的名称。

注意

该值通常是 cxf,这是默认 总线实例的名称

httpn:engine-factory 元素有三个子项,其中包含用于配置由 Netty 运行时工厂实例化的 HTTP 端口的信息。子项包括在 表 12.10 “配置 Netty Runtime Factory 的元素” 中。

表 12.10. 配置 Netty Runtime Factory 的元素

element描述

httpn:engine

指定特定 Netty 运行时实例的配置。请参阅 “engine 元素”一节

httpn:identifiedTLSServerParameters

指定用于保护 HTTP 服务提供商的可重用属性集。它有一个属性 id,它指定属性可以引用的唯一标识符。

httpn:identifiedThreadingParameters

指定控制 Netty 实例的线程池的可重用属性集合。它有一个属性 id,它指定属性可以引用的唯一标识符。

请参阅 “配置线程池”一节

engine 元素

httpn:engine 元素用于配置 Netty 运行时的特定实例。表 12.11 “配置 Netty Runtime 实例的属性” 显示 httpn:engine 元素支持的属性。

表 12.11. 配置 Netty Runtime 实例的属性

属性描述

port

指定 Netty HTTP 服务器实例使用的端口。您可以为 port 属性指定 0 值。engine 元素中指定的任何线程属性及其 port 属性设置为 0,作为未明确配置的 Netty 监听器的配置。

主机

指定 Netty HTTP 服务器实例使用的侦听地址。该值可以是主机名或 IP 地址。如果没有指定,Netty HTTP 服务器将侦听所有本地地址。

readIdleTime

指定 Netty 连接的最大读时时间。每当对底层流有任何读取操作时,会重置计时器。

writeIdleTime

指定 Netty 连接的最大写入空闲时间。每当对底层流有任何写入操作时,会重置计时器。

maxChunkContentSize

指定 Netty 连接的最大聚合内容大小。默认值为 10MB。

httpn:engine 元素具有一个用于配置安全属性的子元素,以及用于配置 Netty 实例的线程池的一个子元素。对于每种配置,您可以直接提供配置信息,也可以提供对父 httpn:engine-factory 元素中定义的一组配置属性的引用。

httpn:engine 支持的子元素显示在 表 12.12 “配置 Netty Runtime 实例元素” 中。

表 12.12. 配置 Netty Runtime 实例元素

element描述

httpn:tlsServerParameters

指定一组属性,用于配置用于特定 Netty 实例的安全性。

httpn:tlsServerParametersRef

指的是由 identifiedTLSServerParameters 元素定义的一组安全属性。id 属性提供引用的 TLSServerParameters 元素的 id。

httpn:threadingParameters

指定特定 Netty 实例使用的线程池的大小。请参阅 “配置线程池”一节

httpn:threadingParametersRef

是指由 确定的ThreadingParameters 元素定义的一组属性。id 属性提供所 识别的ThreadingParameters 元素的 id。

httpn:sessionSupport

true 时,启用对 HTTP 会话的支持。默认为 false

httpn:reuseAddress

指定设置 ReuseAddress TCP socket 选项的布尔值。默认为 false

配置线程池

您可以通过以下任一方式配置 Netty 实例的线程池的大小:

  • 使用 engine-factory 元素中的 identifiedThreadingParameters 元素指定线程池的大小。然后,您可以使用 threadingParametersRef 元素来引用这个元素。
  • 使用 threadingParameters 元素直接指定线程池的大小。

threadingParameters 元素有一个属性来指定线程池的大小,如 表 12.13 “配置 Netty Thread Pool 的属性” 所述。

注意

httpn:identifiedThreadingParameters 元素具有单个子 线程Parameters 元素。

表 12.13. 配置 Netty Thread Pool 的属性

属性描述

threadPoolSize

指定 Netty 实例用于处理请求的线程数量。

示例

例 12.17 “配置 Netty 实例” 显示配置片段,用于配置各种 Netty 端口。

例 12.17. 配置 Netty 实例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:h="http://cxf.apache.org/transports/http/configuration"
       xmlns:httpn="http://cxf.apache.org/transports/http-netty-server/configuration"
       xmlns:sec="http://cxf.apache.org/configuration/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/configuration/security
            http://cxf.apache.org/schemas/configuration/security.xsd
        http://cxf.apache.org/transports/http/configuration
            http://cxf.apache.org/schemas/configuration/http-conf.xsd
        http://cxf.apache.org/transports/http-netty-server/configuration
            http://cxf.apache.org/schemas/configuration/http-netty-server.xsd"
>
    ...
    <httpn:engine-factory bus="cxf">
       <httpn:identifiedTLSServerParameters id="sample1">
         <httpn:tlsServerParameters jsseProvider="SUN" secureSocketProtocol="TLS">
            <sec:clientAuthentication want="false" required="false"/>
         </httpn:tlsServerParameters>
       </httpn:identifiedTLSServerParameters>

       <httpn:identifiedThreadingParameters id="sampleThreading1">
          <httpn:threadingParameters threadPoolSize="120"/>
       </httpn:identifiedThreadingParameters>

       <httpn:engine port="9000" readIdleTime="30000" writeIdleTime="90000">
          <httpn:threadingParametersRef id="sampleThreading1"/>
       </httpn:engine>

       <httpn:engine port="0">
          <httpn:threadingParameters threadPoolSize="400"/>
       </httpn:engine>

       <httpn:engine port="9001" readIdleTime="40000" maxChunkContentSize="10000">
         <httpn:threadingParameters threadPoolSize="99" />
         <httpn:sessionSupport>true</httpn:sessionSupport>
       </httpn:engine>

       <httpn:engine port="9002">
         <httpn:tlsServerParameters>
           <sec:clientAuthentication want="true" required="true"/>
         </httpn:tlsServerParameters>
       </httpn:engine>

       <httpn:engine port="9003">
          <httpn:tlsServerParametersRef id="sample1"/>
       </httpn:engine>

    </httpn:engine-factory>
</beans>