携程 Apollo 配置中心:Example
本文介绍如何基于 Spring Boot 来搭建 Apollo 客户端,并展示如何动态更改运行时服务的输出日志等级。参考阅读 Apollo · Java 客户端使用指南 以及 Apollo · 使用示例
搭建客户端
- 加入依赖
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.3.0</version>
</dependency>
- 在
application.yml
中添加配置:
# appid 是应用的身份信息,需后台设定
app:
id: 2019
# 一般指向 config service 服务地址
apollo:
meta: http://localhost:8080
- 在启动类上添加
@EnableApolloConfig
注解用于开启 Apollo 客户端:
@EnableApolloConfig
@SpringBootApplication
public class ApolloApplication {
public static void main(String[] args) {
SpringApplication.run(ApolloApplication.class, args);
System.out.println("ApolloApplication started...");
// 监听配置变化事件
Config config = ConfigService.getAppConfig();
config.addChangeListener(changeEvent -> {
log.debug("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()));
}
});
}
}
动态调整日志级别
- 在
application.yml
中设定日志等级
logging:
level:
cn.mariojd.config.apollo: info
- 配置监听事件,实现动态变更日志等级
@Slf4j
@Configuration
public class DynamicLogLevelConfig {
@Resource
private LoggingSystem loggingSystem;
private static final String LOG_KEY = "logging.level";
@ApolloConfigChangeListener
private void configChangeListener(ConfigChangeEvent e) {
e.changedKeys().stream().filter(key -> StringUtils.startsWithIgnoreCase(LOG_KEY, key)).forEach(key -> {
ConfigChange change = e.getChange(key);
String oleLevel = change.getOldValue();
String newLevel = change.getNewValue();
// 只有合法的日志等级配置才会生效,常见的如 DEBUG, INFO, WARN, ERROR
Arrays.stream(LogLevel.values()).filter(logLevel ->
StringUtils.startsWithIgnoreCase(newLevel, logLevel.toString())).findFirst()
.ifPresent(logLevel -> loggingSystem.setLogLevel("cn.mariojd.config.apollo", logLevel));
System.out.println(String.format("动态调整日志级别:Key -> %s ; OldLevel -> %s ; NewLevel -> %s",
key, oleLevel, newLevel));
});
}
}
- 进行简单的测试,项目中引入了
lombok
@Slf4j
@EnableApolloConfig
@SpringBootApplication
public class ApolloApplication implements InitializingBean {
private ExecutorService executorService = Executors.newFixedThreadPool(1);
public static void main(String[] args) {
SpringApplication.run(ApolloApplication.class, args);
log.info("ApolloApplication started...");
}
@Override
public void afterPropertiesSet() {
executorService.submit(this::testDynamicLogLevelConfig);
}
public void testDynamicLogLevelConfig() {
// 默认 INFO 级别下程序不输出 DEBUG 日志
for (; ; ) {
log.debug("Debug log...");
log.info("Info log...");
log.warn("Warn log...");
log.error("Error log...");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 在 Apollo Protal 管理后台新增项目
2019
,添加配置并发布
- 查看应用日志输出,符合预期,测试结束
Docker 部署 Apollo 服务导致 Apollo 客户端 无法访问?
参考这里提供的几种解决方案。当然了,最简单的方式还是在启动时指定-Dapollo.configService=http://IP:PORT
,来跳过meta service
的服务发现:
示例源码
欢迎关注我的个人公众号:超级码里奥
如果这对您有帮助,欢迎点赞和分享,转载请注明出处