Sentinel icon indicating copy to clipboard operation
Sentinel copied to clipboard

[BUG] Spring Cloud Alibaba 2023.0.3.2 + Dubbo 3.3.3 + Nacos 2.5.0 + Spring Boot 3.4.2 + JDK 21 接人 Sentinel 1.8.8 ,Sentinel dashboard 1.8.8 没有显示应用, 是什么原因?

Open HeroOnline opened this issue 1 year ago • 5 comments

Issue Description

Type: bug report

Describe what happened

Describe what you expected to happen

How to reproduce it (as minimally and precisely as possible)

Tell us your environment

Anything else we need to know?

HeroOnline avatar Feb 13 '25 04:02 HeroOnline

com.alibaba.csp.sentinel.config.SentinelConfig#getConfig(java.lang.String, boolean)

public static String getConfig(String key, boolean envVariableKey) { AssertUtil.notNull(key, "key cannot be null"); if (envVariableKey) { // 是否这里调用错了??? // String value = System.getenv(key);

    // **TODO: 是否这里换成 ???**
    String value = System.getProperty(key);
    if (StringUtil.isNotEmpty(value)) {
        return value;
    }
}
return getConfig(key);

}

HeroOnline avatar Feb 13 '25 13:02 HeroOnline

com.alibaba.csp.sentinel.config.SentinelConfig#getConfig(java.lang.String, boolean)

/** * Get config value of the specific key. * * @param key config key * @param envVariableKey Get the value of the environment variable with the given key * @return the config value. */ public static String getConfig(String key, boolean envVariableKey) { AssertUtil.notNull(key, "key cannot be null"); if (envVariableKey) { // Spring Cloud Alibaba 2023.0.3.2 + Dubbo 3.3.3 + Nacos 2.5.0 + Spring Boot 3.4.2 + JDK 21 // 接人 Sentinel 1.8.8 ,Sentinel dashboard 1.8.8 没有显示应用, 是什么原因? // TODO: 经过断点调试,发现 System.getenv(key); 获取不到值 // modify by HeroCao [email protected] @ 2025.02.13 21:09 // System.getProperty(key); 就可以获取到值 // String value = System.getenv(key);

        // add by HeroCao [email protected]  @ 2025.02.13 21:09
        String value = System.getProperty(key);
        if (StringUtil.isNotEmpty(value)) {
            return value;
        }
    }
    return getConfig(key);
}

HeroOnline avatar Feb 13 '25 13:02 HeroOnline

解决方案:

在 com.alibaba.csp.sentinel.config.SentinelConfig

中修改,如下代码:

/** * Get config value of the specific key. * * @param key config key * @return the config value. */ public static String getConfig(String key) { // modify by HeroCao [email protected] @ 2025.02.14 16:49 // AssertUtil.notNull(key, "key cannot be null"); // return props.get(key);

    // add by HeroCao [email protected] @ 2025.02.14 16:49
    // fix BUG for JDK 17、JDK 21+
    return getConfigValue(key, false);
}

/**
 * Get config value of the specific key.
 *
 * @param key            config key
 * @param envVariableKey Get the value of the environment variable with the given key
 * @return the config value.
 */
public static String getConfig(String key, boolean envVariableKey) {

// AssertUtil.notNull(key, "key cannot be null"); // if (envVariableKey) { // // // Spring Cloud Alibaba 2023.0.3.2 + Dubbo 3.3.3 + Nacos 2.5.0 + Spring Boot 3.4.2 + JDK 21 // // 接人 Sentinel 1.8.8 ,Sentinel dashboard 1.8.8 没有显示应用, 是什么原因? // // TODO: 经过断点调试,发现 System.getenv(key); 获取不到值 // // modify by HeroCao [email protected] @ 2025.02.13 21:09 // // System.getProperty(key); 就可以获取到值 //// String value = System.getenv(key); // // // add by HeroCao [email protected] @ 2025.02.13 21:09 // String value = System.getProperty(key); // if (StringUtil.isNotEmpty(value)) { // return value; // } // } // return getConfig(key);

    // add by HeroCao [email protected] @ 2025.02.14 16:49
    return getConfigValue(key, envVariableKey);
}

/**
 * Get config value of the specific key.
 * support JDK 8、JDK 11、JDK 17、JDK 21、JDK 23
 *
 * @param key            config key
 * @param envVariableKey Get the value of the environment variable with the given key
 * @return the config value.
 * @author HeroCao [email protected]
 * @date 2025-02-14 16:48
 */
public static String getConfigValue(String key, boolean envVariableKey) {
    AssertUtil.notNull(key, "key cannot be null");
    if (envVariableKey) {
        String value = System.getenv(key);
        if (StringUtil.isNotEmpty(value)) {
            return value;
        }
        value = System.getProperty(key);
        if (StringUtil.isNotEmpty(value)) {
            return value;
        }
    }

    String value = props.get(key);
    if (StringUtil.isEmpty(value)) {
        value = System.getProperty(key);
    }

    // print log
    if (StringUtil.isEmpty(value)) {
        // warn log
        RecordLog.warn(String.format("[SentinelConfig] # getConfigValue # WARN: The key=%s no existing value=%s found", key, value));
    }
    return value;
}

HeroOnline avatar Feb 15 '25 09:02 HeroOnline

还有一个问题,是显示 Dubbo 服务对应的 main方法 的类名,而不是 application name ,解决方案如下:

com.alibaba.csp.sentinel.config.SentinelConfig

类中,添加 和 修改代码如下:

/** * spring.application.name * fix BUG: JDK 17、JDK 21+ ,Dubbo 3.x show main class name * add by HeroCao [email protected] at 2025.02.14 18:08 */ public static final String APPLICATION_NAME = "spring.application.name";

private static void resolveAppName() { // add by HeroCao [email protected] Priority: spring.application.name -> system env -> csp.sentinel.app.name -> project.name -> main class (or jar) name // fix BUG: JDK 17、JDK 21+ ,Dubbo 3.x show main class name ## Begin ## String name = getConfig(APPLICATION_NAME, true); if (!StringUtil.isBlank(name)) { appName = name; RecordLog.info("App name resolved from system property {}: {}", APPLICATION_NAME, appName); return; } // fix BUG: JDK 17、JDK 21+ ,Dubbo 3.x show main class name ## End ##

    // Priority: system env -> csp.sentinel.app.name -> project.name -> main class (or jar) name
    String envKey = toEnvKey(APP_NAME_PROP_KEY);
    String n = System.getenv(envKey);
    if (!StringUtil.isBlank(n)) {
        appName = n;
        RecordLog.info("App name resolved from system env {}: {}", envKey, appName);
        return;
    }
    n = props.get(APP_NAME_PROP_KEY);
    if (!StringUtil.isBlank(n)) {
        appName = n;
        RecordLog.info("App name resolved from property {}: {}", APP_NAME_PROP_KEY, appName);
        return;
    }
    n = props.get(PROJECT_NAME_PROP_KEY);
    if (!StringUtil.isBlank(n)) {
        appName = n;
        RecordLog.info("App name resolved from property {}: {}", PROJECT_NAME_PROP_KEY, appName);
        return;
    }
    // Parse sun.java.command property by default.
    String command = System.getProperty("sun.java.command");
    if (StringUtil.isBlank(command)) {
        RecordLog.warn("Cannot resolve default appName from property sun.java.command");
        return;
    }
    command = command.split("\\s")[0];
    String separator = File.separator;
    if (command.contains(separator)) {
        String[] strs;
        if ("\\".equals(separator)) {
            // Handle separator in Windows.
            strs = command.split("\\\\");
        } else {
            strs = command.split(separator);
        }
        command = strs[strs.length - 1];
    }
    if (command.toLowerCase().endsWith(".jar")) {
        command = command.substring(0, command.length() - 4);
    }
    appName = command;
    RecordLog.info("App name resolved from default: {}", appName);
}

HeroOnline avatar Feb 15 '25 09:02 HeroOnline

Spring Boot [3.4.2] is not compatible with this Spring Cloud release train(2023.0.3.2)degrade to [3.2.x, 3.3.x] .

bryanxtong avatar Apr 24 '25 00:04 bryanxtong