diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServer.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServer.java index d2255b36094..e7068618526 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServer.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServer.java @@ -26,6 +26,8 @@ import org.springframework.http.server.reactive.HttpHandler; */ public interface HttpServer extends InitializingBean, Lifecycle { + void setHost(String host); + void setPort(int port); void setHandler(HttpHandler handler); diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServerSupport.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServerSupport.java index e498af70e6e..bb710f40b4d 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServerSupport.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/HttpServerSupport.java @@ -17,23 +17,37 @@ package org.springframework.http.server.reactive.boot; +import org.springframework.beans.factory.InitializingBean; import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.util.SocketUtils; /** * @author Rossen Stoyanchev */ public class HttpServerSupport { + private String host = "0.0.0.0"; + private int port = -1; private HttpHandler httpHandler; + public void setHost(String host) { + this.host = host; + } + + public String getHost() { + return host; + } public void setPort(int port) { this.port = port; } public int getPort() { + if(this.port == -1) { + this.port = SocketUtils.findAvailableTcpPort(8080); + } return this.port; } diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/JettyHttpServer.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/JettyHttpServer.java index f462a8ad207..51837739ce1 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/JettyHttpServer.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/JettyHttpServer.java @@ -24,7 +24,6 @@ import org.eclipse.jetty.servlet.ServletHolder; import org.springframework.beans.factory.InitializingBean; import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; import org.springframework.util.Assert; -import org.springframework.util.SocketUtils; /** * @author Rossen Stoyanchev @@ -44,10 +43,6 @@ public class JettyHttpServer extends HttpServerSupport implements InitializingBe @Override public void afterPropertiesSet() throws Exception { - if (getPort() == -1) { - setPort(SocketUtils.findAvailableTcpPort(8080)); - } - this.jettyServer = new Server(); Assert.notNull(getHttpHandler()); @@ -59,6 +54,7 @@ public class JettyHttpServer extends HttpServerSupport implements InitializingBe contextHandler.addServlet(servletHolder, "/"); ServerConnector connector = new ServerConnector(this.jettyServer); + connector.setHost(getHost()); connector.setPort(getPort()); this.jettyServer.addConnector(connector); } diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/ReactorHttpServer.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/ReactorHttpServer.java index b83e2d0fa05..8bb845a534b 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/ReactorHttpServer.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/ReactorHttpServer.java @@ -39,9 +39,7 @@ public class ReactorHttpServer extends HttpServerSupport Assert.notNull(getHttpHandler()); this.reactorHandler = new ReactorHttpHandlerAdapter(getHttpHandler()); - - this.reactorServer = (getPort() != -1 ? reactor.io.netty.http.HttpServer.create(getPort()) : - reactor.io.netty.http.HttpServer.create()); + this.reactorServer = reactor.io.netty.http.HttpServer.create(getHost(), getPort()); } @Override diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/RxNettyHttpServer.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/RxNettyHttpServer.java index d73fe0385f0..11171996845 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/RxNettyHttpServer.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/RxNettyHttpServer.java @@ -16,6 +16,8 @@ package org.springframework.http.server.reactive.boot; +import java.net.InetSocketAddress; + import io.netty.buffer.ByteBuf; import org.springframework.http.server.reactive.RxNettyHttpHandlerAdapter; @@ -38,9 +40,8 @@ public class RxNettyHttpServer extends HttpServerSupport implements HttpServer { Assert.notNull(getHttpHandler()); this.rxNettyHandler = new RxNettyHttpHandlerAdapter(getHttpHandler()); - this.rxNettyServer = (getPort() != -1 ? - io.reactivex.netty.protocol.http.server.HttpServer.newServer(getPort()) : - io.reactivex.netty.protocol.http.server.HttpServer.newServer()); + this.rxNettyServer = io.reactivex.netty.protocol.http.server.HttpServer + .newServer(new InetSocketAddress(getHost(), getPort())); } diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/TomcatHttpServer.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/TomcatHttpServer.java index 90c75057c88..d0170d890b5 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/TomcatHttpServer.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/TomcatHttpServer.java @@ -20,6 +20,7 @@ import java.io.File; import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; +import org.apache.catalina.core.StandardHost; import org.apache.catalina.startup.Tomcat; import org.springframework.beans.factory.InitializingBean; @@ -46,11 +47,8 @@ public class TomcatHttpServer extends HttpServerSupport implements InitializingB @Override public void afterPropertiesSet() throws Exception { - if (getPort() == -1) { - setPort(SocketUtils.findAvailableTcpPort(8080)); - } - this.tomcatServer = new Tomcat(); + this.tomcatServer.setHostname(getHost()); this.tomcatServer.setPort(getPort()); Assert.notNull(getHttpHandler()); diff --git a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/UndertowHttpServer.java b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/UndertowHttpServer.java index 66d92e2c257..5c6048353c3 100644 --- a/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/UndertowHttpServer.java +++ b/spring-web-reactive/src/main/java/org/springframework/http/server/reactive/boot/UndertowHttpServer.java @@ -44,8 +44,7 @@ public class UndertowHttpServer extends HttpServerSupport implements HttpServer Assert.notNull(getHttpHandler()); HttpHandler handler = new UndertowHttpHandlerAdapter(getHttpHandler(), dataBufferFactory); - int port = (getPort() != -1 ? getPort() : 8080); - this.server = Undertow.builder().addHttpListener(port, "localhost") + this.server = Undertow.builder().addHttpListener(getPort(), getHost()) .setHandler(handler).build(); }