5

Can anybody tell me how 2 ports (for HTTP and HTTPS) can be configured when using Spring Boot 2 and WebFlux? Any hint is appreciated!

Brian Clozel
  • 46,620
  • 12
  • 129
  • 152
Juergen Zimmermann
  • 1,652
  • 6
  • 22
  • 38

3 Answers3

9

This isn't directly supported by Spring Boot 2 yet.

But, you may be able to get it to work in a couple of ways.

By default, Spring Boot WebFlux uses Netty. If you are already configured for ssl, then Spring Boot will start up and open port 8443 (or whatever you have configured).

Then, to add 8080, you can do:

@Autowired
HttpHandler httpHandler;

WebServer http;

@PostConstruct
public void start() {
    ReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(8080);
    this.http = factory.getWebServer(this.httpHandler);
    this.http.start();
}

@PreDestroy
public void stop() {
    this.http.stop();
}

Which is a bit clunky since your https configuration is in one spot (application.yml) and your http configuration is in Java config, but I have tested this myself with a toy application. Not sure how robust of a solution it is, though.

Another option that may work is to try the equivalent of other suggestions, but use the reactive version of the class, which is TomcatReactiveWebServerFactory. I'm not aware of any way to provide more than one connector for it, but you could possibly override its getWebServer method:

@Bean
TomcatReactiveWebServerFactory twoPorts() {
    return new TomcatReactiveWebServerFactory(8443) {
        @Override
        public WebServer getWebServer(HttpHandler httpHandler) {
           // .. copy lines from parent class
           // .. and add your own Connector, similar to how tutorials describe for servlet-based support
        }
    }
}

Also, a bit messy, and I have not tried that approach myself.

Of course, keep track of the ticket so you know when Spring Boot 2 provides official support.

jzheaux
  • 4,971
  • 3
  • 15
  • 26
1

Follow the instructions listed in the link provided by jojo_berlin (here's the link). Instead of using his EmbeddedTomcatConfiguration class though, use this below

@Configuration
public class TomcatConfig {

    @Value("${server.http.port}")
    private int httpPort;

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setPort(httpPort);
        factory.addAdditionalTomcatConnectors(connector);

        return factory;
    }
}
Stefan Falk
  • 18,764
  • 34
  • 144
  • 286
jrlambs
  • 81
  • 8
0

Actually you can define a second connector as described here . So you can define a https connector as your default and an additional HTTP Connector

jojo_Berlin
  • 617
  • 1
  • 4
  • 18
  • The answer you point to won't work as is with Spring Boot 2, since the embedded containers package structure has been [refactored](https://stackoverflow.com/a/47554861/5873923). – Marc Tarin Jan 12 '18 at 09:06
  • The linked instructions are to build a servlet-based application, not webflux-based. – jzheaux Aug 30 '18 at 21:18