spring,  undertow

How to add a listener port to Undertow in Spring Boot

How to add a listener port to Undertow in Spring Boot

Super short post today. I couldn’t believe none has tried this before. We wanted to add a management port to separate traffic and healthchecks ports. But during the migration, we wanted to have both ports listening to healthchecks so we make everything back-compatible.

Once you active the Spring Boot management port, /actuator endpoints are moved there. This is the easiest way to add an extra port to the Undertow server.

@ConditionalOnProperty(name = “server.extra-port”) @Configuration @Slf4j public class UndertowConfiguration implements WebServerFactoryCustomizer {

@Value("${server.extra-port:-1}")
private int httpPort;

@Override
public void customize(@Nonnull UndertowServletWebServerFactory factory) {
    factory.addBuilderCustomizers(builder -> {
        if (httpPort > 0) {
            log.info("Using extra port {} for the traffic Undertow", httpPort);
            builder.addHttpListener(httpPort, "0.0.0.0");
        }
    });
}

}


More info: https://docs.spring.io/spring-boot/docs/2.1.10.RELEASE/reference/html/howto-embedded-web-servers.html#howto-configure-webserver

[^1]: Matt Paul Catalano https://unsplash.com/photos/0QEG_xOoY7Y