spring-cloud / spring-cloud-consul

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

Declaring a custom context in spring.config.import leads to error caused by an extra leading '/' #730

Closed fmartinou closed 3 years ago

fmartinou commented 3 years ago

Hi,

Every microservice we run can access its configuration on Consul under a folder conventioned as $product/$environment/$module/$app/$version

Example: my-product/my-env/my-module/my-app/2.0.0

To configure our Spring Boot apps, we set on them an env var: SPRING_CONFIG_IMPORT=consul:/my-product/my-env/my-module/my-app/0.0.0

Unfortunately our configuration is never overridden with values from Consul because of a bug in ConsulPropertySource

// context = /my-product/my-env/my-module/my-app/0.0.0
// key       =  my-module/my-env/my-module/my-app/0.0.0/sample/nested/my-super-prop1
// please pay attention to the leading '/' which is the cause of all the troubles....

// So the Line 101 key = key.replace(this.context, "") doesn't strip the context as expected!

protected void parsePropertiesInKeyValueFormat(List<GetValue> values) {
        if (values == null) {
            return;
        }

        for (GetValue getValue : values) {
            String key = getValue.getKey();
            if (!StringUtils.endsWithIgnoreCase(key, "/")) {
                key = key.replace(this.context, "").replace('/', '.');
                String value = getValue.getDecodedValue();
                this.properties.put(key, value);
            }
        }
    }

Previously (Spring Boot 2.4.5) we used a workaround by defining a ; in the String: SPRING_CONFIG_IMPORT=consul:localhost:8500;my-product/my-env/my-module/my-app/0.0.0

But is does not work anymore on greater versions of Spring Boot/Spring Cloud . We now get a java.lang.NumberFormatException: For input string: "8500;"

Is that something that can be directly fixed in Spring Cloud Consul or is there an other solution to get it working?

fmartinou commented 3 years ago

Hi,

Any news on this?

fmartinou commented 3 years ago

I found a workaround to achieve what I want (make Spring Cloud Consul get the configuration from my-product/my-env/my-module/my-app/2.0.0)

I removed the context from SPRING_CONFIG_IMPORT and I defined a custom prefix and a custom appname.

Example:

SPRING_CONFIG_IMPORT=consul:
SPRING_CLOUD_CONSUL_CONFIG_PREFIX=my-product/my-env
SPRING_CLOUD_CONSUL_CONFIG_NAME=my-module/my-app/2.0.0

It's a little dirty because Spring Cloud Consul will also watch for keys under my-product/my-env/application 😞 .

spencergibb commented 3 years ago

Closed via #744