配置中心是什么
配置中心,通過key=value的形式存儲環境變量。配置中心的屬性做了修改,項目中可以通過配置中心的依賴(sdk)立即感知到。需要做的就是如何在屬性發生變化時,改變帶有@ConfigurationProperties的bean的相關屬性。
配置中心原理
在讀配置中心源碼的時候發現,里面維護了一個Environment,以及ZookeeperPropertySource。當配置中心屬性發生變化的時候,清空ZookeeperPropertySource,并放入最新的屬性值。
public class ZookeeperPropertySource extends EnumerablePropertySource<Properties>
ZookeeperPropertySource重寫了equals和hahscode方法,根據這兩個方法可以判定配置中心是否修改了屬性。
?
動態刷新bean屬性原理
實現原理圖
動態刷新bean父類
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) {// NOSONARString 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;}@Overridepublic 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;}@Subscribepublic 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(); }
1、isCfgCenterEffect方法主要判斷項目是否接入了配置中心并且配置中心配有bean中相關的屬性。
2、binding方法主要根據isCfgCenterEffect方法的返回值去加載配置中心的properties還是本地的properties。
3、getDefaultResourcePath是主要是獲取本地資源的默認路徑(在沒有接入配置中心的情況下)。
4、getConfigPrefix方法返回bean中配置屬性的公共前綴(等同于@ConfigurationProperties中的prefix屬性)。
5、refreshForEvent方法主要是在某個bean感知到配置中心更新屬性時異步通知其他bean進行屬性的更新。
bean屬性綁定工具類
動態將propertysource綁定到帶有@ConfigurationProperties注解的bean中。
參考?org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor
?
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));//NOSONARprivate 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 proxiesenhancer.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;}} }
這個工具主要作用:
1、判斷配置中心的屬性是否發生了變化
2、為BaseConfigCenterBean子類創建代理類,使屬性在getter方法時檢測屬性是否應該刷新。
3、提供將BaseConfigCenterBean類型的對象的注冊為guava eventbus的監聽對象,使之具有根據刷新事件自動刷新自身屬性。
bean后置處理器
public class ConfigCenterBeanPostProcessor implements BeanPostProcessor {@Overridepublic 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;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;} }
該后置處理器的作用是對所有BaseConfigCenterBean類型的bean進行處理,生成代理bean,并注冊為guava eventbus相應的listener。
pojo屬性綁定配置中心優雅方案1
@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;//業務 @ConfigFieldprivate List<Scene> bizs;//業務和渠道映射關系 @ConfigFieldprivate 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;}@ToInitialprivate 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);}}//刷新事件調用 @Overridepublic void refreshForEvent() {this.refreshQrcodeProperties();}//本地資源文件 @Overridepublic String getDefaultResourcePath() {return "config/qrcode.properties";}//屬性配置 公共前綴(和@ConfigurationProperties prefix 屬性一致) @Overridepublic String getConfigPrefix() {return "wx.temporary.qrcode";}//模塊名稱 @Overridepublic String getModuleName() {return "微信臨時二維碼配置";}@Overridepublic 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;}@Overridepublic String toString() {return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE, false, false, Scene.class);}} }
pojo屬性綁定配置中心優雅方案2
@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;}@ToInitialprivate 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);}}@Overridepublic void refreshForEvent() {this.refreshQrcodeProperties();}@Overridepublic String getDefaultResourcePath() {return "config/qrcode.properties";}@Overridepublic String getConfigPrefix() {return "wx.temporary.qrcode";}@Overridepublic String getModuleName() {return "微信臨時二維碼配置";}@Overridepublic 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;}@Overridepublic String toString() {return ReflectionToStringBuilder.toString(this, ToStringStyle.JSON_STYLE, false, false, Scene.class);}} }
方案1和方案2略有不同,針對一些屬性,我們需要做一些邏輯處理。方案1中將源屬性和邏輯之后的屬性都放在了同一類中,方案二則是將源屬性單獨放到一個靜態類中,最終處理過后的屬性放在了目標類中。另外二者的doBind方法也是有區別的,仔細看一下BaseConfigCenterBean這個類就可以了。
?
? ? ?就先分享這么多了,更多分享請關注我們的技術公眾吧!!!
參考文章:算法和技術SHARING