栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Apollo配置信息被程序识别的方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Apollo配置信息被程序识别的方式

1.API方式直接识别

读取默认namespace
Config config = ConfigService.getAppConfig();
String someKey = "someKeyFromDefaultNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);

读取指定namespace
String somePublicNamespace = "CAT";
Config config = ConfigService.getConfig(somePublicNamespace); //config instance is singleton for each namespace and is never null
String someKey = "someKeyFromPublicNamespace";
String someDefaultValue = "someDefaultValueForTheKey";
String value = config.getProperty(someKey, someDefaultValue);

2.基于java注解的几种方式

在application.properties/bootstrap.properties中按照如下样例配置即可
apollo.bootstrap.namespaces = application,FX.apollo
配置中增加:apollo.bootstrap.enabled = true 可保证启动就执行

@Configuration
@EnableApolloConfig(order = 2)
public class SomeAppConfig {
@Bean
public TestJavaConfigBean javaConfigBean() {
return new TestJavaConfigBean();
}
}

@Configuration
@EnableApolloConfig(value = {"FX.apollo", "FX.soa"}, order = 1)
public class AnotherAppConfig {}
//后续在具体的类中可直接使用------TestJavaConfigBean
public class TestJavaConfigBean {
@Value("{batch:200}")
public void setBatch(int batch) {
this.batch = batch;
}
public int getTimeout() {
return timeout;
}
public int getBatch() {
return batch;
}
}

@ConfigurationProperties(prefix = "redis.cache")
public class SampleRedisConfig {
private int expireSeconds;
private int commandTimeout;
public void setExpireSeconds(int expireSeconds) {
this.expireSeconds = expireSeconds;
}
public void setCommandTimeout(int commandTimeout) {
this.commandTimeout = commandTimeout;
}
}

3.基于Apollo注解的几种方式

1.@ApolloConfig 用来自动注入Config对象
2.@ApolloConfigChangeListener 用来自动注册ConfigChangeListener
3.@ApolloJsonValue 用来把配置的json字符串自动注入为对象

@Configuration
@EnableApolloConfig
public class AppConfig { 
  @Bean 
  public TestApolloAnnotationBean testApolloAnnotationBean() { 
    return new TestApolloAnnotationBean(); 
  }
}

public class TestApolloAnnotationBean { 
  @ApolloConfig 
  private Config config; //inject config for namespace application 
  @ApolloConfig("application") 
  private Config anotherConfig; //inject config for namespace application 
  @ApolloConfig("FX.apollo") 
  private Config yetAnotherConfig; //inject config for namespace FX.apollo 
   
  @ApolloJsonValue("${jsonBeanProperty:[]}") 
  private List anotherJsonBeans; 

  @Value("${batch:100}") 
  private int batch; 
 
  //config change listener for namespace application 
  @ApolloConfigChangeListener 
  private void someOnChange(ConfigChangeEvent changeEvent) { 
    //update injected value of batch if it is changed in Apollo 
    if (changeEvent.isChanged("batch")) { 
      batch = config.getIntProperty("batch", 100); 
    }
  }
  //config change listener for namespace application 
  @ApolloConfigChangeListener("application") 
  private void anotherOnChange(ConfigChangeEvent changeEvent) { 
    //do something 
  }
  //config change listener for namespaces application and FX.apollo 
  @ApolloConfigChangeListener({"application", "FX.apollo"}) 
  private void yetAnotherOnChange(ConfigChangeEvent changeEvent) { 
    //do something 
  }

  //example of getting config from Apollo directly 
  //this will always return the latest value of timeout 
  public int getTimeout() { 
    return config.getIntProperty("timeout", 200); 
  }

  //example of getting config from injected value 
  //the program needs to update the injected value when batch is changed in Apollo using @ApolloConfigChangeListener shown above 
  public int getBatch() { 
    return this.batch; 
  }

  private static class JsonBean{ 
    private String someString; 
    private int someInt; 
  }
}

4.Spring的xml格式整合Apollo

如果之前有使用org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的,请替换成org.springframework.context.support.PropertySourcesPlaceholderConfigurer。Spring 3.1以后就不建议使用PropertyPlaceholderConfigurer了,要改用PropertySourcesPlaceholderConfigurer。

 

 

     

     

     

     

         

         

    




 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号