瀏覽代碼

Add appType property field in SentinelConfig

- The `appType` can be retrieved from `csp.sentinel.app.type` field. It's useful to identify the service type (e.g. API gateway).

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 年之前
父節點
當前提交
bb4fde58b8
共有 2 個檔案被更改,包括 53 行新增6 行删除
  1. +2
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java
  2. +51
    -6
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java

+ 2
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/Constants.java 查看文件

@@ -64,4 +64,6 @@ public final class Constants {
* The global switch for Sentinel.
*/
public static volatile boolean ON = true;

private Constants() {}
}

+ 51
- 6
sentinel-core/src/main/java/com/alibaba/csp/sentinel/config/SentinelConfig.java 查看文件

@@ -27,15 +27,25 @@ import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;

/**
* The universal config of Courier. The config is retrieved from
* {@code ${user.home}/logs/csp/${appName}.properties} by default.
* The universal local config center of Sentinel. The config is retrieved from command line arguments
* and {@code ${user.home}/logs/csp/${appName}.properties} file by default.
*
* @author leyou
* @author Eric Zhao
*/
public class SentinelConfig {

private static final Map<String, String> props = new ConcurrentHashMap<String, String>();
/**
* The default application type.
*
* @since 1.6.0
*/
public static final int APP_TYPE_COMMON = 0;

private static final Map<String, String> props = new ConcurrentHashMap<>();
private static int appType = APP_TYPE_COMMON;

public static final String APP_TYPE = "csp.sentinel.app.type";
public static final String CHARSET = "csp.sentinel.charset";
public static final String SINGLE_METRIC_FILE_SIZE = "csp.sentinel.metric.file.single.size";
public static final String TOTAL_METRIC_FILE_COUNT = "csp.sentinel.metric.file.total.count";
@@ -49,8 +59,32 @@ public class SentinelConfig {
static final int DEFAULT_STATISTIC_MAX_RT = 4900;

static {
initialize();
loadProps();
try {
initialize();
loadProps();

resolveAppType();
RecordLog.info("[SentinelConfig] Application type resolved: " + appType);
} catch (Throwable ex) {
RecordLog.warn("[SentinelConfig] Failed to initialize", ex);
ex.printStackTrace();
}
}

private static void resolveAppType() {
try {
String type = getConfig(APP_TYPE);
if (type == null) {
appType = APP_TYPE_COMMON;
return;
}
appType = Integer.parseInt(type);
if (appType < 0) {
appType = APP_TYPE_COMMON;
}
} catch (Exception ex) {
appType = APP_TYPE_COMMON;
}
}

private static void initialize() {
@@ -135,6 +169,16 @@ public class SentinelConfig {
return AppNameUtil.getAppName();
}

/**
* Get application type.
*
* @return application type, common (0) by default
* @since 1.6.0
*/
public static int getAppType() {
return appType;
}

public static String charset() {
return props.get(CHARSET);
}
@@ -162,7 +206,8 @@ public class SentinelConfig {
public static int coldFactor() {
try {
int coldFactor = Integer.parseInt(props.get(COLD_FACTOR));
if (coldFactor <= 1) {// check the cold factor larger than 1
// check the cold factor larger than 1
if (coldFactor <= 1) {
coldFactor = DEFAULT_COLD_FACTOR;
RecordLog.warn("cold factor=" + coldFactor + ", should be larger than 1, use default value: "
+ DEFAULT_COLD_FACTOR);


Loading…
取消
儲存