sasiperi / sasiperi.github.io

0 stars 0 forks source link

Update for "Spring Cloud Config Server and Legacy Spring Apps" #6

Open stubob opened 3 years ago

stubob commented 3 years ago

Hey, your article was a life-saver. Not sure how many people are left out there that want to enable Spring Cloud Config for a legacy app, but I sure will use it. I had a couple changes to the code if you're interested. I ditched the second class for the Environment and just included it inline:

@Override
protected ConfigurableEnvironment createEnvironment() {
    System.out.println("##################### loaded context");
    return new StandardServletEnvironment(){
        @Override
        protected void customizePropertySources(MutablePropertySources propertySources)
        {

I also put in a check to see if the PropertySource loaded correctly:

super.customizePropertySources(propertySources);
            try
            {
                PropertySource<?> source = initConfigServicePropertySourceLocator(this);
                if(source != null) {
                    propertySources.addLast(source);
                }
            }

I wanted to be able to use the bootstrap.properties like CloudConfigClient is supposed to when it starts up, so I changed:

private PropertySource<?> initConfigServicePropertySourceLocator(ConfigurableEnvironment environment)
            throws IOException {
            ClassPathResource resource = new ClassPathResource("bootstrap.properties");
            if(resource.exists()){
                Properties props = PropertiesLoaderUtils.loadProperties(resource);
                MutablePropertySources propertySources = environment.getPropertySources();
                propertySources
                    .addFirst(new PropertiesPropertySource("bootstrap", props));
            }
            LOGGER.info("*********  SASI LOADING CUSTOM **********");
            ConfigClientProperties configClientProperties = new ConfigClientProperties(environment);
            System.out.println(environment.toString());

            ConfigServicePropertySourceLocator configServicePropertySourceLocator = new ConfigServicePropertySourceLocator(configClientProperties);

to load bootstrap.properties if it exists, and add it as the first PropertySource. That way I can put spring.cloud.config.uri and spring.application.name in bootstrap.properties. Other than that, this was just what I needed. I went back and forth between trying to load my applicationConfig.xml from Spring, or loading Spring from the applicationConfig.xml.

I wanted to comment on the article, but the page won't reload once I log in. Thanks again.

sasiperi commented 3 years ago

Hello @stubob,

I am very sorry somehow I did not notice this till now! I am glad this blog helped you! I liked your suggestions. Would you mind, fork and making the change in my repo named "blog"? And make PR (pull request), I will merge the changes in and the blog will be updated. (url: https://github.com/sasiperi/blog)

Its OK if you can not, and it's too much work for you, I will make the change later.

THANKS AGAIN for the feedback and great suggestions!

Regards Sasi Peri