1. Namespace
1.1. What is Namespace
Namespace is a collection of configuration items, similar to the concept of a configuration file.
Apollo when creating the project, will by default create an "application" of Namespace. As the name suggests, "application" is used for the application itself, the familiar Spring Boot students know, Spring Boot project has a default configuration file application.yml. Here application.properties equivalent to "application" of Namespace. For 90% of the applications, "application" of the Namespace is already configured to meet the daily use of the scene.
1.2. How do clients get Namespace
2. Rebuild
git clone https://github.com/ctripcorp/apollo.git
After modifying the code to call $ {YOUR-WORKSPACE} /apollo/script/build.sh
Complete implementation of production after the zip package bulid.sh will again target directory under various projects
So we can get the following three packages
apollo-adminservice-1.5.0-SNAPSHOT-github.zip
apollo-configservice-1.5.0-SNAPSHOT-github.zip
apollo-portal-1.5.0-SNAPSHOT-github.zip
After modifying the database connection address after decompression, start
3. Increase the available environment
Earlier we initialize the database by executing the initialization script, the script is inserted in the last few initialization data
Thus, by modifying apollo.portal.envs to add the value of the activation field (available) environment. Directly modify the initialization script, or script to perform update again later executed. E.g:
1 UPDATE ApolloPortalDB.ServerConfig SET value = 'local,dev,test,prod' WHERE id = 1;
2 UPDATE ApolloPortalDB.ServerConfig SET value = '[{"orgId":"TEC","orgName":"技术部"}]' WHERE id = 2;
访问 http://localhost:8070/ (apollo/admin)
本例中,增加了TEST环境,还增加了一个Namespace
4. API使用方式
API方式是最简单、高效使用Apollo配置的方式,不依赖Spring框架即可使用。
4.1. 获取默认namespace的配置
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对应的实时最新的配置值。
另外,配置值从内存中获取,所以不需要应用自己做缓存。
4.2. 监听配置变化事件
监听配置变化事件只在应用真的关心配置变化,需要在配置变化时得到通知时使用,比如:数据库连接串变化后需要重建连接等。
如果只是希望每次都取到最新的配置的话,只需要按照上面的例子,调用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 });
4.3. 获取公共Namespace的配置
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);
4.4. 获取非properties格式namespace的配置
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);
5. 实时动态调整日志级别
这个功能很实用
引入依赖
1 <groupId>com.ctrip.framework.apollo</groupId>
2 <artifactId>apollo-client</artifactId>
3 <version>1.4.0</version>
4 </dependency>
日志级别配置
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
1 server.port=9000
2
3 app.id=1001
4 env=LOCAL
5 apollo.meta=http://localhost:8080
6 apollo.cacheDir=/Users/chengjiansheng/data
7 apollo.bootstrap.enabled=true
修改配置
5. 工程结构
https://github.com/chengjiansheng/apollo-demo
6. 文档