Spring Boot Reactor Netty Configuration – Spring Boot Reactor Netty配置

最后修改: 2019年 3月 22日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to look at different configuration options for a Reactor Netty server in a Spring Boot application. In the end, we’ll have an application showcasing different configuration approaches.

在本教程中,我们将研究Spring Boot应用中Reactor Netty服务器的不同配置选项。最后,我们将有一个应用程序展示不同的配置方法。

2. What Is Reactor Netty?

2.什么是Reactor Netty?

Before we start, let’s look at what Reactor Netty is and how it relates to Spring Boot.

在我们开始之前,让我们看看什么是Reactor Netty以及它与Spring Boot的关系。

Reactor Netty is an asynchronous event-driven network application framework. It provides non-blocking and backpressure-ready TCP, HTTP, and UDP clients and servers. As the name implies, it’s based on the Netty framework.

Reactor Netty是一个异步事件驱动的网络应用框架。它提供了非阻塞和背压就绪的TCP、HTTP和UDP客户端和服务器。顾名思义,它是基于Netty框架的。

Now, let’s see where Spring and Spring Boot come into the picture.

现在,让我们看看Spring和Spring Boot是如何进入画面的。

Spring WebFlux is a part of the Spring framework and provides reactive programming support for web applications. If we’re using WebFlux in a Spring Boot application, Spring Boot automatically configures Reactor Netty as the default server. In addition to that, we can explicitly add Reactor Netty to our project, and Spring Boot should again automatically configure it.

Spring WebFlux是 Spring 框架的一部分,为 Web 应用程序提供反应式编程支持。如果我们在 Spring Boot 应用程序中使用 WebFlux,Spring Boot 自动配置 Reactor Netty 为默认服务器。除此之外,我们可以明确地将Reactor Netty添加到我们的项目中,Spring Boot应该再次自动配置它。

Now, we’ll build an application to learn how we can customize our auto-configured Reactor Netty server. After that, we’ll cover some common configuration scenarios.

现在,我们将建立一个应用程序来学习如何定制我们自动配置的Reactor Netty服务器。之后,我们将介绍一些常见的配置情况。

3. Dependencies

3.依赖性

Firstly, we’ll add the required Maven dependency.

首先,我们要添加所需的Maven依赖性。

To use the Reactor Netty server, we will add the spring-boot-starter-webflux as a dependency in our pom file:

为了使用Reactor Netty服务器,我们将在pom文件中添加spring-boot-starter-webflux作为一个依赖项。

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

This will also pull in spring-boot-starter-reactor-netty as a transitive dependency into our project.

这也将把spring-boot-starter-reactor-netty作为一个过渡性依赖拉入我们的项目。

4. Server Configuration

4.服务器配置

4.1. Using Properties Files

4.1.使用属性文件

As the first option, we can configure the Netty server through properties files. Spring Boot exposes some of the common server configurations in the application properties file:

作为第一个选择,我们可以通过属性文件来配置Netty服务器。Spring Boot在application属性文件中公开了一些常见的服务器配置。

Let’s define the server port in application.properties:

让我们在application.properties中定义服务器端口。

server.port=8088

Or we could have done the same in application.yml:

或者我们可以在application.yml中做同样的事情。

server:
    port: 8088

Besides the server port, Spring Boot has many other available server configuration optionsThe properties that start with the server prefix let us override the default server configuration. We can easily look up these properties in the Spring documentation under the EMBEDDED SERVER CONFIGURATION section.

除了服务器端口,Spring Boot 还有许多其他可用的服务器配置选项server前缀开头的属性 让我们覆盖默认的服务器配置。我们可以在Spring 文档中的EMBEDDED SERVER CONFIGURATION 部分轻松查找这些属性。

4.2. Using Programmatic Configuration

4.2.使用程序化配置

Now, let’s look at how we can configure our embedded Netty server through code. For this purpose, Spring Boot gives us the WebServerFactoryCustomizer and NettyServerCustomizer classes.

现在,让我们看看我们如何通过代码配置我们的嵌入式Netty服务器。为此,Spring Boot为我们提供了WebServerFactoryCustomizerNettyServerCustomizer类。

Let’s use these classes to configure the Netty port as we did previously with our properties file:

让我们使用这些类来配置Netty端口,就像我们之前在properties文件中做的那样

@Component
public class NettyWebServerFactoryPortCustomizer 
  implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {

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

Spring Boot will pick up our factory customizer component during startup and will configure the server port.

Spring Boot会在启动时接收我们的工厂定制器组件,并配置服务器端口。

Alternatively, we can implement NettyServerCustomizer:

另外,我们可以实现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);
    }
}

And add it to the server factory:

并将其添加到服务器工厂。

serverFactory.addServerCustomizers(new PortCustomizer(8088));

These two approaches give us a lot of flexibility when configuring our embedded Reactor Netty server.

这两种方法在配置我们的嵌入式Reactor Netty服务器时给了我们很大的灵活性。

Furthermore, we can also customize the EventLoopGroup:

此外,我们还可以自定义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);
    }
}

However, there is a caveat for this case. Since Spring Boot auto-configures the Netty server, we may need to skip auto-configuration by explicitly defining our NettyReactiveWebServerFactory bean.

然而,在这种情况下有一个注意事项。由于Spring Boot会自动配置Netty服务器,我们可能需要通过明确定义我们的NettyReactiveWebServerFactory bean来跳过自动配置。

For this purpose, we should define our bean in a configuration class and add our customizer there:

为此,我们应该在一个配置类中定义我们的Bean,并在那里添加我们的自定义器。

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

Next, we’ll continue with some common Netty configuration scenarios.

接下来,我们将继续介绍一些常见的Netty配置情况。

5. SSL Configuration

5.SSL配置

Let’s see how we can configure SSL.

让我们看看如何配置SSL。

We’ll use the SslServerCustomizer class which is another implementation of NettyServerCustomizer:

我们将使用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);
    }
}

Here we’ve defined our keystore related properties, disabled HTTP/2, and set the port to 8443.

在这里,我们定义了我们的钥匙库相关属性,禁用了HTTP/2,并将端口设置为8443。

6. Access Log Configuration

6.访问日志配置

Now, we’ll look at how we can configure access logging using Logback.

现在,我们来看看如何配置访问日志使用Logback

Spring Boot lets us configure access logging in the application properties file for Tomcat, Jetty, and Undertow. However, Netty does not have this support just yet.

Spring Boot允许我们在Tomcat、Jetty和Undertow的应用程序属性文件中配置访问日志。但是,Netty还没有这种支持。

To enable Netty access logging, we should set -Dreactor.netty.http.server.accessLogEnabled=true when running our application:

要启用Netty访问日志,我们应该在运行应用程序时设置 -Dreactor.netty.http.server.accessLogEnabled=true

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

7. Conclusion

7.结语

In this article, we’ve covered how to configure the Reactor Netty server in a Spring Boot application.

在这篇文章中,我们已经介绍了如何在Spring Boot应用程序中配置Reactor Netty服务器。

Firstly, we used the general Spring Boot property-based configuration capabilities. And then, we explored how to programmatically configure Netty in a fine-grained manner.

首先,我们使用了Spring Boot基于属性的一般配置功能。然后,我们探讨了如何以细粒度的方式对Netty进行编程配置。

Finally, the source code for this article is available on Github.

最后,本文的源代码可在Github上获得