Spring cloud configuration client

Override remote configuration properties

By default, Spring Cloud is allowed to be overridden, spring.cloud.config.allowOverride=true
Adjust the value to "false" through the program start parameter

--spring.cloud.config.allowOverride=true

After startup, Postman sends POST request again, and adjusts the value of spring.application.name to "spring cloud new"

Custom Bootstrap configuration

  1. Create META-INF/spring.factories file {similar to Spring boot custom Starter}
  2. Custom Bootstrap configuration configuration
package com.segmentfault.springcloudlesson1.bootstrap;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;

import java.util.HashMap;
import java.util.Map;

/**
 * bootstrap Configure bean
 */
@Configuration
public class MyConfiguration implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        //Get the ConfigurableEnvironment instance from ConfigurableApplicationContext
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        //Get PropertySources
        MutablePropertySources propertySources = environment.getPropertySources();
        //Define a new propertySource and put it first
        propertySources.addFirst(createPropertySource());
    }

    private PropertySource createPropertySource() {
        Map<String, Object> source = new HashMap<>();
        source.put("name", "shuai");
        PropertySource propertySource = new MapPropertySource("my-property-source", source);
        return propertySource;
    }
}
  1. Configure the MEAT-INF/spring.factories file and associate the Key
org.springframework.cloud.bootstrapConfiguration= \
com.segmentfault.springcloudlesson2.bootstrap.Myconfiguration

Custom Bootstrap configuration property source

  1. Implement PropertySourceLocator
package com.segmentfault.springcloudlesson1.bootstrap;

import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Custom {@ link PropertySourceLocator} implementation
 */
public class MyPropertySourceLocator implements PropertySourceLocator {
    @Override
    public PropertySource<?> locate(Environment environment) {
        if (environment instanceof ConfigurableEnvironment) {
            ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);
            //Get PropertySources
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            //Define a new propertySource and put it first
            propertySources.addFirst(createPropertySource());
        }
        return null;
    }

    private PropertySource createPropertySource() {
        Map<String, Object> source = new HashMap<>();
        source.put("spring.application.name", "shuaishuai");
        //Set name and source
        PropertySource propertySource = new MapPropertySource("over-bootstrap", source);
        return propertySource;
    }
}

This is a wrong step to understand how

  1. MyPropertySourceLocator is exposed as a Spring Bean

@Bean
public MyPropertySourceLocator myPropertySourceLocator() {

return new MyPropertySourceLocator();

}

2. Configure META-INF/spring.factories

org.springframework.cloud.bootstrapConfiguration= \
com.segmentfault.springcloudlesson1.bootstrap.MyConfiguration,\
com.segmentfault.springcloudlesson1.bootstrap.MyPropertySourceLocator

Keywords: Java Spring

Added by tolli on Sat, 07 Dec 2019 21:05:55 +0200