Spring cloud配置客戶端(三)

覆蓋遠程配置屬性

默認情況,Spring Cloud是允許覆蓋的,spring.cloud.config.allowOverride=true
通過程序啓動參數,調整這個值爲"false"

--spring.cloud.config.allowOverride=true

啓動後,重新Postman發送POST請求,調整spring.application.name值爲"spring-cloud-new"

自定義Bootstrap配置

  1. 創建META-INF/spring.factories文件{類似於Spring boot 自定義Starter}
  2. 自定義Bootstrap配置Configurtion
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 配置bean
 */
@Configuration
public class MyConfiguration implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        //從ConfigurableApplicationContext獲取ConfigurableEnvironment實例
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        //獲取PropertySources
        MutablePropertySources propertySources = environment.getPropertySources();
        //定義一個新的 propertySource,並放置在首位
        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. 配置MEAT-INF/spring.factories文件,關聯Key
org.springframework.cloud.bootstrapConfiguration= \
com.segmentfault.springcloudlesson2.bootstrap.Myconfiguration

自定義Bootstrap配置屬性源

  1. 實現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;

/**
 * 自定義{@link PropertySourceLocator}實現
 */
public class MyPropertySourceLocator implements PropertySourceLocator {
    @Override
    public PropertySource<?> locate(Environment environment) {
        if (environment instanceof ConfigurableEnvironment) {
            ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);
            //獲取PropertySources
            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
            //定義一個新的 propertySource,並放置在首位
            propertySources.addFirst(createPropertySource());
        }
        return null;
    }

    private PropertySource createPropertySource() {
        Map<String, Object> source = new HashMap<>();
        source.put("spring.application.name", "shuaishuai");
        //設置名稱和來源
        PropertySource propertySource = new MapPropertySource("over-bootstrap", source);
        return propertySource;
    }
}

這是個錯誤步驟,目的是瞭解此方式

  1. MyPropertySourceLocator暴露成 Spring Bean

@Bean
public MyPropertySourceLocator myPropertySourceLocator() {

return new MyPropertySourceLocator();

}

2配置META-INF/spring.factories

org.springframework.cloud.bootstrapConfiguration= \
com.segmentfault.springcloudlesson1.bootstrap.MyConfiguration,\
com.segmentfault.springcloudlesson1.bootstrap.MyPropertySourceLocator
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章