This is an automated email from the ASF dual-hosted git repository. szetszwo pushed a commit to branch branch-2 in repository https://gitbox.apache.org/repos/asf/ratis.git
commit 3f500ddb6681b8d9cc1e665de09baf01a030c2dc Author: lanicc <[email protected]> AuthorDate: Fri Jul 8 12:27:40 2022 +0800 RATIS-1615. Server should support listening on a specified network address (#672) --- .../org/apache/ratis/netty/NettyConfigKeys.java | 22 ++++++++++++++++++++++ .../apache/ratis/netty/server/NettyRpcService.java | 5 ++++- .../ratis/netty/server/NettyServerStreamRpc.java | 5 ++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ratis-netty/src/main/java/org/apache/ratis/netty/NettyConfigKeys.java b/ratis-netty/src/main/java/org/apache/ratis/netty/NettyConfigKeys.java index fcd7eacb..07e66216 100644 --- a/ratis-netty/src/main/java/org/apache/ratis/netty/NettyConfigKeys.java +++ b/ratis-netty/src/main/java/org/apache/ratis/netty/NettyConfigKeys.java @@ -41,9 +41,20 @@ public interface NettyConfigKeys { String PREFIX = NettyConfigKeys.PREFIX + ".server"; + String HOST_KEY = PREFIX + ".host"; + String HOST_DEFAULT = null; + String PORT_KEY = PREFIX + ".port"; int PORT_DEFAULT = 0; + static String host(RaftProperties properties) { + return get(properties::get, HOST_KEY, HOST_DEFAULT, getDefaultLog()); + } + + static void setHost(RaftProperties properties, String host) { + set(properties::set, HOST_KEY, host); + } + static int port(RaftProperties properties) { return getInt(properties::getInt, PORT_KEY, PORT_DEFAULT, getDefaultLog(), requireMin(0), requireMax(65536)); @@ -62,9 +73,20 @@ public interface NettyConfigKeys { String PREFIX = NettyConfigKeys.PREFIX + ".dataStream"; + String HOST_KEY = PREFIX + ".host"; + String HOST_DEFAULT = null; + String PORT_KEY = PREFIX + ".port"; int PORT_DEFAULT = 0; + static String host(RaftProperties properties) { + return get(properties::get, HOST_KEY, HOST_DEFAULT, getDefaultLog()); + } + + static void setHost(RaftProperties properties, String host) { + set(properties::set, HOST_KEY, host); + } + static int port(RaftProperties properties) { return getInt(properties::getInt, PORT_KEY, PORT_DEFAULT, getDefaultLog(), requireMin(0), requireMax(65536)); diff --git a/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java b/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java index 1f546873..23fd98f3 100644 --- a/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java +++ b/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java @@ -116,13 +116,16 @@ public final class NettyRpcService extends RaftServerRpcWithProxy<NettyRpcProxy, } }; + final String host = NettyConfigKeys.Server.host(server.getProperties()); final int port = NettyConfigKeys.Server.port(server.getProperties()); + InetSocketAddress socketAddress = + host == null || host.isEmpty() ? new InetSocketAddress(port) : new InetSocketAddress(host, port); this.channel = JavaUtils.memoize(() -> new ServerBootstrap() .group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(initializer) - .bind(port)); + .bind(socketAddress)); } @Override diff --git a/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyServerStreamRpc.java b/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyServerStreamRpc.java index dd79d839..311c4c5c 100644 --- a/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyServerStreamRpc.java +++ b/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyServerStreamRpc.java @@ -170,7 +170,10 @@ public class NettyServerStreamRpc implements DataStreamServerRpc { NettyConfigKeys.DataStream.Server.workerGroupSize(properties), useEpoll); final SslContext sslContext = NettyUtils.buildSslContextForServer(tlsConf); + final String host = NettyConfigKeys.DataStream.host(server.getProperties()); final int port = NettyConfigKeys.DataStream.port(properties); + InetSocketAddress socketAddress = + host == null || host.isEmpty() ? new InetSocketAddress(port) : new InetSocketAddress(host, port); this.channelFuture = new ServerBootstrap() .group(bossGroup, workerGroup) .channel(bossGroup instanceof EpollEventLoopGroup ? @@ -179,7 +182,7 @@ public class NettyServerStreamRpc implements DataStreamServerRpc { .childHandler(newChannelInitializer(sslContext)) .childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.TCP_NODELAY, true) - .bind(port); + .bind(socketAddress); } static DataStreamClient newClient(RaftPeer peer, RaftProperties properties) {
