上一篇 <<<Apollo在代码中使用时的配置信息
下一篇 >>>传统任务调度实现方案及demo示例
1.API方式直接识别
2.基于java注解的几种方式
3.基于Apollo注解的几种方式
@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 annotated on fields example, the default value is specified as empty list - []
* <br />
* jsonBeanProperty=[{"someString":"hello","someInt":100},{"someString":"world!","someInt":200}]
*/
@ApolloJsonValue("${jsonBeanProperty:[]}")
private List<JsonBean> 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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[<u>http://www.springframework.org/schema/beans</u>](http://www.springframework.org/schema/beans)"
xmlns:xsi="[<u>http://www.w3.org/2001/XMLSchema-instance</u>](http://www.w3.org/2001/XMLSchema-instance)"
xmlns:apollo="[<u>http://www.ctrip.com/schema/apollo</u>](http://www.ctrip.com/schema/apollo)"
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)
[<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)">
<apollo:config order="2"/><!—-启用默认的namespaces-->
<!-- 这个是最复杂的配置形式,指示Apollo注入FX.apollo和FX.soa namespace的配置到Spring环境中,并且顺序在application前面 -->
<apollo:config namespaces="FX.apollo,FX.soa" order="1"/>
<bean class="com.ctrip.framework.apollo.spring.TestXmlBean">
<property name="timeout" value="${timeout:100}"/>
<property name="batch" value="${batch:200}"/>
</bean>
</beans>
推荐阅读:
<<<传统配置的缺陷与常用分布式配置中心介绍
<<<SpringCloud配置中心实现原理
<<<SpringCloud配置步骤与使用说明
<<<SpringCloud配置更新后的刷新机制
<<<Apollo配置中心总体设计原理
<<<Apollo客户端与服务端同步原理
<<<Apollo配置更新的推送机制
<<<Apollo单机环境搭建
<<<Apollo多环境部署
<<<Apollo操作手册之基础配置
<<<Apollo操作手册之项目设置
<<<Apollo操作手册之配置说明汇总
<<<Apollo操作手册之配置集群环境
<<<Apollo操作手册之Namespace管理
<<<Apollo操作手册之配置增删改操作
<<<Apollo操作手册之配置同步发布和回滚操作
<<<Apollo操作手册之配置灰度发布
<<<Apollo在代码中使用时的配置信息