公司最近使用了携程的开源框架apollo来对配置文件进行管理,总体上来说还是非常简单的。
1.接入apollo
1.1 在yml或者properties文件中添加apollo配置
在resource目录下创建bootstrap.properties/yml或者application.properties/yml,配置内容如下:
#apollo中的服务名称
app.id=你在apollo后台中的项目唯一标示码
#注入默认application
apollo.bootstrap.enabled=true
#优先加载apollo配置
apollo.bootstrap.eagerLoad.enabled=true
#注入多个namespace,配置在前的优先级更高
apollo.bootstrap.namespaces=profile,application
1.2将现有resource下的yml或者properties配置添加到apollo后台,并删除本地文件
1.3增加pom依赖
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.4.0</version>
</dependency>
1.4如果代码中有使用 @ConfigurationProperties 。通过增加以下代码来支持自动更新,同时这段代码也支持了日志级别自动更新
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext