12.4. 配置 Undertow 运行时

概述

Undertow 运行时供 HTTP 服务提供商和 HTTP 用户使用分离端点。可以配置运行时的线程池,您可以通过 Undertow 运行时设置 HTTP 服务提供商的多个安全设置。

Maven 依赖项

如果将 Apache Maven 用作构建系统,您可以通过在项目的 pom.xml 文件中包含下列依赖项,将 Undertow 运行时添加到项目中:

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

命名空间

用于配置 Undertow 运行时的元素在命名空间 http://cxf.apache.org/transports/http-undertow/configuration 中定义。若要使用 Undertow 配置元素,您必须将 例 12.14 “Undertow 运行时配置命名空间” 中显示的行添加到端点配置文件的 Bean 元素中。在本例中,命名空间被分配了前缀 httpu。另外,您必须将配置元素的命名空间添加到 xsi:schemaLocation 属性。

例 12.14. Undertow 运行时配置命名空间

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

engine-factory 元素

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

注意

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

http:engine-factory 元素有三个子项,其中包含用于配置使用 Undertow 运行时工厂实例化的 HTTP 端口的信息。子对象在 表 12.7 “配置Undertow Runtime onnectionFactoryy 的元素” 中描述。

表 12.7. 配置Undertow Runtime onnectionFactoryy 的元素

元素描述

httpu:engine

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

httpu:identifiedTLSServerParameters

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

httpu:identifiedThreadingParameters

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

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

engine 元素

httpu:engine 元素用于配置 Undertow 运行时的特定实例。它有两个属性( host ),它指定具有嵌入式 undertow 和端口 的全局 IP 地址,用于指定由 Undertow 实例管理的端口数量。

注意

您可以为 端口 属性指定一个 0 值。httpu:engine 元素中指定的任何线程属性设为 0, 用作未明确配置的所有 Undertow 侦听器的配置。

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

表 12.8 “配置 Undertow 运行时实例的元素” 中描述了用于提供配置属性的子元素。

表 12.8. 配置 Undertow 运行时实例的元素

元素描述

httpu:tlsServerParameters

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

httpu:tlsServerParametersRef

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

httpu:threadingParameters

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

httpu:threadingParametersRef

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

配置线程池

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

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

threadingParameters 有两个属性来指定线程池的大小。属性在 表 12.9 “配置 Undertow 线程池的属性” 中描述。

注意

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

表 12.9. 配置 Undertow 线程池的属性

属性描述

workerIOThreads

指定为 worker 创建的 I/O 线程数量。如果未指定,则会选择 dafult 值。默认值为每个 CPU 内核的一个 I/O 线程。

minThreads

指定用于处理请求的最小线程数量。

最大线程数

指定用于处理请求的最大线程数量。

示例

例 12.15 “配置 Undertow 实例” 显示配置片段,在端口号 9001 上配置 Undertow 实例。

例 12.15. 配置 Undertow 实例

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:sec="http://cxf.apache.org/configuration/security"
  xmlns:http="http://cxf.apache.org/transports/http/configuration"
  xmlns:httpu="http://cxf.apache.org/transports/http-undertow/configuration"
  xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
  xsi:schemaLocation="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-undertow/configuration
            http://cxf.apache.org/schemas/configuration/http-undertow.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
  ...

  <httpu:engine-factory bus="cxf">
    <httpu:identifiedTLSServerParameters id="secure">
      <sec:keyManagers keyPassword="password">
        <sec:keyStore type="JKS" password="password"
                      file="certs/cherry.jks"/>
      </sec:keyManagers>
    </httpu:identifiedTLSServerParameters>

    <httpu:engine port="9001">
      <httpu:tlsServerParametersRef id="secure" />
      <httpu:threadingParameters minThreads="5"
                                 maxThreads="15" />
    </httpu:engine>
  </httpu:engine-factory>
 </beans>

限制并发请求和队列大小

您可以配置 Request Limiting Handler,该处理程序将设置并发连接请求数和可通过 Undertow 服务器实例处理的最大队列大小。此配置示例如下所示 例 12.16 “限制连接请求和队列大小”

表 12.10. 用于配置请求限制处理程序的属性

属性描述

maximumConcurrentRequests

指定由 Undertow 实例处理的最大并发请求数量。如果请求数超过这个限制,则请求会被放入。

queueSize

指定可通过 Undertow 实例处理的请求总数。如果请求数超过这个限制,则请求将被拒绝。

例 12.16. 限制连接请求和队列大小

<httpu:engine-factory>
     <httpu:engine port="8282">
         <httpu:handlers>
             <bean class="org.jboss.fuse.quickstarts.cxf.soap.CxfRequestLimitingHandler">
                 <property name="maximumConcurrentRequests" value="1" />
                 <property name="queueSize" value="1"/>
             </bean>
         </httpu:handlers>
     </httpu:engine>
</httpu:engine-factory>