Spring Boot WebFlux NettyServerCustomizer不起作用

时间:2018-04-13 21:12:35

标签: spring-mvc spring-boot spring-webflux spring-web

我的应用程序非常简单,我读过我可以使用NettyServerCustomizer来自定义NettyServer。但它不起作用,没有调用NettyServerCustomizer接口的实现。 项目由Spring Initializr生成

申请代码:

@SpringBootApplication
@RestController
@RequestMapping("/test")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping
    public Mono<String> test() {
        return Mono.just("test");
    }

    @Bean
    public NettyServerCustomizer nettyCustomizer() {
        return builder -> builder.port(9909);
    }

}

POM

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

1 个答案:

答案 0 :(得分:1)

定义NettyServerCustomizer类型的bean本身没有任何效果,您可以使用WebServerFactoryCustomizer

获得所需的结果
@Bean
public WebServerFactoryCustomizer getWebServerFactoryCustomizer() {
    WebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> customizer = factory -> factory.setPort(9909);
    return customizer;
}

另请注意,该端口也是使用属性Environmentserver.port读取的,我认为除此之外最简洁的方法是对属性值的来源有很大的灵活性。 / p>

如果您想知道为什么NettyServerCustomizer不起作用,那么在代码中进行一些挖掘就会发现这个类没有以任何方式注入NettyReactiveWebServerFactory