spring-cloud / spring-cloud-consul

Spring Cloud Consul
http://cloud.spring.io/spring-cloud-consul/
Apache License 2.0
813 stars 541 forks source link

can't use system properties to override configuration in consul #398

Open timxi opened 6 years ago

timxi commented 6 years ago

I'm using spring could consul 1.3.2.release version.

Here is my bootstrap.yml:

spring:
  application:
    name: myApp
  cloud:
    config:
      allow-override: true
      override-system-properties: false
      override-none: false
    consul:
      host: localhost
      port: 8500
      config:
        enabled: true

with configuration when I passed -Dmy.prop=system the parameter for my java application, the application still get my.prop from instead of the command line.

I debugged the PropertySourceBootstrapConfiguration source in insertPropertySources method, it seems that spring cloud create new PropertySourceBootstrapProperties with default value in code, it doesn't get the configuration from bootstrap.yml:

    private void insertPropertySources(MutablePropertySources propertySources,
            CompositePropertySource composite) {
        MutablePropertySources incoming = new MutablePropertySources();
        incoming.addFirst(composite);
        PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties();
        new RelaxedDataBinder(remoteProperties, "spring.cloud.config")
                .bind(new PropertySourcesPropertyValues(incoming));
        if (!remoteProperties.isAllowOverride() || (!remoteProperties.isOverrideNone()
                && remoteProperties.isOverrideSystemProperties())) {
            propertySources.addFirst(composite);
            return;
        }
spencergibb commented 6 years ago

Spring Cloud config properties have no effect on consul config

timxi commented 6 years ago

thanks, @spencergibb, how can I override the configuration value in consul?

for example, I have my.prop=123 in consul, how can I override this via command line?

spencergibb commented 6 years ago

Why do you want to override consul?

timxi commented 6 years ago

sometimes I just want to change some of the application node configurations to do some test not all of the nodes. If I can override the value in consul, it will give me the flexibility to pass the override value via command and restart the individual node.

jwbennet commented 6 years ago

I ran into this issue as well today and you can set the values for spring.cloud.config.allow-override, spring.cloud.config.override-system-properties and spring.cloud.config.override-none but they have to be set in Consul. It feels a bit odd because you cannot configure that along with the rest of the configuration which you put in bootstrap.yml.

spencergibb commented 6 years ago

@jwbennet setting those values in consul seems wrong, chicken and egg problem.

SystemOutPrint commented 6 years ago

You can try the following configuration.

default:
  my-prop: default-value 

my:
  prop: ${default.my-prop}

java -jar xxx.jar --default.my-prop=xxx

spencergibb commented 6 years ago

@SystemOutPrint it's not a bug, I don't need to reproduce it, it's an enhancement request.