Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does it support dynamic configuration addition? #3900

Closed
youngledo opened this issue Nov 21, 2024 · 7 comments
Closed

Does it support dynamic configuration addition? #3900

youngledo opened this issue Nov 21, 2024 · 7 comments
Labels
area/nacos spring cloud alibaba nacos kind/question

Comments

@youngledo
Copy link

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 yuluo-yx added kind/question area/nacos spring cloud alibaba nacos labels Nov 21, 2024
@yuluo-yx
Copy link
Collaborator

啥意思?没理解

@youngledo
Copy link
Author

youngledo commented Nov 21, 2024

上面的扩展配置(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来达到目的。

@shiyiyue1102
Copy link
Contributor

ExtensionConfigs 在未来版本中会废弃,统一到Spring官方推荐的spring.config.import引入自定义属性源

@shiyiyue1102
Copy link
Contributor

NacosConfigManager

NacosConfigManager是SCA内部组件,不对外承诺API稳定性

@youngledo
Copy link
Author

NacosConfigManager

NacosConfigManager是SCA内部组件,不对外承诺API稳定性

那还有其它方法吗?因为动态加载配置确实有场景需要。

@shiyiyue1102
Copy link
Contributor

需要

Spring官方加载外部数据源的方式就是通过spring.config.import, 并不建议通过SCA内部API去扩展配置加载的行为,SCA未来也没有对这部分功能做增强的计划,会增加内部代码的复杂性。
如果要实现你的需求,建议还是基于spring.config.import这一参数,可以通过shell脚本在应用启动时区动态修改application.properties,spring.config.import支持导入多个nacos配置。
示例如下:
spring.config.import[0]=optional:nacos:datasource.properties?group=config&refreshEnabled=true
spring.config.import[1]=nacos:cipher-kms-aes-256-datasource.properties?group=datasource&refreshEnabled=true
spring.config.import[2]=optional:nacos:SampleApp.application.properties?group=default
spring.config.import[3]=optional:nacos:SampleApp.application.yaml?group=default&refreshEnabled=true

@youngledo
Copy link
Author

好的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/nacos spring cloud alibaba nacos kind/question
Projects
None yet
Development

No branches or pull requests

3 participants