提交 886406b9 编写于 作者: J Jason Song

Merge pull request #51 from yiming187/conf_update

Extract domain conf into files
......@@ -4,18 +4,12 @@ import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.client.constants.Constants;
import com.ctrip.apollo.core.MetaDomainConsts;
import com.ctrip.apollo.core.utils.ResourceUtils;
import com.ctrip.apollo.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
......@@ -41,7 +35,7 @@ public class ClientEnvironment {
if (env.get() == null) {
Env resultEnv = Apollo.getEnv();
Properties apolloProperties = null;
apolloProperties = readConfigFile(DEFAULT_FILE, null);
apolloProperties = ResourceUtils.readConfigFile(DEFAULT_FILE, null);
if (apolloProperties != null) {
String strEnv = apolloProperties.getProperty(Constants.ENV);
if (!StringUtils.isBlank(strEnv)) {
......@@ -62,47 +56,4 @@ public class ClientEnvironment {
return MetaDomainConsts.getDomain(getEnv());
}
@SuppressWarnings("unchecked")
private Properties readConfigFile(String configPath, Properties defaults) {
InputStream in = this.getClass().getResourceAsStream(configPath);
logger.info("Reading config from resource {}", configPath);
Properties props = new Properties();
try {
if (in == null) {
// load outside resource under current user path
Path path = new File(System.getProperty("user.dir") + configPath).toPath();
if (Files.isReadable(path)) {
in = new FileInputStream(path.toFile());
logger.info("Reading config from file {} ", path);
}
}
if (defaults != null) {
props.putAll(defaults);
}
if (in != null) {
props.load(in);
in.close();
}
} catch (Exception ex) {
logger.warn("Reading config failed: {}", ex.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
logger.warn("Close config failed: {}", ex.getMessage());
}
}
}
StringBuilder sb = new StringBuilder();
for (Enumeration<String> e = (Enumeration<String>) props.propertyNames(); e
.hasMoreElements(); ) {
String key = e.nextElement();
String val = (String) props.getProperty(key);
sb.append(key).append('=').append(val).append('\n');
}
logger.info("Reading properties: \n" + sb.toString());
return props;
}
}
package com.ctrip.apollo.core;
import com.ctrip.apollo.Apollo.Env;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class MetaDomainConsts {
public static final String DEFAULT_PORT = "8080";
public static final String LOCAL = "http://localhost" + ":" + DEFAULT_PORT;
public static final String DEV =
"http://10.3.2.56" + ":" + DEFAULT_PORT;
public static final String FAT =
"http://ws.meta.apollo.fx.fat.nt.ctripcorp.com" + ":" + DEFAULT_PORT;
public static final String FWS =
"http://ws.meta.apollo.fx.fws.nt.ctripcorp.com" + ":" + DEFAULT_PORT;
public static final String UAT =
"http://ws.meta.apollo.fx.uat.nt.ctripcorp.com" + ":" + DEFAULT_PORT;
public static final String LPT =
"http://ws.meta.apollo.fx.lpt.nt.ctripcorp.com" + ":" + DEFAULT_PORT;
public static final String TOOLS =
"http://ws.meta.apollo.fx.tools.ctripcorp.com" + ":" + DEFAULT_PORT;
import com.ctrip.apollo.Apollo.Env;
import com.ctrip.apollo.core.utils.ResourceUtils;
public static final String PRO = "http://ws.meta.apollo.fx.ctripcorp.com" + ":" + DEFAULT_PORT;
public class MetaDomainConsts {
private static Map<Env, String> domains = new HashMap<>();
static {
domains.put(Env.LOCAL, LOCAL);
domains.put(Env.DEV, DEV);
domains.put(Env.FAT, FAT);
domains.put(Env.FWS, FWS);
domains.put(Env.UAT, UAT);
domains.put(Env.LPT, LPT);
domains.put(Env.TOOLS, TOOLS);
domains.put(Env.PRO, PRO);
Properties prop = new Properties();
prop = ResourceUtils.readConfigFile("apollo-env.properties", prop);
domains.put(Env.LOCAL, prop.getProperty("local.meta", "http://localhost:8080"));
domains.put(Env.DEV, prop.getProperty("dev.meta"));
domains.put(Env.FAT, prop.getProperty("fat.meta"));
domains.put(Env.FWS, prop.getProperty("fws.meta"));
domains.put(Env.UAT, prop.getProperty("uat.meta"));
domains.put(Env.LPT, prop.getProperty("lpt.meta"));
domains.put(Env.TOOLS, prop.getProperty("tools.meta"));
domains.put(Env.PRO, prop.getProperty("pro.meta"));
}
public static String getDomain(Env env) {
......
package com.ctrip.apollo.core.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ResourceUtils {
private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class);
@SuppressWarnings("unchecked")
public static Properties readConfigFile(String configPath, Properties defaults) {
InputStream in = ClassLoader.getSystemResourceAsStream(configPath);
logger.info("Reading config from resource {}", configPath);
Properties props = new Properties();
try {
if (in == null) {
// load outside resource under current user path
Path path = new File(System.getProperty("user.dir") + configPath).toPath();
if (Files.isReadable(path)) {
in = new FileInputStream(path.toFile());
logger.info("Reading config from file {} ", path);
}
}
if (defaults != null) {
props.putAll(defaults);
}
if (in != null) {
props.load(in);
in.close();
}
} catch (Exception ex) {
logger.warn("Reading config failed: {}", ex.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
logger.warn("Close config failed: {}", ex.getMessage());
}
}
}
StringBuilder sb = new StringBuilder();
for (Enumeration<String> e = (Enumeration<String>) props.propertyNames(); e
.hasMoreElements(); ) {
String key = e.nextElement();
String val = (String) props.getProperty(key);
sb.append(key).append('=').append(val).append('\n');
}
logger.info("Reading properties: \n" + sb.toString());
return props;
}
}
local.meta=http://localhost:8090
\ No newline at end of file
package com.ctrip.apollo.core;
import org.junit.Test;
import org.springframework.util.Assert;
import com.ctrip.apollo.Apollo.Env;
public class MetaDomainTest {
@Test
public void testDefaultDomain() {
Assert.notNull(MetaDomainConsts.getDomain(Env.LOCAL));
Assert.isNull(MetaDomainConsts.getDomain(Env.PRO));
}
}
local.meta=http://localhost:8090
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册