Skip to content

Commit

Permalink
Support arbitrary configuration properties file path for Sentinel (#804)
Browse files Browse the repository at this point in the history
- Support customized log and configuration properties directory
  • Loading branch information
linlinisme authored and sczyh30 committed Jul 8, 2019
1 parent c14e329 commit ff33de1
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,13 @@
*/
package com.alibaba.csp.sentinel.config;

import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

/**
* The universal local config center of Sentinel. The config is retrieved from command line arguments
Expand Down Expand Up @@ -64,7 +60,6 @@ public class SentinelConfig {
try {
initialize();
loadProps();

resolveAppType();
RecordLog.info("[SentinelConfig] Application type resolved: " + appType);
} catch (Throwable ex) {
Expand All @@ -91,49 +86,19 @@ private static void resolveAppType() {

private static void initialize() {
// Init default properties.
SentinelConfig.setConfig(CHARSET, DEFAULT_CHARSET);
SentinelConfig.setConfig(SINGLE_METRIC_FILE_SIZE, String.valueOf(DEFAULT_SINGLE_METRIC_FILE_SIZE));
SentinelConfig.setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT));
SentinelConfig.setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
SentinelConfig.setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
setConfig(CHARSET, DEFAULT_CHARSET);
setConfig(SINGLE_METRIC_FILE_SIZE, String.valueOf(DEFAULT_SINGLE_METRIC_FILE_SIZE));
setConfig(TOTAL_METRIC_FILE_COUNT, String.valueOf(DEFAULT_TOTAL_METRIC_FILE_COUNT));
setConfig(COLD_FACTOR, String.valueOf(DEFAULT_COLD_FACTOR));
setConfig(STATISTIC_MAX_RT, String.valueOf(DEFAULT_STATISTIC_MAX_RT));
}

private static void loadProps() {
// Resolve app name.
AppNameUtil.resolveAppName();
try {
String appName = AppNameUtil.getAppName();
if (appName == null) {
appName = "";
}
// We first retrieve the properties from the property file.
String fileName = LogBase.getLogBaseDir() + appName + ".properties";
File file = new File(fileName);
if (file.exists()) {
RecordLog.info("[SentinelConfig] Reading config from " + fileName);
FileInputStream fis = new FileInputStream(fileName);
Properties fileProps = new Properties();
fileProps.load(fis);
fis.close();

for (Object key : fileProps.keySet()) {
SentinelConfig.setConfig((String)key, (String)fileProps.get(key));
}
}
} catch (Throwable ioe) {
RecordLog.info(ioe.getMessage(), ioe);
Properties properties = SentinelConfigLoader.getProperties();
for (Object key : properties.keySet()) {
setConfig((String) key, (String) properties.get(key));
}

// JVM parameter override file config.
for (Map.Entry<Object, Object> entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) {
String configKey = entry.getKey().toString();
String configValue = entry.getValue().toString();
String configValueOld = getConfig(configKey);
SentinelConfig.setConfig(configKey, configValue);
if (configValueOld != null) {
RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, configValueOld, configValue);
}
}
}

/**
Expand Down Expand Up @@ -190,7 +155,7 @@ public static long singleMetricFileSize() {
return Long.parseLong(props.get(SINGLE_METRIC_FILE_SIZE));
} catch (Throwable throwable) {
RecordLog.warn("[SentinelConfig] Parse singleMetricFileSize fail, use default value: "
+ DEFAULT_SINGLE_METRIC_FILE_SIZE, throwable);
+ DEFAULT_SINGLE_METRIC_FILE_SIZE, throwable);
return DEFAULT_SINGLE_METRIC_FILE_SIZE;
}
}
Expand All @@ -200,7 +165,7 @@ public static int totalMetricFileCount() {
return Integer.parseInt(props.get(TOTAL_METRIC_FILE_COUNT));
} catch (Throwable throwable) {
RecordLog.warn("[SentinelConfig] Parse totalMetricFileCount fail, use default value: "
+ DEFAULT_TOTAL_METRIC_FILE_COUNT, throwable);
+ DEFAULT_TOTAL_METRIC_FILE_COUNT, throwable);
return DEFAULT_TOTAL_METRIC_FILE_COUNT;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.csp.sentinel.config;

import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.alibaba.csp.sentinel.util.ConfigUtil;
import com.alibaba.csp.sentinel.util.StringUtil;

import java.io.File;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArraySet;

import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator;

/**
* <p>
* class responsible for loading the Sentinel Core configuration
* </p>
*
* @author lianglin
* @since 1.7.0
*/
public class SentinelConfigLoader {


private static final String DIR_NAME = "logs" + File.separator + "csp";
private static final String USER_HOME = "user.home";

public static final String SENTINEL_CONFIG = "csp.sentinel.config.file";
private static String DEFAULT_SENTINEL_CONFIG_FILE = "classpath:sentinel.properties";


private static Properties properties = new Properties();

static {
load();
}


private static void load() {

String fileName = System.getProperty(SENTINEL_CONFIG);
if (StringUtil.isBlank(fileName)) {
fileName = DEFAULT_SENTINEL_CONFIG_FILE;
}

Properties p = ConfigUtil.loadProperties(fileName);

//old version config file
if (p == null) {
String path = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator;
fileName = path + AppNameUtil.getAppName() + ".properties";
File file = new File(fileName);
if (file.exists()) {
p = ConfigUtil.loadProperties(fileName);
}
}

if (p != null && !p.isEmpty()) {
properties.putAll(p);
}

for (Map.Entry<Object, Object> entry : new CopyOnWriteArraySet<>(System.getProperties().entrySet())) {
String configKey = entry.getKey().toString();
String newConfigValue = entry.getValue().toString();
String oldConfigValue = properties.getProperty(configKey);
properties.put(configKey, newConfigValue);
if (oldConfigValue != null) {
RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, oldConfigValue, newConfigValue);
}
}
}


public static Properties getProperties() {
return properties;
}


}
Loading

0 comments on commit ff33de1

Please sign in to comment.