12.5. 配置网络运行时

概述

Netty 运行时供 HTTP 服务供应商和 HTTP 消费者使用分离端点。可以配置运行时的线程池,您还可以通过 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.17 “Netty Runtime Configuration Namespace” 中显示的行添加到端点配置文件的 Bean 元素中。另外,您必须将配置元素的命名空间添加到 xsi:schemaLocation 属性。

例 12.17. Netty Runtime Configuration Namespace

<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 运行时的根元素。它只有一个必需属性 bus,其值是管理所配置的 Netty 实例的 总线 名称。

注意

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

httpn:engine-factory 元素有三个子项,其中包含用于配置由 Netty runtime factory 实例化的 HTTP 端口的信息。子对象在 表 12.11 “配置 Netty Runtime Factory 的元素” 中描述。

表 12.11. 配置 Netty Runtime Factory 的元素

元素描述

httpn:engine

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

httpn:identifiedTLSServerParameters

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

httpn:identifiedThreadingParameters

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

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

engine 元素

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

表 12.12. 配置网络运行时实例的属性

属性描述

port

指定 Netty HTTP 服务器实例使用的端口。您可以为端口属性指定一个 0 值。引擎元素中指定的线程属性设为 0,其 port 属性设置为 0,所有未明确配置的 Netty 侦听器的配置。

主机

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

readIdleTime

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

writeIdleTime

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

maxChunkContentSize

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

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

httpn:engine 支持的子元素显示在 表 12.13 “配置网络运行时实例的元素” 中。

表 12.13. 配置网络运行时实例的元素

元素描述

httpn:tlsServerParameters

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

httpn:tlsServerParametersRef

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

httpn:threadingParameters

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

httpn:threadingParametersRef

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

httpn:sessionSupport

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

httpn:reuseAddress

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

配置线程池

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

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

threadingParameters 元素有一个属性来指定线程池的大小,如 表 12.14 “配置网络线程池的属性” 所述。

注意

httpn:identifiedThreadingParameters 元素具有一个子 线程参数 元素。

表 12.14. 配置网络线程池的属性

属性描述

threadPoolSize

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

示例

例 12.18 “配置网络实例” 显示配置各种 Netty 端口的配置片段。

例 12.18. 配置网络实例

<?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>