spring-cloud-config icon indicating copy to clipboard operation
spring-cloud-config copied to clipboard

After upgrading spring-cloud-config-client from 4.0.2 to 4.1.0 and Spring Boot from 3.1.5 to 3.2.0 properties from application.yml are missing

Open anvo1115 opened this issue 2 years ago • 8 comments

Initially I created an issue for spring-boot but their team asked to create it to spring-cloud. https://github.com/spring-projects/spring-boot/issues/39238

We upgraded our spring-boot-starter-web library from 3.5.1 to 3.2.0 version , spring-cloud-config-client from 4.0.2 to 4.1.0 and some functional stopped working.

  1. We have a spring.factories file where we register org.springframework.boot.BootstrapRegistryInitializer=com.my.configuration.CustomBootstrapper

  2. In CustomBootstrapper we have custom call for config-server data loading:


public class CustomBootstrapper implements BootstrapRegistryInitializer {

    public CustomBootstrapper() {
    }

    @Override
    public void initialize(BootstrapRegistry registry) {
        registry.register(ConfigServerBootstrapper.LoaderInterceptor.class,
                context -> new CustomInterceptor(new CustomDataLoader()));
    }

    @AllArgsConstructor
    static final class CustomInterceptor implements ConfigServerBootstrapper.LoaderInterceptor {
        private final CustomDataLoader customDataLoader;

        @Override
        public ConfigData apply(ConfigServerBootstrapper.LoadContext loadContext) {
            return customDataLoader.doLoad(loadContext.getLoaderContext(), loadContext.getResource());
        }
    }
 }

(3) In class CustomDataLoader we try to get custom property from application.yml :

    public class CustomDataLoader extends ConfigServerConfigDataLoader {
    private static final Log logger = LogFactory.getLog(CustomDataLoader.class);

    public CustomDataLoader() {
        super(destination -> logger);
    }

    @Override
    protected Environment getRemoteEnvironment(ConfigDataLoaderContext context, ConfigServerConfigDataResource resource, String label, String state) {
        Binder binder = context.getBootstrapContext().get(Binder.class);
        BindResult<String> propertyBindResult = binder.bind("my-property", String.class);
       // other code
     }

// other code

Unfortunately we got null in propertyBindResult. But previously it returned the value set in application.yml.

anvo1115 avatar Jan 18 '24 21:01 anvo1115

There are some changes we made which effect when getRemoteEnvironment is called and what configuration property sources are loaded.

I think this PR would actually give you the functionality you need.

Then you could do something like

ConfigServerConfigDataLocationResolver.PropertyResolver resolver = context.getBootstrapContext()
				.get(ConfigServerConfigDataLocationResolver.PropertyResolver.class);
		String myProperty = resolver.get("my-property", String.class, "default-value");

If you could build my branch and test this out using Spring Cloud 2022.0.5-SNAPSHOT that would go a long ways to making sure that PR addresses your use case as well.

ryanjbaxter avatar Jan 19 '24 20:01 ryanjbaxter

@ryanjbaxter , I tested your changes. Unfortunately I always get "default-value". But we expected to get the value from application.yml file.

anvo1115 avatar Jan 22 '24 00:01 anvo1115

Here is a sample I used to confirm things worked...maybe I am not doing the same thing.

demo.zip

ryanjbaxter avatar Jan 22 '24 22:01 ryanjbaxter

@ryanjbaxter , thank you for the demo. it worked on my side, too. I found that in my project I use spring-boot spring-boot 3.1.8 and you have 3.1.9-SNAPSHOT. Did you make changes there , too?

I'm trying to set all the dependencies as in your project.

anvo1115 avatar Jan 24 '24 14:01 anvo1115

No that shouldnt matter

ryanjbaxter avatar Jan 24 '24 14:01 ryanjbaxter

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Jan 31 '24 14:01 spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-cloud-issues avatar Feb 07 '24 14:02 spring-cloud-issues

I found how to reproduce the issue in your Demo.

Add to your test class DemoApplicationTests


@SpringBootTest(properties = {
		"spring.config.import=optional:configserver:http://localhost:8065"
			})

Than you will see that the default value was returned instead of value from application.properties.

anvo1115 avatar Feb 11 '24 22:02 anvo1115

When you add the properties property to the annotation src/main/resources/application.properties is no longer included in the Environment, that is why default-value is returned. If you don't add that property to the annotation it works as expected.

ryanjbaxter avatar Mar 08 '24 20:03 ryanjbaxter

Is it expected behaviour? Earlier in spring-cloud-config-client 4.0.2 and Spring Boot 3.1.5 everything worked anyway. If you consider that this is right, will you merge your fix into spring-cloud-config-client 4.1.x ?

anvo1115 avatar Mar 10 '24 21:03 anvo1115

The change is merged into 4.1.x at this point.

Is this the expected behavior for the @SpringBootTest annotation? I am not sure, that's a question for the Boot team, but that is what I observe happening.

ryanjbaxter avatar Mar 10 '24 21:03 ryanjbaxter

I created an issue for spring-boot: https://github.com/spring-projects/spring-boot/issues/39940

anvo1115 avatar Mar 13 '24 22:03 anvo1115

@ryanjbaxter , When are you planning to release 4.1.x ? I can see that only 4.0.5 is released.

anvo1115 avatar Mar 13 '24 23:03 anvo1115

The current plan is to have a release on March 26th

https://github.com/spring-cloud/spring-cloud-release/milestones

ryanjbaxter avatar Mar 13 '24 23:03 ryanjbaxter

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Mar 20 '24 23:03 spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-cloud-issues avatar Mar 27 '24 23:03 spring-cloud-issues