• 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字符串自动注入为对象

    1. @Configuration
    2. @EnableApolloConfig
    3. public class AppConfig {
    4. @Bean
    5. public TestApolloAnnotationBean testApolloAnnotationBean() {
    6. return new TestApolloAnnotationBean();
    7. }
    8. }

    1. public class TestApolloAnnotationBean {
    2. @ApolloConfig
    3. private Config config; //inject config for namespace application
    4. @ApolloConfig("application")
    5. private Config anotherConfig; //inject config for namespace application
    6. @ApolloConfig("FX.apollo")
    7. private Config yetAnotherConfig; //inject config for namespace FX.apollo
    8. /**
    9. * ApolloJsonValue annotated on fields example, the default value is specified as empty list - []
    10. * <br />
    11. * jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}]
    12. */
    13. @ApolloJsonValue("${jsonBeanProperty:[]}")
    14. private List<JsonBean> anotherJsonBeans;
    15. @Value("${batch:100}")
    16. private int batch;
    17. //config change listener for namespace application
    18. @ApolloConfigChangeListener
    19. private void someOnChange(ConfigChangeEvent changeEvent) {
    20. //update injected value of batch if it is changed in Apollo
    21. if (changeEvent.isChanged("batch")) {
    22. batch = config.getIntProperty("batch", 100);
    23. }
    24. }
    25. //config change listener for namespace application
    26. @ApolloConfigChangeListener("application")
    27. private void anotherOnChange(ConfigChangeEvent changeEvent) {
    28. //do something
    29. }
    30. //config change listener for namespaces application and FX.apollo
    31. @ApolloConfigChangeListener({"application", "FX.apollo"})
    32. private void yetAnotherOnChange(ConfigChangeEvent changeEvent) {
    33. //do something
    34. }
    35. //example of getting config from Apollo directly
    36. //this will always return the latest value of timeout
    37. public int getTimeout() {
    38. return config.getIntProperty("timeout", 200);
    39. }
    40. //example of getting config from injected value
    41. //the program needs to update the injected value when batch is changed in Apollo using @ApolloConfigChangeListener shown above
    42. public int getBatch() {
    43. return this.batch;
    44. }
    45. private static class JsonBean{
    46. private String someString;
    47. private int someInt;
    48. }
    49. }

    4.Spring的xml格式整合Apollo

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

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="[<u>http://www.springframework.org/schema/beans</u>](http://www.springframework.org/schema/beans)"
    3. xmlns:xsi="[<u>http://www.w3.org/2001/XMLSchema-instance</u>](http://www.w3.org/2001/XMLSchema-instance)"
    4. xmlns:apollo="[<u>http://www.ctrip.com/schema/apollo</u>](http://www.ctrip.com/schema/apollo)"
    5. xsi:schemaLocation="[<u>http://www.springframework.org/schema/beans</u>](http://www.springframework.org/schema/beans) [<u>http://www.springframework.org/schema/beans/spring-beans.xsd</u>](http://www.springframework.org/schema/beans/spring-beans.xsd)
    6. [<u>http://www.ctrip.com/schema/apollo</u>](http://www.ctrip.com/schema/apollo) [<u>http://www.ctrip.com/schema/apollo.xsd</u>](http://www.ctrip.com/schema/apollo.xsd)">
    7. <apollo:config order="2"/><!—-启用默认的namespaces-->
    8. <!-- 这个是最复杂的配置形式,指示Apollo注入FX.apollo和FX.soa namespace的配置到Spring环境中,并且顺序在application前面 -->
    9. <apollo:config namespaces="FX.apollo,FX.soa" order="1"/>
    10. <bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
    11. <property name="timeout" value="${timeout:100}"/>
    12. <property name="batch" value="${batch:200}"/>
    13. </bean>
    14. </beans>



     

  • 相关阅读:
    干货:如何在前端统计用户访问来源?
    [java]深度剖析自动装箱与拆箱
    PAM从入门到精通(二)
    leetCode 746. 使用最小花费爬楼梯
    比色免疫测定:Enzo CHO宿主细胞蛋白ELISA试剂盒方案
    工业路由器网关的网络协议之NAT技术
    navicat和dbeaver使用对比以及优缺点
    LeetCode每日一题——1235. 规划兼职工作
    一文详述流媒体传输网络MediaUni
    设计模式-建造者模式
  • 原文地址:https://blog.csdn.net/jmysql/article/details/125432532