配置中心,經過key=value的形式存儲環境變量。配置中心的屬性作了修改,項目中能夠經過配置中心的依賴(sdk)當即感知到。須要作的就是如何在屬性發生變化時,改變帶有@ConfigurationProperties的bean的相關屬性。java
在讀配置中心源碼的時候發現,裏面維護了一個Environment,以及ZookeeperPropertySource。當配置中心屬性發生變化的時候,清空ZookeeperPropertySource,並放入最新的屬性值。算法
public class ZookeeperPropertySource extends EnumerablePropertySource<Properties>
ZookeeperPropertySource重寫了equals和hahscode方法,根據這兩個方法能夠斷定配置中心是否修改了屬性。spring
public abstract class BaseConfigCenterBean implements InitializingBean { private static Logger LOGGER = LoggerFactory.getLogger(BaseConfigCenterBean.class); //配置中心是否生效 protected boolean cfgCenterEffect = false; public boolean isCfgCenterEffect() { this.checkCfgCenterEffect(); return cfgCenterEffect; } private void checkCfgCenterEffect() { boolean tmpCfgCenterEffect = !Objects.isNull(ConfigHelper.getEnvironment()); if (tmpCfgCenterEffect) {// NOSONAR String value = (String) ConfigHelper.getZookeeperPropertySource().getProperty("cfg.center.effect"); if (StringUtils.isBlank(value)) { tmpCfgCenterEffect = false; } else { tmpCfgCenterEffect = Boolean.valueOf(value); } } cfgCenterEffect = tmpCfgCenterEffect; if (cfgCenterEffect) { String prefix = this.getConfigPrefix(); cfgCenterEffect = Arrays.stream(ConfigHelper.getZookeeperPropertySource().getPropertyNames()) .filter(keyName -> keyName.indexOf(prefix) == 0) .count() > 0; if (!cfgCenterEffect) { LOGGER.info(String.format("配置中心沒有發現模塊=%s, prefix=%s的配置,將使用本地配置...", this.getModuleName(), prefix)); } } } /** * 綁定自身目標 **/ protected void doBind() { Class<? extends BaseConfigCenterBean> clazz = this.getClass(); if (AopUtils.isCglibProxy(this)) { clazz = (Class<? extends BaseConfigCenterBean>) AopUtils.getTargetClass(this); } BaseConfigCenterBean target = binding(isCfgCenterEffect(), clazz, this.getDefaultResourcePath()); this.copyProperties(target); } private void copyProperties(BaseConfigCenterBean target) { ReflectionUtils.doWithFields(this.getClass(), field -> { field.setAccessible(true); field.set(this, field.get(target)); }, field -> AnnotatedElementUtils.isAnnotated(field, ConfigField.class)); } /** * 綁定其餘目標 * * @param clazz 目標類 **/ protected <T> T doBind(Class<T> clazz) { T target = binding(isCfgCenterEffect(), clazz, this.getDefaultResourcePath()); if (target instanceof InitializingBean) { try { ((InitializingBean) target).afterPropertiesSet(); } catch (Exception e) { LOGGER.error(String.format("屬性初始化失敗[afterPropertiesSet], class=%s", ClassUtils.getSimpleName(clazz), e)); } } return target; } private <T> T binding(boolean cfgCenterEffect, Class<T> clazz, String defaultResourcePath) { Optional<PropertySource> propertySource = Optional.empty(); if (cfgCenterEffect) { propertySource = Optional.ofNullable(ConfigHelper.getZookeeperPropertySource()); } else { Optional<ResourcePropertySource> resourcePropertySource = ResourceUtils.getResourcePropertySource(defaultResourcePath); if (resourcePropertySource.isPresent()) { propertySource = Optional.ofNullable(resourcePropertySource.get()); } } if (propertySource.isPresent()) { T target; try { target = RelaxedConfigurationBinder .with(clazz) .setPropertySources(propertySource.get()) .doBind(); } catch (GeneralException e) { LOGGER.error(String.format("屬性綁定失敗, class=%s", ClassUtils.getSimpleName(clazz)), e); return null; } return target; } return null; } @Override public void afterPropertiesSet() { Class<?> target = this.getClass(); if (AopUtils.isAopProxy(this)) { target = AopUtils.getTargetClass(this); } LOGGER.info(String.format("%s->%s模塊引入配置中心%s...", this.getModuleName(), ClassUtils.getSimpleName(target), (isCfgCenterEffect() ? "生效" : "無效"))); } public String getModuleName() { return StringUtils.EMPTY; } @Subscribe public void listenRefreshEvent(ConfigCenterUtils.ConfigRefreshEvent refreshEvent) { if (!refreshEvent.getModuleName().equals(this.getModuleName())) { this.refreshForEvent(); } } //經過事件進行刷新 public abstract void refreshForEvent(); //獲取本地配置默認路徑 public abstract String getDefaultResourcePath(); //獲取配置屬性的公共前綴 public abstract String getConfigPrefix(); }
一、isCfgCenterEffect方法主要判斷項目是否接入了配置中心而且配置中心配有bean中相關的屬性。微信
二、binding方法主要根據isCfgCenterEffect方法的返回值去加載配置中心的properties仍是本地的properties。app
三、getDefaultResourcePath是主要是獲取本地資源的默認路徑(在沒有接入配置中心的狀況下)。異步
四、getConfigPrefix方法返回bean中配置屬性的公共前綴(等同於@ConfigurationProperties中的prefix屬性)。ide
五、refreshForEvent方法主要是在某個bean感知到配置中心更新屬性時異步通知其餘bean進行屬性的更新。工具
動態將propertysource綁定到帶有@ConfigurationProperties註解的bean中。post
參考 org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessorui
public class RelaxedConfigurationBinder<T> { private final PropertiesConfigurationFactory<T> factory; public RelaxedConfigurationBinder(T object) { this(new PropertiesConfigurationFactory<>(object)); } public RelaxedConfigurationBinder(Class<?> type) { this(new PropertiesConfigurationFactory<>(type)); } public static <T> RelaxedConfigurationBinder<T> with(T object) { return new RelaxedConfigurationBinder<>(object); } public static <T> RelaxedConfigurationBinder<T> with(Class<T> type) { return new RelaxedConfigurationBinder<>(type); } public RelaxedConfigurationBinder(PropertiesConfigurationFactory<T> factory) { this.factory = factory; ConfigurationProperties properties = getMergedAnnotation(factory.getObjectType(), ConfigurationProperties.class); javax.validation.Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); factory.setValidator(new SpringValidatorAdapter(validator)); factory.setConversionService(new DefaultConversionService()); if (!Objects.isNull(properties)) {//NOSONAR factory.setIgnoreNestedProperties(properties.ignoreNestedProperties()); factory.setIgnoreInvalidFields(properties.ignoreInvalidFields()); factory.setIgnoreUnknownFields(properties.ignoreUnknownFields()); factory.setTargetName(properties.prefix()); factory.setExceptionIfInvalid(properties.exceptionIfInvalid()); } } public RelaxedConfigurationBinder<T> setTargetName(String targetName) { factory.setTargetName(targetName); return this; } public RelaxedConfigurationBinder<T> setPropertySources(PropertySource<?>... propertySources) { MutablePropertySources sources = new MutablePropertySources(); for (PropertySource<?> propertySource : propertySources) { sources.addLast(propertySource); } factory.setPropertySources(sources); return this; } public RelaxedConfigurationBinder<T> setPropertySources(Environment environment) { factory.setPropertySources(((ConfigurableEnvironment) environment).getPropertySources()); return this; } public RelaxedConfigurationBinder<T> setPropertySources(PropertySources propertySources) { factory.setPropertySources(propertySources); return this; } public RelaxedConfigurationBinder<T> setConversionService(ConversionService conversionService) { factory.setConversionService(conversionService); return this; } public RelaxedConfigurationBinder<T> setValidator(Validator validator) { factory.setValidator(validator); return this; } public RelaxedConfigurationBinder<T> setResolvePlaceholders(boolean resolvePlaceholders) { factory.setResolvePlaceholders(resolvePlaceholders); return this; } public T doBind() throws GeneralException { try { return factory.getObject(); } catch (Exception ex) { throw new GeneralException("配置綁定失敗!", ex); } } }
public class ConfigCenterUtils { private static Logger LOGGER = LoggerFactory.getLogger(ConfigCenterUtils.class); private static AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(8));//NOSONAR private static Properties cfgProperties; private static Environment environment; static { cfgProperties = new Properties(); cfgProperties.putAll(ConfigHelper.getZookeeperPropertySource().getProperties()); } public static void setEnvironment(Environment environment) { ConfigCenterUtils.environment = environment; } public static String getValue(String name) { try { return PropertiesUtil.getValue(name); } catch (Exception e) { LOGGER.info("配置中心無效, property name=" + name, e); } if (Objects.isNull(environment)) { LOGGER.info("environment無效,property name=" + name); return StringUtils.EMPTY; } if (!environment.containsProperty(name)) { LOGGER.info("environment無配置 property name=" + name); return StringUtils.EMPTY; } return environment.getProperty(name); } public synchronized static boolean propertySourceShouldRefresh(String moduleName, ZookeeperPropertySource newPropertySource) { if (!cfgProperties.equals(newPropertySource.getProperties())) { cfgProperties.clear(); cfgProperties.putAll(newPropertySource.getProperties()); eventBus.post(new ConfigRefreshEvent(moduleName)); return true; } return false; } public static <T> T createToRefreshPropertiesBean(Class<T> clazz) { Enhancer enhancer = new Enhancer(); // 設置代理對象父類 enhancer.setSuperclass(clazz); // 標識Spring-generated proxies enhancer.setInterfaces(new Class[]{SpringProxy.class}); // 設置加強 enhancer.setCallback((MethodInterceptor) (target, method, args, methodProxy) -> { ToRefresh toRefresh = AnnotationUtils.findAnnotation(method, ToRefresh.class); if (Objects.isNull(toRefresh) || StringUtils.isBlank(toRefresh.method())) { return methodProxy.invokeSuper(target, args); } Method refreshMethod = ReflectionUtils.findMethod(target.getClass(), toRefresh.method()); if (Objects.isNull(refreshMethod)) { return methodProxy.invokeSuper(target, args); } refreshMethod = BridgeMethodResolver.findBridgedMethod(refreshMethod); refreshMethod.setAccessible(true); refreshMethod.invoke(target, null); return methodProxy.invokeSuper(target, args); }); T target = (T) enhancer.create();// 建立代理對象 MethodIntrospector.selectMethods(clazz, (ReflectionUtils.MethodFilter) method -> AnnotatedElementUtils.isAnnotated(method, ToInitial.class)) .stream().findFirst().ifPresent(method -> { method.setAccessible(true); try { method.invoke(target, null); } catch (Exception e) { LOGGER.error(String.format("初始化異常,class=%s ...", ClassUtils.getSimpleName(clazz)), e); } }); return target; } public static void registerListener(BaseConfigCenterBean refreshableBean) { eventBus.register(refreshableBean); } public static class ConfigRefreshEvent { private String moduleName; public ConfigRefreshEvent(String moduleName) { this.moduleName = moduleName; } public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } } }
這個工具主要做用:
一、判斷配置中心的屬性是否發生了變化
二、爲BaseConfigCenterBean子類建立代理類,使屬性在getter方法時檢測屬性是否應該刷新。
三、提供將BaseConfigCenterBean類型的對象的註冊爲guava eventbus的監聽對象,使之具備根據刷新事件自動刷新自身屬性。
public class ConfigCenterBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (AnnotatedElementUtils.isAnnotated(bean.getClass(), ConfigCenterBean.class)) { BaseConfigCenterBean refreshableBean = (BaseConfigCenterBean) ConfigCenterUtils.createToRefreshPropertiesBean(bean.getClass()); ConfigCenterUtils.registerListener(refreshableBean); return refreshableBean; } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }
該後置處理器的做用是對全部BaseConfigCenterBean類型的bean進行處理,生成代理bean,並註冊爲guava eventbus相應的listener。
@ConfigCenterBean @ConfigurationProperties(prefix = "wx.temporary.qrcode") @Component public class QrcodeConstants extends BaseConfigCenterBean { private static Logger LOGGER = LoggerFactory.getLogger(QrcodeConstants.class); //渠道 @ConfigField //標識該屬性來自配置中心 private List<Scene> channels; //業務 @ConfigField private List<Scene> bizs; //業務和渠道映射關係 @ConfigField private Map<String, String> biz2Channel; private Map<String, Scene> channelMap; private Map<String, Scene> bizMap; public List<Scene> getChannels() { return channels; } public void setChannels(List<Scene> channels) { this.channels = channels; } public List<Scene> getBizs() { return bizs; } public void setBizs(List<Scene> bizs) { this.bizs = bizs; } @ToRefresh(method = "toRefresh") public Map<String, Scene> getChannelMap() { return channelMap; } @ToRefresh(method = "toRefresh") public Map<String, Scene> getBizMap() { return bizMap; } @ToRefresh(method = "toRefresh") public Map<String, String> getBiz2Channel() { return biz2Channel; } public void setBiz2Channel(Map<String, String> biz2Channel) { this.biz2Channel = biz2Channel; } @ToInitial private void refreshQrcodeProperties() { try { super.doBind(); //屬性處理 if (CollectionUtils.isEmpty(channels)) { this.channelMap = Maps.newHashMap(); } else { this.channelMap = channels.stream() .collect(Collectors.toMap(channel -> channel.getType(), Function.identity())); } if (CollectionUtils.isEmpty(bizs)) { this.bizMap = Maps.newHashMap(); } else { this.bizMap = bizs.stream() .collect(Collectors.toMap(biz -> biz.getType(), Function.identity())); } LOGGER.info(String.format("%s 刷新成功..., 當前配置=%s...", this.getModuleName(), this)); } catch (Exception e) { LOGGER.error("QrcodeConstants 對象屬性綁定失敗...", e); } } private void toRefresh() { try { if (isCfgCenterEffect()) { ZookeeperPropertySource propertySource = ConfigHelper.getZookeeperPropertySource(); if (ConfigCenterUtils.propertySourceShouldRefresh(this.getModuleName(), propertySource)) { this.refreshQrcodeProperties(); } } } catch (Exception e) { LOGGER.error("QrcodeConstants 對象屬性刷新失敗", e); } } //刷新事件調用 @Override public void refreshForEvent() { this.refreshQrcodeProperties(); } //本地資源文件 @Override public String getDefaultResourcePath() { return "config/qrcode.properties"; } //屬性配置 公共前綴(和@ConfigurationProperties prefix 屬性一致) @Override public String getConfigPrefix() { return "wx.temporary.qrcode"; } //模塊名稱 @Override public String getModuleName() { return "微信臨時二維碼配置"; } @Override public String toString() { return ReflectionToStringBuilder.toString(this , ToStringStyle.JSON_STYLE , false , false , QrcodeConstants.class); } public static class Scene { private String type; private String desc; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return ReflectionToStringBuilder.toString(this , ToStringStyle.JSON_STYLE , false , false , Scene.class); } } }
@ConfigCenterBean @Component public class QrcodeConstants extends BaseConfigCenterBean { private static Logger LOGGER = LoggerFactory.getLogger(QrcodeConstants.class); //業務和渠道映射關係 private Map<String, String> biz2Channel; //渠道 private Map<String, Scene> channelMap; //業務 private Map<String, Scene> bizMap; private QrcodeProperties qrcodeProperties; @ToRefresh(method = "toRefresh") public Map<String, Scene> getChannelMap() { return channelMap; } @ToRefresh(method = "toRefresh") public Map<String, Scene> getBizMap() { return bizMap; } @ToRefresh(method = "toRefresh") public Map<String, String> getBiz2Channel() { return biz2Channel; } public void setBiz2Channel(Map<String, String> biz2Channel) { this.biz2Channel = biz2Channel; } public QrcodeProperties getRawQrcodeProperties() { return qrcodeProperties; } @ToInitial private void refreshQrcodeProperties() { try { QrcodeProperties qrcodeProperties = super.doBind(QrcodeProperties.class); if (Objects.isNull(qrcodeProperties)) { LOGGER.error(String.format("沒有加載到%s配置,請檢查配置...", this.getModuleName())); return; } this.qrcodeProperties = qrcodeProperties; //屬性處理 if (CollectionUtils.isEmpty(qrcodeProperties.channels)) { this.channelMap = Maps.newHashMap(); } else { this.channelMap = qrcodeProperties.channels.stream() .collect(Collectors.toMap(channel -> channel.getType(), Function.identity())); } if (CollectionUtils.isEmpty(qrcodeProperties.bizs)) { this.bizMap = Maps.newHashMap(); } else { this.bizMap = qrcodeProperties.bizs.stream() .collect(Collectors.toMap(biz -> biz.getType(), Function.identity())); } if (CollectionUtils.isEmpty(qrcodeProperties.getBiz2Channel())) { this.biz2Channel = Maps.newHashMap(); } else { this.biz2Channel = qrcodeProperties.getBiz2Channel(); } LOGGER.info(String.format("%s 刷新成功..., 當前配置=%s...", this.getModuleName(), this)); } catch (Exception e) { LOGGER.error("QrcodeConstants 對象屬性綁定失敗...", e); } } private void toRefresh() { try { if (isCfgCenterEffect()) { ZookeeperPropertySource propertySource = ConfigHelper.getZookeeperPropertySource(); if (ConfigCenterUtils.propertySourceShouldRefresh(this.getModuleName(), propertySource)) { this.refreshQrcodeProperties(); } } } catch (Exception e) { LOGGER.error("QrcodeConstants 對象屬性刷新失敗", e); } } @Override public void refreshForEvent() { this.refreshQrcodeProperties(); } @Override public String getDefaultResourcePath() { return "config/qrcode.properties"; } @Override public String getConfigPrefix() { return "wx.temporary.qrcode"; } @Override public String getModuleName() { return "微信臨時二維碼配置"; } @Override public String toString() { return new ToStringBuilder(this) .append("biz2Channel", biz2Channel) .append("channelMap", channelMap) .append("bizMap", bizMap) .toString(); } @ConfigurationProperties(prefix = "wx.temporary.qrcode") public static class QrcodeProperties { //渠道 private List<Scene> channels; //業務 private List<Scene> bizs; //業務和渠道映射關係 private Map<String, String> biz2Channel; public List<Scene> getChannels() { return channels; } public void setChannels(List<Scene> channels) { this.channels = channels; } public List<Scene> getBizs() { return bizs; } public void setBizs(List<Scene> bizs) { this.bizs = bizs; } public Map<String, String> getBiz2Channel() { return biz2Channel; } public void setBiz2Channel(Map<String, String> biz2Channel) { this.biz2Channel = biz2Channel; } } public static class Scene { private String type; private String desc; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return ReflectionToStringBuilder.toString(this , ToStringStyle.JSON_STYLE , false , false , Scene.class); } } }
方案1和方案2略有不一樣,針對一些屬性,咱們須要作一些邏輯處理。方案1中將源屬性和邏輯以後的屬性都放在了同一類中,方案二則是將源屬性單獨放到一個靜態類中,最終處理事後的屬性放在了目標類中。另外兩者的doBind方法也是有區別的,仔細看一下BaseConfigCenterBean這個類就能夠了。
就先分享這麼多了,更多分享請關注咱們的技術公衆吧!!!
參考文章:算法和技術SHARING