一、引入依赖java
二、配置文件web
(1)apollo-env.propertiesredis
#环境 env=dev # app id app.id=coupon-center # meta服务地址 dev.meta=http://127.0.0.1:26008
(2) application.propertiesspring
apollo.bootstrap.enabled=true #引入多个namespaces apollo.bootstrap.namespaces=\ devGroup.coupon.application,\ devGroup.coupon.db,\ devGroup.coupon.redis,\ devGroup.coupon.rabbit-mq,\ devGroup.coupon.logger
三、自动刷新日志级别bootstrap
(1)日志配置文件(devGroup.coupon.logger):app
logging.level.org.springframework.web.servlet = info logging.level.springfox.documentation.spring.web = info logging.level.org.springframework.web.servlet.PageNotFound = error logging.level.org.springframework.jdbc = info
(2)刷新代码以下: ide
import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggingSystem; import org.springframework.context.annotation.Configuration; /** * 自动刷新日志 */ @Slf4j @Configuration public class ApolloLoggerConfig { private static final String LOGGER_TAG = "logging.level."; @Autowired private LoggingSystem loggingSystem; @ApolloConfigChangeListener("devGroup.coupon.logger") private void configChangeListter(ConfigChangeEvent changeEvent) { log.info("【Apollo动态修改日志级别】 自动刷新 开始"); for (String changedKey : changeEvent.changedKeys()) { if (changedKey.startsWith(LOGGER_TAG)) { ConfigChange configChange = changeEvent.getChange(changedKey); String oldValue = configChange.getOldValue(); String newValue = configChange.getNewValue(); LogLevel level = LogLevel.valueOf(newValue.toUpperCase()); loggingSystem.setLogLevel(changedKey.replace(LOGGER_TAG, ""), level); log.info("【Apollo动态修改日志级别】changedKey:【{}】, oldValue=【{}】, newValue:【{}】",changedKey, oldValue, newValue); } } log.info("【Apollo动态修改日志级别】自动刷新 结束"); } }
四、自动刷新 ConfigurationProperties 标注的类属性this
(1)须要自动刷新的类spa
package com.moon.coupon.service.controller.test; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "spring.jdbc.master") //@PropertySource("classpath:/jdbc.properties") public class JdbcConfig { private String driver; private String name; public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
(2)实现自动刷新日志
import com.ctrip.framework.apollo.core.ConfigConsts; import com.ctrip.framework.apollo.model.ConfigChange; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Slf4j @Component public class ApolloConfigChanged implements ApplicationContextAware { private ApplicationContext applicationContext; @Autowired private RefreshScope refreshScope; /** * 刷新的namespace的名字:devGroup.coupon.application * apollo自定义的几个namespace:{@link ConfigConsts} * @param changeEvent */ @ApolloConfigChangeListener("devGroup.coupon.application") private void someChangeHandler(ConfigChangeEvent changeEvent) { log.info("================Apollo 自动刷新值 开始 ==========================="); for (String changedKey : changeEvent.changedKeys()) { ConfigChange configChange = changeEvent.getChange(changedKey); String oldValue = configChange.getOldValue(); String newValue = configChange.getNewValue(); log.info("changedKey:【{}】,oldValue=【{}】, newValue:【{}】", changedKey, oldValue, newValue); } refreshProperties(changeEvent); log.info("================Apollo 自动刷新值 结束 ==========================="); } public void refreshProperties(ConfigChangeEvent changeEvent) { // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys())); //refreshScope.refreshAll(); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }