springboot logback从配置中心读取环境
·
springboot logback从配置中心读取环境
ScenDemoApplication.java
package com.scen.web;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*
*
* @author Scen
* @date 2018/4/28 10:41
*/
@SpringBootApplication
@EnableApolloConfig
public class ScenDemoApplication {
public static void main(String[] args) {
//logback从系统变量中取spring.profiles.active的值的时间先于应用从apollo配置中心获取配置再set进系统变量的时间
Config config = ConfigService.getAppConfig();
String key = "logback.env";
String defaultValue = "prod";
String value = config.getProperty(key, defaultValue);
System.out.println(value);
System.setProperty("spring.profiles.active", value);
SpringApplication.run(ScenDemoApplication.class, args);
}
}
用的是apollo配置中心,其他配置中心同理(不是最优方案,只是提供一个思路,和springboot的启动顺序有关),logback配置方式可以参考上一篇
springboot logback按天打印日志分环境控制日志级别彩色日志控制单个日志文件大小
2019-04-21更新:现在只需在apollo配置中心配置spring.profiles.active=dev或者spring.profiles.active=prod,然后在本地的配置文件里加上apollo.bootstrap.eagerLoad.enabled=true(先于日志框架加载配置)这个配置,就解决了加载顺序问题,其他非apollo配置中心可能还得用以前的方法除非配置中心支持eagerLoad
更多推荐
所有评论(0)