# 8.2.2. 嵌入式反应式服务器支持

Spring Boot 支持以下嵌入式反应式 Web 服务器：Reactor Netty、Tomcat、Jetty 和 Undertow。大多数开发人员使用适当的“Starter”来获取完全配置的实例。默认情况下，嵌入式服务器在端口 8080 上侦听 HTTP 请求。

**定制反应式服务器**

常见的反应式 Web 服务器设置可以使用 Spring`Environment`属性进行配置。通常，您可以在您的`application.properties`或`application.yaml`文件中定义属性。

常见的服务器设置包括：

* 网络设置：传入 HTTP 请求的侦听端口 ( `server.port`)、要绑定的接口地址 ( `server.address`) 等。
* 错误管理：错误页面的位置（`server.error.path`）等。
* [SSL协议](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.webserver.configure-ssl)
* [HTTP 压缩](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.webserver.enable-response-compression)

Spring Boot 尝试尽可能多地公开通用设置，但这并不总是可行。对于这些情况，专用命名空间（例如`server.netty.*`提供特定于服务器的定制）。

> 请参阅[`ServerProperties`](https://github.com/spring-projects/spring-boot/tree/v3.2.0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java)课程以获取完整列表。

**程序化定制**

如果您需要以编程方式配置反应式 Web 服务器，您可以注册一个实现 `WebServerFactoryCustomizer` 接口的 Spring bean。 `WebServerFactoryCustomizer` 提供对 `ConfigurableReactiveWebServerFactory` 的访问，其中包括许多自定义 setter 方法。以下示例显示以编程方式设置端口：

```java
@Component
public class MyWebServerFactoryCustomizer implements WebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> {

    @Override
    public void customize(ConfigurableReactiveWebServerFactory server) {
        server.setPort(9000);
    }

}
```

`JettyReactiveWebServerFactory`、`NettyReactiveWebServerFactory`、`TomcatReactiveWebServerFactory`和`UndertowReactiveWebServerFactory`是它们的专用变体，`ConfigurableReactiveWebServerFactory`分别为 Jetty、Reactor Netty、Tomcat 和 Undertow 提供了额外的自定义 setter 方法。以下示例展示了如何进行自定义`NettyReactiveWebServerFactory`以提供对 Reactor Netty 特定配置选项的访问：

```java
@Component
public class MyNettyWebServerFactoryCustomizer implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory factory) {
        factory.addServerCustomizers((server) -> server.idleTimeout(Duration.ofSeconds(20)));
    }

}
```

**直接自定义 ConfigurableReactiveWebServerFactory**

对于需要从`ReactiveWebServerFactory` 扩展的更高级用例，您可以自己公开此类类型的 bean。

为许多配置选项提供了设置器。如果您需要做一些更奇特的事情，还提供了几个受保护的方法“挂钩”。详细信息请参见[源代码文档。](https://docs.spring.io/spring-boot/docs/3.2.0/api/org/springframework/boot/web/reactive/server/ConfigurableReactiveWebServerFactory.html)

> 自动配置的定制器仍然应用于您的定制工厂，因此请谨慎使用该选项。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/8.-wang-luo/8.2-fan-ying-shi-wang-luo-ying-yong-cheng-xu/8.2.2.-qian-ru-shi-fan-ying-shi-fu-wu-qi-zhi-chi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
