Apollo阿波罗配置中心
2017-10-19 19:42
543 查看
一、准备工作
1.2.1 AppId classpath:/META-INF/app.properties 内容app.id=YOUR-APP-ID
1.2.2 Environment 对于Mac/Linux,文件位置为/opt/settings/server.properties 例如env=DEV 详细见文档。
1.2.3 本地缓存路径 /opt/data/{appId}/config-cache (权限)
二、Maven Dependency
3.1 API使用方式
3.2.1 基于XML的配置
Apollo同时还增加了两个新的Annotation来简化在Spring环境中的使用。
@ApolloConfig
用来自动注入Config对象
@ApolloConfigChangeListener
用来自动注册ConfigChangeListener
github
junit单元测试
2、Environment
对于Mac/Linux,文件位置为/opt/settings/server.properties
对于Windows,文件位置为C:\opt\settings\server.properties
3、本地缓存
Mac/Linux: /opt/data/{appId}/config-cache
Windows: C:\opt\data{appId}\config-cache
1.2.1 AppId classpath:/META-INF/app.properties 内容app.id=YOUR-APP-ID
1.2.2 Environment 对于Mac/Linux,文件位置为/opt/settings/server.properties 例如env=DEV 详细见文档。
1.2.3 本地缓存路径 /opt/data/{appId}/config-cache (权限)
二、Maven Dependency
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>0.7.0</version> </dependency>三、客户端用法
3.1 API使用方式
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null String someKey = "someKeyFromDefaultNamespace"; String someDefaultValue = "someDefaultValueForTheKey"; String value = config.getProperty(someKey, someDefaultValue);3.1.2 监听配置变化事件
Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null config.addChangeListener(new ConfigChangeListener() { @Override public void onChange(ConfigChangeEvent changeEvent) { System.out.println("Changes for namespace " + changeEvent.getNamespace()); for (String key : changeEvent.changedKeys()) { ConfigChange change = changeEvent.getChange(key); System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType())); } } });3.1.3 获取公共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);3.2 Spring整合方式
3.2.1 基于XML的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:apollo="http://www.ctrip.com/schema/apollo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.ctrip.com/schema/apollo http://www.ctrip.com/schema/apollo.xsd"> <apollo:config order="2"/> <!-- 这个是最复杂的配置形式,指示Apollo注入FX.apollo和FX.soa namespace的配置到Spring环境中,并且顺序在application前面 --> <apollo:config namespaces="FX.apollo,FX.soa" order="1"/> </beans>3.2.3 Spring Annotation支持
Apollo同时还增加了两个新的Annotation来简化在Spring环境中的使用。
@ApolloConfig
用来自动注入Config对象
@ApolloConfigChangeListener
用来自动注册ConfigChangeListener
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 @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; } }转载文章:Apollo配置
github
junit单元测试
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring.xml") public class TestApolloConfig { @Autowired private ApolloUtil apolloUtil; @Test public void testAllconfig() throws Exception { Set<String> keys = apolloUtil.getPropertyNames(); for (Object key : keys) { String val=apolloUtil.getString(key.toString()); System.out.println(key.toString() + "===" + val); } } }
2、Environment
对于Mac/Linux,文件位置为/opt/settings/server.properties
对于Windows,文件位置为C:\opt\settings\server.properties
3、本地缓存
Mac/Linux: /opt/data/{appId}/config-cache
Windows: C:\opt\data{appId}\config-cache
相关文章推荐
- Ubuntu 16.04下搭建基于携程Apollo(阿波罗)配置中心单机模式
- 携程Apollo(阿波罗)配置中心把现有项目的配置文件迁移到Apollo
- 携程Apollo(阿波罗)配置中心在.NET Core项目快速集成
- 携程Apollo(阿波罗)配置中心在Spring Boot项目快速集成
- 携程Apollo(阿波罗)配置中心使用Google代码风格文件(在Eclipse使用Google代码风格)(配合阿里巴巴代码规约快速设置)
- 携程Apollo(阿波罗)配置中心Spring Boot迁移日志组件,使用配置中心进行管理的思路
- 携程Apollo(阿波罗)配置中心的Java样例客户端启动和调试
- 携程的配置中心(阿波罗apollo)
- 携程Apollo(阿波罗)配置中心在.NET Core项目快速集成
- SpringCloud系列:整合Apollo实现分布式配置中心(一)
- 携程Apollo(阿波罗)配置中心本地开发模式不接入配置中心进行本地开发
- Apollo 配置中心详解
- 学习使用Apollo配置中心
- 携程 Apollo 配置中心 | 学习笔记(三) 自定义Spring Boot项目通过配置中心启动项目
- .NET Core微服务之基于Apollo实现统一配置中心
- CentOS 7 搭建基于携程Apollo(阿波罗)配置中心单机模式
- 携程Apollo(阿波罗)配置中心用户管理和部门管理
- Apollo配置管理中心
- 携程Apollo(阿波罗)配置中心用户管理和部门管理
- Apollo配置中心-多组共享配置(关于Namespaces)