Skip to content

Commit

Permalink
feature:add new parameter option "-e" used for setting name of config…
Browse files Browse the repository at this point in the history
…uration. (apache#1689)
  • Loading branch information
xingfudeshi committed Sep 25, 2019
1 parent 1b3b9ab commit f7b12f5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package io.seata.config;

import java.util.Objects;

import io.seata.common.exception.NotSupportYetException;
import io.seata.common.loader.EnhancedServiceLoader;

import java.util.Objects;

/**
* The type Configuration factory.
*
Expand All @@ -30,7 +30,7 @@ public final class ConfigurationFactory {
private static final String REGISTRY_CONF_PREFIX = "registry";
private static final String REGISTRY_CONF_SUFFIX = ".conf";
private static final String ENV_SYSTEM_KEY = "SEATA_ENV";
private static final String ENV_PROPERTY_KEY = "seataEnv";
public static final String ENV_PROPERTY_KEY = "seataEnv";

private static final String SYSTEM_PROPERTY_SEATA_CONFIG_NAME = "seata.config.name";

Expand All @@ -51,7 +51,7 @@ public final class ConfigurationFactory {
envValue = System.getenv(ENV_SYSTEM_KEY);
}
CURRENT_FILE_INSTANCE = (null == envValue) ? new FileConfiguration(seataConfigName + REGISTRY_CONF_SUFFIX)
: new FileConfiguration(seataConfigName + "-" + envValue + REGISTRY_CONF_SUFFIX);
: new FileConfiguration(seataConfigName + "-" + envValue + REGISTRY_CONF_SUFFIX);
}

private static final String NAME_KEY = "name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void initChannel(SocketChannel ch) {
public void shutdown() {
try {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Shuting server down. ");
LOGGER.debug("Shutting server down. ");
}
if (initialized.get()) {
RegistryFactory.getInstance().unregister(new InetSocketAddress(XID.getIpAddress(), XID.getPort()));
Expand Down
27 changes: 21 additions & 6 deletions server/src/main/java/io/seata/server/ParameterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import io.seata.config.Configuration;
import io.seata.common.util.StringUtils;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;

import static io.seata.config.ConfigurationFactory.ENV_PROPERTY_KEY;

/**
* The type Parameter parser.
*
Expand All @@ -33,10 +35,6 @@ public class ParameterParser {
private static final int SERVER_DEFAULT_PORT = 8091;
private static final String SERVER_DEFAULT_STORE_MODE = "file";
private static final int SERVER_DEFAULT_NODE = 1;
/**
* The constant CONFIG.
*/
protected static final Configuration CONFIG = ConfigurationFactory.getInstance();

@Parameter(names = "--help", help = true)
private boolean help;
Expand All @@ -45,9 +43,11 @@ public class ParameterParser {
@Parameter(names = {"--port", "-p"}, description = "The port to listen.", order = 2)
private int port = SERVER_DEFAULT_PORT;
@Parameter(names = {"--storeMode", "-m"}, description = "log store mode : file、db", order = 3)
private String storeMode = CONFIG.getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE);
private String storeMode;
@Parameter(names = {"--serverNode", "-n"}, description = "server node id, such as 1, 2, 3. default is 1", order = 4)
private int serverNode = SERVER_DEFAULT_NODE;
@Parameter(names = {"--seataEnv", "-e"}, description = "The name used for multi-configuration isolation.", order = 5)
private String seataEnv;

/**
* Instantiates a new Parameter parser.
Expand All @@ -67,6 +67,12 @@ private void init(String[] args) {
jCommander.usage();
System.exit(0);
}
if (StringUtils.isNotBlank(seataEnv)) {
System.setProperty(ENV_PROPERTY_KEY, seataEnv);
}
if(StringUtils.isBlank(storeMode)){
storeMode= ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE);
}
} catch (ParameterException e) {
printError(e);
}
Expand Down Expand Up @@ -124,4 +130,13 @@ public boolean isHelp() {
public int getServerNode() {
return serverNode;
}

/**
* Gets seata env
*
* @return the name used for multi-configuration isolation.
*/
public String getSeataEnv() {
return seataEnv;
}
}
8 changes: 5 additions & 3 deletions server/src/main/java/io/seata/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ public class Server {
* @throws IOException the io exception
*/
public static void main(String[] args) throws IOException {
//initialize the metrics
MetricsManager.get().init();

//initialize the parameter parser
//Note that the parameter parser should always be the first line to execute.
//Because, here we need to parse the parameters needed for startup.
ParameterParser parameterParser = new ParameterParser(args);

//initialize the metrics
MetricsManager.get().init();

System.setProperty(ConfigurationKeys.STORE_MODE, parameterParser.getStoreMode());

RpcServer rpcServer = new RpcServer(WORKING_THREADS);
Expand Down
10 changes: 9 additions & 1 deletion server/src/test/java/io/seata/server/ParameterParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ParameterParserTest {
*/
@BeforeEach
private void init() {
String[] args = new String[] {"-h", "127.0.0.1", "-p", "8088", "-m", "file"};
String[] args = new String[] {"-h", "127.0.0.1", "-p", "8088", "-m", "file","-e","test"};
parameterParser = new ParameterParser(args);
}

Expand Down Expand Up @@ -73,6 +73,14 @@ public void testGetStoreMode() {
Assertions.assertEquals("file", parameterParser.getStoreMode());
}

/**
* test get seata env
*/
@Test
public void testGetSeataEnv() {
Assertions.assertEquals("test", parameterParser.getSeataEnv());
}

/**
* clean up
*/
Expand Down

0 comments on commit f7b12f5

Please sign in to comment.