我的代码:
环境类:
public class Environment {
public static final String ENV_PRODUCT = "product";
public static final String ENV_PRELEASE = "prelease";
public static final String ENV_PERFORMANCE = "performance";
public static final String ENV_QA = "qa";
public static final String ENV_ALPHA = "alpha";
public static final String ENV_DEV = "dev";
public static final String ENV_TEST = "test";
public static final String ENV_PPE = "ppe";
public static final String ENV_STAGING = "staging";
public static final String ENV_PROD = "prod";
private static Properties props;
private static String appName;
private static Appenv appenv;
static {
try {
props = PropertiesUtils.loadFromClassPath("META-INF/app.properties");
if (props == null) {
props = PropertiesUtils.loadFromFileSystem("/data/webapps/config/app.properties");
}
} catch (Throwable e) {
System.err.println("failed to load app.properties");
e.printStackTrace();
}
if (props != null) {
appName = props.getProperty("lion.app.name");
if(StringUtils.isEmpty(appName)) {
appName = props.getProperty("app.name");
}
appName = StringUtils.trimToNull(appName);
} else {
props = new Properties();
}
appenv = EnvironmentLoader.loadAppenv();
}
public static String getProperty(String key) {
return props.getProperty(key);
}
public static String getAppName() {
return appName;
}
/**
* cell value means machine's "SET" info
* @return
*/
public static String getCell() {
return MetaConfigManager.getInstance().getSet() == null ? appenv.getSet()
: MetaConfigManager.getInstance().getSet();
}
}
MetaConfigManager类:
public class MetaConfigManager {
private static final Logger logger = LoggerFactory.getLogger(MetaConfigManager.class);
private static MetaConfigManager metaConfigManager = new MetaConfigManager();
private String metaConfigDir = Constants.DEFAULT_META_CONFIG_BASE_DIR + "/" + Environment.getEnvironment();
private ConcurrentMap<String, String> metaConfig;
private Monitor monitor = MonitorLoader.getMonitor();
private boolean persistEnabled = true;
private String metaBaseUrl = System.getProperty("lion.meta.url",
LionEnvHttpServersUtil.getLionMetaHost());
private Random random = new Random();
private ExecutorService refreshExecutor;
private Set<MetaConfigListener> listeners = new HashSet<MetaConfigListener>();
public static MetaConfigManager getInstance() {
return metaConfigManager;
}
//jvm启动参数指定的meta配置文件,优先加载,不会被meta服务器上的配置刷新覆盖
private ConcurrentMap<String, String> customMetaConfig;
private MetaConfigManager() {
init();
startMetaSyncTask();
}
private void init() {
try{
customMetaConfig = getMetaFromCustomConfig();
metaConfig = getDefaultMeta();
// load local config
Properties properties = EnvironmentLoader.loadLocalAppenv();
for (String key : metaConfig.keySet()) {
String value = StringUtils.trimToNull(properties.getProperty(key));
if (!StringUtils.isEmpty(value)) {
metaConfig.put(key, value);
}
}
// load last config
persistEnabled = FileUtils.ensureAccess(metaConfigDir);
if (persistEnabled) {
for (String key : metaConfig.keySet()) {
try {
String value = FileUtils.read(metaConfigDir + File.separatorChar + key);
value = StringUtils.trimToNull(value);
if (null != value){
metaConfig.put(key, value);
}
} catch (Exception e) {
logger.warn(e.getMessage());
}
}
}
// load remote
loadMeta();
}catch (Throwable e){
logger.error("Lion MetaConfigManager init error:",e);
throw e;
}
}
}
我从其他类调用Environment.getCell(),但是在某些情况下会抛出NPE: java.lang.NullPointerException:空 在com.dianping.lion.Environment.getCell(Environment.java:89)〜[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.util.TagUtils.getTag(TagUtils.java:17)〜[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.http.ProjectManager.readConfigCache(ProjectManager.java:168)〜[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.http.ProjectManager.get(ProjectManager.java:113)〜[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.http.HttpConfigLoader.doGet(HttpConfigLoader.java:73)〜[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.http.HttpConfigLoader.get(HttpConfigLoader.java:65)〜[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.ConfigLoaderManager.get(ConfigLoaderManager.java:74)[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.ConfigManagerImpl.doGet(ConfigManagerImpl.java:135)[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.AbstractConfigManager.get(AbstractConfigManager.java:44)上[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.AbstractConfigManager.get(AbstractConfigManager.java:35)上[lion-client-0.8.6.5.jar :?] 在com.dianping.lion.client.Lion.get(Lion.java:30)[lion-client-0.8.6.5.jar :?] 在com.dianping.squirrel.common.config.LionConfigManager.doGetProperty(LionConfigManager.java:36)上[squirrel-client-2.3.8.3.jar :?] 在com.dianping.squirrel.common.config.AbstractConfigManager.getProperty(AbstractConfigManager.java:137)上[squirrel-client-2.3.8.3.jar :?] 在com.dianping.squirrel.common.config.AbstractConfigManager.getBooleanValue(AbstractConfigManager.java:58)上[squirrel-client-2.3.8.3.jar :?] 在com.dianping.squirrel.common.config.AbstractConfigManager.getBooleanValue(AbstractConfigManager.java:53)上[squirrel-client-2.3.8.3.jar :?] 在com.dianping.squirrel.client.impl.memcached.NodeMonitor。(NodeMonitor.java:33)[squirrel-client-2.3.8.3.jar :?] 在sun.misc.Unsafe.ensureClassInitialized(本机方法)〜[?:1.7.0_76]
谁能告诉我为什么?