Spring Cloud使用BootstrapConfiguration配置启动加载项

使用BootstrapConfiguration加载,等同于bootstrap.yml中的配置

使用PropertySourceLocator接口

@Configuration
public class BootstrapConfigurer implements PropertySourceLocator {

    private static final Logger log = LoggerFactory.getLogger(BootstrapConfigurer .class);
    /**
     * @param environment the current Environment
     * @return a PropertySource or null if there is none
     * @throws IllegalStateException if there is a fail fast condition
     */
    @Override
    public PropertySource<?> locate(Environment environment) {
        Map<String, Object> property = setConfig(environment);
        if (property != null && property.size() > 0) {
            return new MapPropertySource("customProperty", property);
        } else {
            return null;
        }
    }

    private Map<String, Object> setConfig(Environment environment) {
        Map<String, Object> property = new HashMap<>();
        property.put("eureka.instance.lease-renewal-interval-in-seconds", "5");
        return property;
    }
}

在META-INF的spring.factories中添加

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.xxx.BootstrapConfigurer

 


版权声明:本文为xiejiefeng333原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。