Sentinel icon indicating copy to clipboard operation
Sentinel copied to clipboard

properties加载建议?

Open VanXD opened this issue 8 years ago • 1 comments

根据代码可以看到是拿的${appName}.properties文件中的配置. 但是这个appName要从JVM属性上拿, 实在不太方便... 咱们可以考虑像Spring或者Dubbo一样, 默认就去拿application.properties或者dubbo.properties吗? 或者最好的就是提供JavaConfig方案?我们就可以想在哪里写配置都行了.

    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("read SentinelConfig 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));
                    try {
                        String systemValue = System.getProperty((String)key);
                        if (!StringUtil.isEmpty(systemValue)) {
                            SentinelConfig.setConfig((String)key, systemValue);
                        }
                    } catch (Exception e) {
                        RecordLog.info(e.getMessage(), e);
                    }
                    RecordLog.info(key + " value: " + SentinelConfig.getConfig((String)key));
                }
            }
        } catch (Throwable ioe) {
            RecordLog.info(ioe.getMessage(), ioe);
        }

        // JVM parameter override file config.
        for (Map.Entry<Object, Object> entry : System.getProperties().entrySet()) {
            SentinelConfig.setConfig(entry.getKey().toString(), entry.getValue().toString());
        }
    }

VanXD avatar Aug 16 '18 07:08 VanXD

This will be fixed in #804.

If you're using Spring Boot/Spring Cloud you could leverage Spring Cloud Alibaba to support configuring via application.properties: https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel#more

sczyh30 avatar Jul 08 '19 03:07 sczyh30