5

I have very simple application, I read I can use NettyServerCustomizer to customize NettyServer. But it does not work, implementation of the NettyServerCustomizer interface is not being called. Project is generated by Spring Initializr

Code of application:

@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>
Simon
  • 847
  • 1
  • 12
  • 23

1 Answers1

2

Defining a bean of type NettyServerCustomizer doesn't have any effect by itself, you can get the desired result with WebServerFactoryCustomizer

@Bean
public WebServerFactoryCustomizer getWebServerFactoryCustomizer() {
    WebServerFactoryCustomizer<ConfigurableReactiveWebServerFactory> customizer = factory -> factory.setPort(9909);
    return customizer;
}

Also note that the port is also read from the Environment using the property server.port, which I think the cleanest way besides it has a great deal of flexibility regarding the source of the property value.

If you are wondering why NettyServerCustomizer doesn't work, a bit of digging in the code reveals that this class is not injected into NettyReactiveWebServerFactory in any way.

Yazan Jaber
  • 1,850
  • 22
  • 35
  • This is fixed in Spring Boot 2.2: https://github.com/spring-projects/spring-boot/blob/78ea42314834ea17b8e53c2bc928d516a9a2db21/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryConfiguration.java#L73 – Andreas Jan 31 '20 at 19:34
  • And working a bit too well :p – PatPatPat Feb 08 '21 at 08:10
  • https://stackoverflow.com/questions/66097881/spring-webflux-grafana-nettyservercustomizer-this-query-results-in-more-tha – PatPatPat Feb 08 '21 at 08:10