知識庫 / Spring / Spring Boot RSS 訂閱

Spring Boot Reactor Netty 配置

Reactive,Spring Boot
HongKong
4
01:26 PM · Dec 06 ,2025

1. 概述

在本教程中,我們將探討 Spring Boot 應用程序中 Reactor Netty 服務器的不同配置選項。最終,我們將擁有一個展示不同配置方法的應用程序。

2. 什麼是 Reactor Netty?

在開始之前,讓我們先了解什麼是 Reactor Netty 以及它與 Spring Boot 的關係。

Reactor Netty 是一個 異步事件驅動的網絡應用程序框架。 它提供非阻塞和具備反壓機制的 TCP、HTTP 和 UDP 客户端和服務器。正如其名稱所示,它基於 Netty 框架。

現在,讓我們看看 Spring 和 Spring Boot 在其中扮演的角色。

Spring WebFlux 是 Spring 框架的一部分,為 Web 應用程序提供反應式編程支持。 如果我們在 Spring Boot 應用程序中使用 WebFlux,Spring Boot自動配置 Reactor Netty 作為默認服務器。 此外,我們還可以顯式地將 Reactor Netty 添加到我們的項目中,Spring Boot 再次會自動配置 它。

現在,我們將構建一個應用程序,以瞭解如何自定義我們的自動配置的 Reactor Netty 服務器。 之後,我們將涵蓋一些常見的配置場景。

3. 依賴項

首先,我們將添加所需的 Maven 依賴項。

為了使用 Reactor Netty 服務器,我們將作為依賴項在 pom 文件中添加 spring-boot-starter-webflux

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

這還將引入 spring-boot-starter-reactor-netty 作為傳遞依賴到我們的項目中。

4. 服務器配置

This section describes the configuration options available for the server. Proper server configuration is crucial for optimal performance and security.

4.1 Network Settings

  • IP Address: The server's IP address. This must be a static IP address to ensure consistent access.
  • Subnet Mask: Defines the network range the server belongs to.
  • Gateway: The IP address of the router that allows the server to communicate with external networks.
  • DNS Servers: The IP addresses of the DNS servers used to resolve domain names to IP addresses.

4.2 Application Settings

  • Port: The port number on which the application listens for incoming connections. The default port is 8080.
  • Timeout: The maximum time (in seconds) that the server will wait for a response from a client.
  • Logging Level: Controls the verbosity of the server logs. Options include: DEBUG, INFO, WARNING, ERROR, FATAL.

4.3 Security Settings

  • Firewall: Configure the firewall to restrict access to the server.
  • SSL/TLS: Enable SSL/TLS encryption to secure communication between the server and clients.
  • User Authentication: Implement user authentication to control access to the server.

4.1. 使用屬性文件

作為首選方案,我們可以通過屬性文件配置 Netty 服務器。Spring Boot 會在 application 屬性文件中暴露一些常見的服務器配置:

讓我們在 application.properties 中定義服務器端口:

server.port=8088

或者,我們也可以在 application.yml 中完成同樣的操作:

server:
    port: 8088

除了服務器端口之外,Spring Boot 還提供了許多其他可用的服務器配置選項。擁有 server 前綴的屬性,使我們能夠覆蓋默認的服務器配置。我們可以輕鬆地在 Spring 文檔嵌入式服務器配置 部分中查找這些屬性。

4.2. 使用程序化配置

現在,讓我們看看如何通過代碼配置我們的嵌入式 Netty 服務器。為此,Spring Boot 提供了 WebServerFactoryCustomizerNettyServerCustomizer 類。

讓我們使用這些類,就像我們之前使用屬性文件一樣,將 Netty 端口配置為我們之前所做的那樣:

@Component
public class NettyWebServerFactoryPortCustomizer 
  implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory serverFactory) {
        serverFactory.setPort(8088);
    }
}

Spring Boot 在啓動時會拾取我們的工廠定製器組件,並配置服務器端口。

或者,我們可以實現 NettyServerCustomizer

private static class PortCustomizer implements NettyServerCustomizer {
    private final int port;

    private PortCustomizer(int port) {
        this.port = port;
    }
    @Override
    public HttpServer apply(HttpServer httpServer) {
        return httpServer.port(port);
    }
}

將它添加到服務器工廠中:

serverFactory.addServerCustomizers(new PortCustomizer(8088));

這兩種方法在配置我們的嵌入式 Reactor Netty 服務器時,給我們提供了很大的靈活性。

此外,我們還可以自定義 EventLoopGroup

private static class EventLoopNettyCustomizer implements NettyServerCustomizer {

    @Override
    public HttpServer apply(HttpServer httpServer) {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        eventLoopGroup.register(new NioServerSocketChannel());
        return httpServer.runOn(eventLoopGroup);
    }
}

當然,以下是翻譯後的內容:

然而,在特定情況下存在一個注意事項。由於 Spring Boot 自動配置了 Netty 服務器,因此我們可能需要通過顯式定義 NettyReactiveWebServerFactory bean 來跳過自動配置。

為了實現此目的,我們應該在配置類中定義 bean 並將其添加到該類中。

@Bean
public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
    NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
    webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer());
    return webServerFactory;
}

接下來,我們將繼續探討一些常見的 Netty 配置場景。

5. SSL 配置

讓我們看看如何配置 SSL。

我們將使用 SslServerCustomizer 類,它是 NettyServerCustomizer 的另一種實現。

@Component
public class NettyWebServerFactorySslCustomizer 
  implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

    @Override
    public void customize(NettyReactiveWebServerFactory serverFactory) {
        Ssl ssl = new Ssl();
        ssl.setEnabled(true);
        ssl.setKeyStore("classpath:sample.jks");
        ssl.setKeyAlias("alias");
        ssl.setKeyPassword("password");
        ssl.setKeyStorePassword("secret");
        Http2 http2 = new Http2();
        http2.setEnabled(false);
        serverFactory.addServerCustomizers(new SslServerCustomizer(ssl, http2, null));
        serverFactory.setPort(8443);
    }
}

我們已定義了與 keystore 相關的屬性,禁用了 HTTP/2,並將端口設置為 8443。

6. 訪問日誌配置

現在,我們將探討如何使用 Logback 配置訪問日誌。

Spring Boot 允許我們通過 Tomcat、Jetty 和 Undertow 的應用程序屬性文件中配置訪問日誌。但是,Netty 目前尚不支持此功能。

為了啓用 Netty 訪問日誌,我們應該在運行應用程序時設置 –Dreactor.netty.http.server.accessLogEnabled=true

mvn spring-boot:run -Dreactor.netty.http.server.accessLogEnabled=true

7. 結論

在本文中,我們介紹瞭如何在 Spring Boot 應用程序中配置 Reactor Netty 服務器的方法。

首先,我們使用了 Spring Boot 通用的基於屬性的配置能力。然後,我們探討了如何以精細的方式通過編程方式配置 Netty。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.