alibaba / spring-cloud-alibaba

Spring Cloud Alibaba provides a one-stop solution for application development for the distributed solutions of Alibaba middleware.
https://sca.aliyun.com
Apache License 2.0
27.99k stars 8.35k forks source link

Does it support dynamic configuration addition? #3900

Open youngledo opened 1 day ago

youngledo commented 1 day ago

Which Component Nacos Config

Describe what problem you have encountered 支持动态添加配置的扩展吗?比如在代码中对扩展配置添加新的配置,而不是写死在yaml中。 Does it support dynamic configuration addition? For example, adding new configurations to the extension configuration in the code instead of writing them in YAML.

image

不知道nacos是否有对应的扩展,如果没有是不是只能通过Spring底层的方法加载? I don't know if Nacos has corresponding extensions. If not, can it only be loaded through the underlying methods of Spring?

yuluo-yx commented 1 day ago

啥意思?没理解

youngledo commented 1 day ago

上面的扩展配置(extension-configs)是写死的,某些时候dataId是在启动时通过环境变量注入的(配置还是在nacos中),这样就得通过代码的形式动态增加。

@Configuration
public class McNacosBootstrapConfiguration {

    private static final String APP_CODE_KEY = "application.code";

    @Bean
    public NacosConfigManager nacosConfigManager(NacosConfigProperties nacosConfigProperties, Environment environment) {
        // 动态加载配置
        String codeValue = environment.getProperty(APP_CODE_KEY);
        if (codeValue != null && !codeValue.trim().isEmpty()) {
            for (String code : codeValue.split(",")) {
                nacosConfigProperties.getExtensionConfigs().add(
                        new NacosConfigProperties.Config("abc" + code + ".properties", "multi", true)
                );
            }
        }
        return new NacosConfigManager(nacosConfigProperties);
    }

}

发现可以覆盖NacosConfigManager来达到目的。