Namespace是配置项的集合,类似于一个配置文件的概念。
Apollo在创建项目的时候,都会默认创建一个“application”的Namespace。顾名思义,“application”是给应用自身使用的,熟悉Spring Boot的同学都知道,Spring Boot项目都有一个默认配置文件application.yml。在这里application.properties就等同于“application”的Namespace。对于90%的应用来说,“application”的Namespace已经满足日常配置使用场景了。
git clone https://github.com/ctripcorp/apollo.git
执行完bulid.sh以后会再各个项目的target目录下生产zip包
于是,我们就可以得到以下三个包
apollo-adminservice-1.5.0-SNAPSHOT-github.zip
apollo-configservice-1.5.0-SNAPSHOT-github.zip
apollo-portal-1.5.0-SNAPSHOT-github.zip
解压后修改数据库连接地址后,启动
前面我们通过执行初始化脚本来初始化数据库,在脚本的最后插入了几条初始化数据
因此,可以通过修改 apollo.portal.envs 字段的值来添加激活的(可用的)环境。直接修改初始化脚本,或者脚本执行完以后再执行update。例如:
API方式是最简单、高效使用Apollo配置的方式,不依赖Spring框架即可使用。
1 // config instance is singleton for each namespace and is never null 2 Config config = ConfigService.getAppConfig(); 3 String someKey = "someKeyFromDefaultNamespace"; 4 String someDefaultValue = "someDefaultValueForTheKey"; 5 String value = config.getProperty(someKey, someDefaultValue);
通过上述的config.getProperty可以获取到someKey对应的实时最新的配置值。
另外,配置值从内存中获取,所以不需要应用自己做缓存。
监听配置变化事件只在应用真的关心配置变化,需要在配置变化时得到通知时使用,比如:数据库连接串变化后需要重建连接等。
如果只是希望每次都取到最新的配置的话,只需要按照上面的例子,调用config.getProperty即可。
1 // config instance is singleton for each namespace and is never null 2 Config config = ConfigService.getAppConfig(); 3 config.addChangeListener(new ConfigChangeListener() { 4 @Override 5 public void onChange(ConfigChangeEvent changeEvent) { 6 System.out.println("Changes for namespace " + changeEvent.getNamespace()); 7 for (String key : changeEvent.changedKeys()) { 8 ConfigChange change = changeEvent.getChange(key); 9 System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType())); 10 } 11 } 12 });
1 String somePublicNamespace = "CAT"; 2 Config config = ConfigService.getConfig(somePublicNamespace); 3 String someKey = "someKeyFromPublicNamespace"; 4 String someDefaultValue = "someDefaultValueForTheKey"; 5 String value = config.getProperty(someKey, someDefaultValue);
apollo-client 1.3.0版本开始对yaml/yml做了更好的支持,使用起来和properties格式一致。
1 Config config = ConfigService.getConfig("application.yml"); 2 String someKey = "someKeyFromYmlNamespace"; 3 String someDefaultValue = "someDefaultValueForTheKey"; 4 String value = config.getProperty(someKey, someDefaultValue);
这个功能很实用
引入依赖
日志级别配置
1 package com.cjs.example.config; 2 3 import com.ctrip.framework.apollo.Config; 4 import com.ctrip.framework.apollo.model.ConfigChange; 5 import com.ctrip.framework.apollo.model.ConfigChangeEvent; 6 import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; 7 import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; 8 import lombok.extern.slf4j.Slf4j; 9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.boot.logging.LogLevel; 11 import org.springframework.boot.logging.LoggingSystem; 12 import org.springframework.context.annotation.Configuration; 13 import org.springframework.util.StringUtils; 14 15 import java.util.Set; 16 17 /** 18 * @author ChengJianSheng 19 * @date 2019-05-31 20 */ 21 @Slf4j 22 @Configuration 23 public class LoggerConfig { 24 25 private static final String LOGGER_TAG = "logging.level."; 26 27 /** 28 * 注入默认的命名空间配置 29 */ 30 @ApolloConfig 31 private Config config; 32 33 @Autowired 34 private LoggingSystem loggingSystem; 35 36 @ApolloConfigChangeListener 37 private void onChange(ConfigChangeEvent configChangeEvent) { 38 System.out.println("配置发生变化"); 39 System.out.println("Changes for namespace " + configChangeEvent.getNamespace()); 40 for (String key : configChangeEvent.changedKeys()) { 41 ConfigChange change = configChangeEvent.getChange(key); 42 System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType())); 43 } 44 45 Set<String> keyNames = config.getPropertyNames(); 46 for (String key : keyNames) { 47 if (StringUtils.isEmpty(key)) { 48 continue; 49 } 50 if (!key.startsWith(LOGGER_TAG)) { 51 continue; 52 } 53 54 String loggerName = key.replace(LOGGER_TAG, ""); 55 String strLevel = config.getProperty(key, "info"); 56 LogLevel level = LogLevel.valueOf(strLevel.toUpperCase()); 57 loggingSystem.setLogLevel(loggerName, level); 58 59 log.info("{}:{}", key, strLevel); 60 } 61 } 62 63 }
application.properties
修改配置
https://github.com/chengjiansheng/apollo-demo