zoukankan      html  css  js  c++  java
  • 本地环境时覆盖Apollo配置

    如果在本地环境,需要进行某些配置的更改,但是只是为了本地开发方便,例如服务端口的更改,Eureka注册地址的更改,那么可以使用该组件进行配置覆盖。
    分步指南
    1. 实现原理
    2. 代码实现
    3. 适用场景
     
    一、实现原理
     
     
    二、代码实现
    LocalApplicationRecoverConfiguration:
     
    @Configuration
    @Slf4j
    @ConditionalOnProperty(value = "spring.profiles.active",havingValue = "local")
    public class LocalApplicationRecoverConfiguration{
     
    @Bean
    public PropertySourcesProcessor configPropertySourcesProcessor() {
    log.info("use local configPropertySourcesProcessor");
    return new LocalConfigPropertySourcesProcessor();
    }
     
    }
     
    LocalConfigPropertySourcesProcessor:
     

    @Slf4j
    public class LocalConfigPropertySourcesProcessor extends PropertySourcesProcessor implements BeanDefinitionRegistryPostProcessor {

    private static final String LOCAL_ACTIVE = "local";

    private static final String[] LOCAL_APPLICATIONS = new String[]{"applicationConfig: [classpath:/application.yml]","applicationConfig: [classpath:/application-local.yml]","applicationConfig: [classpath:/application-local.yaml]","applicationConfig: [classpath:/application-local.properties]"};

    private int order = Integer.MAX_VALUE;

    private ConfigurableEnvironment environment;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
    String[] activeProfiles = environment.getActiveProfiles();
    if(activeProfiles != null){
    for (String activeProfile : activeProfiles) {
    if(LOCAL_ACTIVE.equals(activeProfile)){
    MutablePropertySources propertySources = environment.getPropertySources();
    for (String localApplication : LOCAL_APPLICATIONS) {
    PropertySource<?> propertySource = propertySources.get(localApplication);
    if(propertySource != null){
    propertySources.remove(localApplication);
    propertySources.addFirst(propertySource);
    }
    }
    }
    }
    }
    }

    @Override
    public void setEnvironment(Environment environment) {
    this.environment = (ConfigurableEnvironment)environment;
    }

    @Override
    public int getOrder() {
    return order;
    }
    }
     
     
    三、适用场景
    本地需要更改某些配置,但是不能直接更改Apollo,例如端口号的更改,注册中心地址的更改,数据库配置信息的更改等。简而言之就是,配置更改只对自己可见,不能影响其它开发人员。

  • 相关阅读:
    Linux下配置免密登录!
    centos7主机名的修改
    centos7和centos6区别
    Log4j的配置文件
    Java读取配置文件的几种方法
    关于WEB-INF目录下无法访问webapp下的css等静态文件
    metasploit 渗透测试笔记(meterpreter篇)
    metasploit 渗透测试笔记(基础篇)
    metasploit渗透测试笔记(内网渗透篇)
    自学PHP有哪些书籍和教程值得推荐?
  • 原文地址:https://www.cnblogs.com/ymqj520/p/14648885.html
Copyright © 2011-2022 走看看