You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2022/07/09 03:15:07 UTC
[shardingsphere] branch master updated: listen on specified addresses (#18893)
This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1cdbb17fffc listen on specified addresses (#18893)
1cdbb17fffc is described below
commit 1cdbb17fffc7b6aa9760c5188d281980788cc997
Author: csonezp <cs...@gmail.com>
AuthorDate: Sat Jul 9 11:15:00 2022 +0800
listen on specified addresses (#18893)
* modify start.sh
* listen on specified addresses
* fix Spelling
* fix usage
* fix bug
---
.../src/main/resources/bin/start.sh | 65 ++++++++++++++++++----
.../org/apache/shardingsphere/proxy/Bootstrap.java | 4 +-
.../proxy/arguments/BootstrapArguments.java | 23 +++++++-
.../proxy/arguments/BootstrapArgumentsTest.java | 43 +++++++++++++-
.../proxy/frontend/ShardingSphereProxy.java | 27 ++++++---
5 files changed, 137 insertions(+), 25 deletions(-)
diff --git a/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh b/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
index b14a6b35d8a..e1d9f50b0da 100644
--- a/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
+++ b/shardingsphere-distribution/shardingsphere-proxy-distribution/src/main/resources/bin/start.sh
@@ -61,10 +61,23 @@ JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn1g -Xss1m -XX:AutoBoxCacheMax=4096 -XX
MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap
+unset -v PORT
+unset -v ADDRESSES
+unset -v CONF_PATH
+
print_usage() {
- echo "usage: start.sh [port] [config_dir]"
+ echo "usage:"
+ echo "start.sh [port] [config_dir]"
echo " port: proxy listen port, default is 3307"
- echo " config_dir: proxy config directory, default is conf"
+ echo " config_dir: proxy config directory, default is 'conf'"
+ echo ""
+ echo "start.sh [-a addresses] [-p port] [-c /path/to/conf]"
+ echo "The options are unordered."
+ echo "-a Bind addresses, can be IPv4, IPv6, hostname. In"
+ echo " case more than one address is specified in a"
+ echo " comma-separated list. The default value is '0.0.0.0'."
+ echo "-p Bind port, default is '3307', which could be changed in server.yaml"
+ echo "-c Path to config directory of ShardingSphere-Proxy, default is 'conf'"
exit 0
}
@@ -81,26 +94,54 @@ if [ "$1" == "-v" ] || [ "$1" == "--version" ] ; then
print_version
fi
-echo "Starting the $SERVER_NAME ..."
-
if [ $# == 0 ]; then
CLASS_PATH=${DEPLOY_DIR}/conf:${CLASS_PATH}
fi
-if [ $# == 1 ]; then
- MAIN_CLASS=${MAIN_CLASS}" "$1
+if [[ $1 == -a ]] || [[ $1 == -p ]] || [[ $1 == -c ]] ; then
+ while getopts ":a:p:c:" opt
+ do
+ case $opt in
+ a)
+ echo "The address is $OPTARG"
+ ADDRESSES=$OPTARG;;
+ p)
+ echo "The port is $OPTARG"
+ PORT=$OPTARG;;
+ c)
+ echo "The configuration path is $OPTARG"
+ CONF_PATH=$OPTARG;;
+ ?)
+ print_usage;;
+ esac
+
+ done
+
+elif [ $# == 1 ]; then
+ PORT=$1
echo "The port is $1"
- CLASS_PATH=${DEPLOY_DIR}/conf:${CLASS_PATH}
-fi
-if [ $# == 2 ]; then
- MAIN_CLASS=${MAIN_CLASS}" "$1" "$2
+elif [ $# == 2 ]; then
+ PORT=$1
+ CONF_PATH=$2
echo "The port is $1"
- echo "The configuration path is $DEPLOY_DIR/$2"
- CLASS_PATH=${DEPLOY_DIR}/$2:${CLASS_PATH}
+ echo "The configuration path is $2"
fi
+if [ -z "$CONF_PATH" ]; then
+ CONF_PATH=${DEPLOY_DIR}/conf
+fi
+
+if [ -z "$PORT" ]; then
+ PORT=-1
+fi
+
+CLASS_PATH=${CONF_PATH}:${CLASS_PATH}
+MAIN_CLASS=${MAIN_CLASS}" "${PORT}" "${CONF_PATH}" "${ADDRESSES}
+
+echo "Starting the $SERVER_NAME ..."
echo "The classpath is ${CLASS_PATH}"
+echo "main class ${MAIN_CLASS}"
nohup java ${JAVA_OPTS} ${JAVA_MEM_OPTS} -classpath ${CLASS_PATH} ${MAIN_CLASS} >> ${STDOUT_FILE} 2>&1 &
sleep 1
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
index b28e82a1cf8..9a54374bca0 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.proxy.initializer.BootstrapInitializer;
import java.io.IOException;
import java.sql.SQLException;
+import java.util.List;
/**
* ShardingSphere-Proxy Bootstrap.
@@ -47,7 +48,8 @@ public final class Bootstrap {
BootstrapArguments bootstrapArgs = new BootstrapArguments(args);
YamlProxyConfiguration yamlConfig = ProxyConfigurationLoader.load(bootstrapArgs.getConfigurationPath());
int port = bootstrapArgs.getPort().orElseGet(() -> new ConfigurationProperties(yamlConfig.getServerConfiguration().getProps()).getValue(ConfigurationPropertyKey.PROXY_DEFAULT_PORT));
+ List<String> addresses = bootstrapArgs.getAddresses();
new BootstrapInitializer().init(yamlConfig, port);
- new ShardingSphereProxy().start(port);
+ new ShardingSphereProxy().start(port, addresses);
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
index 095029091ec..c5adc625a15 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/arguments/BootstrapArguments.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.proxy.arguments;
import lombok.RequiredArgsConstructor;
+import java.util.Arrays;
+import java.util.List;
import java.util.Optional;
/**
@@ -29,11 +31,13 @@ public final class BootstrapArguments {
private static final String DEFAULT_CONFIG_PATH = "/conf/";
+ private static final String DEFAULT_BIND_ADDRESS = "0.0.0.0";
+
private final String[] args;
/**
* Get port.
- *
+ *
* @return port
*/
public Optional<Integer> getPort() {
@@ -41,7 +45,11 @@ public final class BootstrapArguments {
return Optional.empty();
}
try {
- return Optional.of(Integer.parseInt(args[0]));
+ int port = Integer.parseInt(args[0]);
+ if (port < 0) {
+ return Optional.empty();
+ }
+ return Optional.of(port);
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException(String.format("Invalid port `%s`.", args[0]));
}
@@ -49,13 +57,22 @@ public final class BootstrapArguments {
/**
* Get configuration path.
- *
+ *
* @return configuration path
*/
public String getConfigurationPath() {
return args.length < 2 ? DEFAULT_CONFIG_PATH : paddingWithSlash(args[1]);
}
+ /**
+ * Get bind address list.
+ *
+ * @return address list
+ */
+ public List<String> getAddresses() {
+ return args.length < 3 ? Arrays.asList(DEFAULT_BIND_ADDRESS) : Arrays.asList(args[2].split(","));
+ }
+
private String paddingWithSlash(final String pathArg) {
StringBuilder result = new StringBuilder(pathArg);
if (!pathArg.startsWith("/")) {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
index 368b7e20800..db601348a4a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/arguments/BootstrapArgumentsTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.proxy.arguments;
import org.junit.Test;
+import java.util.Arrays;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
@@ -38,12 +39,31 @@ public final class BootstrapArgumentsTest {
new BootstrapArguments(new String[]{"WrongArgument"}).getPort();
}
+ @Test
+ public void assertGetPortWithDefaultArgument() {
+ assertFalse(new BootstrapArguments(new String[]{"-1"}).getPort().isPresent());
+ }
+
@Test
public void assertGetPortWithSingleArgument() {
Optional<Integer> actual = new BootstrapArguments(new String[]{"3306"}).getPort();
assertTrue(actual.isPresent());
assertThat(actual.get(), is(3306));
}
+
+ @Test
+ public void assertGetPortWithTwoArgument() {
+ Optional<Integer> actual = new BootstrapArguments(new String[]{"3306", "/test_conf/"}).getPort();
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is(3306));
+ }
+
+ @Test
+ public void assertGetPortWithThreeArgument() {
+ Optional<Integer> actual = new BootstrapArguments(new String[]{"3306", "/test_conf/", "127.0.0.1"}).getPort();
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is(3306));
+ }
@Test
public void assertGetConfigurationPathWithEmptyArgument() {
@@ -54,7 +74,7 @@ public final class BootstrapArgumentsTest {
public void assertGetConfigurationPathWithSingleArgument() {
assertThat(new BootstrapArguments(new String[]{"3306"}).getConfigurationPath(), is("/conf/"));
}
-
+
@Test
public void assertGetConfigurationPathWithTwoArguments() {
assertThat(new BootstrapArguments(new String[]{"3306", "test_conf"}).getConfigurationPath(), is("/test_conf/"));
@@ -62,4 +82,25 @@ public final class BootstrapArgumentsTest {
assertThat(new BootstrapArguments(new String[]{"3306", "test_conf/"}).getConfigurationPath(), is("/test_conf/"));
assertThat(new BootstrapArguments(new String[]{"3306", "/test_conf/"}).getConfigurationPath(), is("/test_conf/"));
}
+
+ @Test
+ public void assertGetAddressesWithEmptyArgument() {
+ assertThat(new BootstrapArguments(new String[]{}).getAddresses(), is(Arrays.asList("0.0.0.0")));
+ }
+
+ @Test
+ public void assertGetAddressesWithSingleArgument() {
+ assertThat(new BootstrapArguments(new String[]{"3306"}).getAddresses(), is(Arrays.asList("0.0.0.0")));
+ }
+
+ @Test
+ public void assertGetAddressesWithTwoArgument() {
+ assertThat(new BootstrapArguments(new String[]{"3306", "test_conf"}).getAddresses(), is(Arrays.asList("0.0.0.0")));
+ }
+
+ @Test
+ public void assertGetAddressesWithThreeArguments() {
+ assertThat(new BootstrapArguments(new String[]{"3306", "test_conf", "127.0.0.1"}).getAddresses(), is(Arrays.asList("127.0.0.1")));
+ assertThat(new BootstrapArguments(new String[]{"3306", "test_conf", "1.1.1.1,127.0.0.1"}).getAddresses(), is(Arrays.asList("1.1.1.1", "127.0.0.1")));
+ }
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
index 66c904f334f..62ff0b88a88 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/ShardingSphereProxy.java
@@ -39,6 +39,9 @@ import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.frontend.netty.ServerHandlerInitializer;
import org.apache.shardingsphere.proxy.frontend.protocol.FrontDatabaseProtocolTypeFactory;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* ShardingSphere-Proxy.
*/
@@ -52,13 +55,14 @@ public final class ShardingSphereProxy {
/**
* Start ShardingSphere-Proxy.
*
- * @param port port
+ * @param port port
+ * @param addresses addresses
*/
@SneakyThrows(InterruptedException.class)
- public void start(final int port) {
+ public void start(final int port, final List<String> addresses) {
try {
- ChannelFuture future = startInternal(port);
- accept(future);
+ List<ChannelFuture> futures = startInternal(port, addresses);
+ accept(futures);
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
@@ -66,16 +70,23 @@ public final class ShardingSphereProxy {
}
}
- private ChannelFuture startInternal(final int port) throws InterruptedException {
+ private List<ChannelFuture> startInternal(final int port, final List<String> addresses) throws InterruptedException {
createEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
initServerBootstrap(bootstrap);
- return bootstrap.bind(port).sync();
+
+ List<ChannelFuture> futures = new ArrayList<>();
+ for (String address : addresses) {
+ futures.add(bootstrap.bind(address, port).sync());
+ }
+ return futures;
}
- private void accept(final ChannelFuture future) throws InterruptedException {
+ private void accept(final List<ChannelFuture> futures) throws InterruptedException {
log.info("ShardingSphere-Proxy {} mode started successfully", ProxyContext.getInstance().getContextManager().getInstanceContext().getModeConfiguration().getType());
- future.channel().closeFuture().sync();
+ for (ChannelFuture future : futures) {
+ future.channel().closeFuture().sync();
+ }
}
private void createEventLoopGroup() {