1. 程式人生 > >依賴配置中心實現注有@ConfigurationProperties的bean相關屬性重新整理

依賴配置中心實現注有@ConfigurationProperties的bean相關屬性重新整理

配置中心是什麼

  配置中心,通過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) {// 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(); }

  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));//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;
        }
    }
}

  這個工具主要作用:

  1、判斷配置中心的屬性是否發生了變化

  2、為BaseConfigCenterBean子類建立代理類,使屬性在getter方法時檢測屬性是否應該重新整理。

  3、提供將BaseConfigCenterBean型別的物件的註冊為guava eventbus的監聽物件,使之具有根據重新整理事件自動重新整理自身屬性。

bean後置處理器

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。

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;

    //業務
    @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);
        }
    }
}

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;
    }

    @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這個類就可以了。

     就先分享這麼多了,更多分享請關注我們的技術公眾吧!!!